设为首页 加入收藏

TOP

ansible常用模块的介绍与使用(二)
2023-07-23 13:38:52 】 浏览:159
Tags:ansible 常用模
118.130 -m raw -a 'cat /tmp/test|grep -Eo hello' 192.168.118.130 | CHANGED | rc=0 >> hello Shared connection to 192.168.118.130 closed.

ansible常用模块之shell

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

//在受控主机上简单创建一个脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# ls
[root@localhost scripts]# vim test.sh 
#!/bin/bash

for i in $(seq 10);do
        echo $i
done

//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.118.130 -m shell -a '/bin/bash /scripts/test.sh'
192.168.118.130 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10

ansible常用模块之script

script模块用于在受控机上执行主控机上的脚本

//在主控机上创建一个脚本
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# mkdir scripts
[root@ansible ~]# cd scripts/
[root@ansible scripts]# vim a.sh
#!/bin/bash

echo "hello world"

//执行脚本时需要绝对路径
[root@ansible ~]# ansible 192.168.118.130 -m script -a '/etc/ansible/scripts/a.sh'
192.168.118.130 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.118.130 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.118.130 closed."
    ],
    "stdout": "hello world\r\n",
    "stdout_lines": [
        "hello world"
    ]
}

ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上。

//将根目录下的anaconda-ks.cfg传送到受控主机下的/tmp/下,并赋予775的权限
[root@ansible ~]# ansible 192.168.118.130 -m template -a 'src=~/anaconda-ks.cfg dest=/tmp/ mode=0775'
192.168.118.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "71e48dd2e917295afbd891c5eae07b2593600f38",
    "dest": "/tmp/anaconda-ks.cfg",
    "gid": 0,
    "group": "root",
    "md5sum": "2157b1294c5aef23139307ff63685719",
    "mode": "0775",
    "owner": "root",
    "size": 1092,
    "src": "/root/.ansible/tmp/ansible-tmp-1666514565.4204142-105228-4638286770822/source",
    "state": "file",
    "uid": 0
}

//可以看见在受控主机下的/tmp/下有anaconda-ks.cfg这个文件,并且有775的权限
[root@ansible ~]# ansible 192.168.118.130 -a 'ls -l /tmp/'
192.168.118.130 | CHANGED | rc=0 >>
total 8
-rwxrwxr-x  1 root root 1092 Oct 23 16:42 anaconda-ks.cfg
drwx------  2 root root   41 Oct 23 16:44 ansible_command_payload__vyel6sg
-rw-r--r--  1 root root   12 Oct 23 16:13 test
drwx------  2 root root    6 Oct 23 16:02 vmware-root_922-2722632355
drwx------. 2 root root    6 Oct 23 15:56 vmware-root_930-2722763397

ansible常用模块之copy

copy模块用于复制文件至远程受控机

[root@ansible ~]# ls /etc/ansible/scripts/
a.sh
//将a.sh复制到受控主机
[root@ansible ~]# ansible 192.168.118.130 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
192.168.118.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "62a1e17a8ab950e59e56563ebf327d9a14520575",
    "dest": "/scripts/a.sh",
    "gid": 0,
    "group": "root"
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ansible部署LNMP 下一篇海思3516系列芯片SPI速率慢问题深..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目