ansible常用模块

ansible常用模块用法 file模块用法

ansible常用模块使用详解

ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块rawcommandshell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

[root@localhost ~]# ansible all -m ping
192.168.253.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.253.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.253.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。

//查看受控主机的/tmp目录
[root@localhost ~]# ansible 192.168.253.131 -a 'ls /tmp'
192.168.253.131 | CHANGED | rc=0 >>
ansible_command_payload_t181ftrf
ks-script-4i88rwq9
ks-script-m7jrq744
systemd-private-63c02f80df3d43d3a37bec98b9a03e22-chronyd.service-VjFHIi
vmware-root_861-3988621786
vmware-root_894-2730693566
vmware-root_903-3979774182

//在受控主机的/tmp目录下新建一个文件test
[root@localhost ~]# ansible 192.168.253.131 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
192.168.253.131 | CHANGED | rc=0 >>

[root@localhost ~]# ansible 192.168.253.131 -a 'ls /tmp'
192.168.253.131 | CHANGED | rc=0 >>
ansible_command_payload_3m25p27a
systemd-private-63c02f80df3d43d3a37bec98b9a03e22-chronyd.service-VjFHIi
test
vmware-root_894-2730693566

//command模块不支持管道符、重定向
[root@localhost ~]# ansible 192.168.253.131 -a "echo 'hello world' > /tmp/test"
192.168.253.131 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@localhost ~]# ansible 192.168.253.131 -a 'cat /tmp/test'
192.168.253.131 | CHANGED | rc=0 >>

[root@localhost ~]# ansible 192.168.253.131-a 'ps -ef|grep vsftpd'
192.168.253.131 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help '
  or 'ps --help '
 for additional help text.

For more details see ps(1).non-zero return code

ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

//支持重定向
[root@localhost ~]# ansible 192.168.253.131 -m raw -a "echo 'hello world' > /tmp/test"
192.168.253.131 | CHANGED | rc=0 >>
Shared connection to 192.168.253.131 closed.

[root@localhost ~]# ansible 192.168.253.131 -a 'cat /tmp/test'
192.168.253.131 | CHANGED | rc=0 >>
hello world

//支持管道符
[root@localhost ~]# ansible 192.168.253.131 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.253.131 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.253.131 closed.

ansible常用模块之shell

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

//查看受控主机上的脚本
[root@localhost scripts]# ll
total 4
-rwxr-xr-x. 1 root root 35 Oct 23 14:32 test.sh

//使用shell模块执行受控机上的脚本
[root@localhost ~]# ansible 192.168.253.131 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.253.131 | CHANGED | rc=0 >>

[root@localhost ~]# ansible 192.168.253.131 -m shell -a 'cat /tmp/test'
192.168.253.131 | CHANGED | rc=0 >>
hello world!!!

ansible常用模块之script

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

[root@localhost ~]# mkdir -p /etc/ansible/scripts/
[root@localhost ~]# cd /etc/ansible/scripts/
[root@localhost scripts]# vim a.sh
[root@localhost scripts]# cd
[root@localhost ~]# ll /etc/ansible/scripts/
total 4
-rw-r--r--. 1 root root 35 Oct 23 14:42 a.sh

//执行
[root@localhost ~]# ansible 192.168.253.131 -m script -a '/etc/ansible/scripts/a.sh &> /tmp/test'
192.168.253.131 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.253.131 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.253.131 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

//查看受控机上的/tmp/test文件内容
[root@localhost ~]# ansible 192.168.253.131 -m shell -a 'cat /tmp/test'
192.168.253.131 | CHANGED | rc=0 >>
hello world!!!

ansible常用模块之template

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

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# ls
CentOS7-Base-163.repo  CentOS-Base.repo  CentOS-SIG-ansible-29.repo  ls.repo
[root@localhost yum.repos.d]# cd
//将设置好的yum源传到受控主机上
[root@localhost ~]# ansible 192.168.253.131 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
192.168.253.131 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
    "dest": "/etc/yum.repos.d/163.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "5a3e688854d9ceccf327b953dab55b21",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1462,
    "src": "/root/.ansible/tmp/ansible-tmp-1

你可能感兴趣的:(ansible,linux,运维)