安装
yum -y install ansible
配置文件/etc/ansible/hosts
模块介绍与使用
ping模块
1 [root@node1 config]# ansible k8s -m ping 2 192.168.1.3 | SUCCESS => { 3 "changed": false, 4 "ping": "pong" 5 } 6 192.168.1.2 | SUCCESS => { 7 "changed": false, 8 "ping": "pong" 9 } 10 192.168.1.1 | SUCCESS => { 11 "changed": false, 12 "ping": "pong" 13 } 14 [root@node1 config]#
command模块和shell
shell和command的区别:shell模块可以特殊字符,而command是不支持
1 [root@node1 config]# ansible k8s -m command -a 'date' 2 192.168.1.1 | SUCCESS | rc=0 >> 3 2018年 01月 21日 星期日 17:09:42 CST 4 5 192.168.1.3 | SUCCESS | rc=0 >> 6 2018年 01月 21日 星期日 17:09:42 CST 7 8 192.168.1.2 | SUCCESS | rc=0 >> 9 2018年 01月 21日 星期日 17:09:42 CST
command万能模块也可以这样写
1 [root@node1 config]# ansible k8s -a 'date' 2 192.168.1.1 | SUCCESS | rc=0 >> 3 2018年 01月 21日 星期日 17:10:00 CST 4 5 192.168.1.3 | SUCCESS | rc=0 >> 6 2018年 01月 21日 星期日 17:10:00 CST 7 8 192.168.1.2 | SUCCESS | rc=0 >> 9 2018年 01月 21日 星期日 17:10:00 CST 10 11 [root@node1 config]#
shell模块支持特殊字符比如空格
1 [root@node1 config]# ansible k8s -m shell -a 'touch /tmp/ansible.txt' 2 3 192.168.1.1 | SUCCESS | rc=0 >> 4 5 6 192.168.1.3 | SUCCESS | rc=0 >> 7 8 9 192.168.1.2 | SUCCESS | rc=0 >> 10 11 12 [root@node1 config]# ansible k8s -m shell -a 'ls -l /tmp/ansible.txt' 13 192.168.1.1 | SUCCESS | rc=0 >> 14 -rw-r--r-- 1 root root 0 1月 21 17:12 /tmp/ansible.txt 15 16 192.168.1.3 | SUCCESS | rc=0 >> 17 -rw-r--r-- 1 root root 0 1月 21 17:12 /tmp/ansible.txt 18 19 192.168.1.2 | SUCCESS | rc=0 >> 20 -rw-r--r-- 1 root root 0 1月 21 17:12 /tmp/ansible.txt 21 22 [root@node1 config]#
copy模块
把本地的文件拷贝到远程机器上去
1 [root@node1 tmp]# ansible k8s -m copy -a 'src=/tmp/ahahaa dest=/tmp/' 2 192.168.1.3 | SUCCESS => { 3 "changed": true, 4 "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 5 "dest": "/tmp/ahahaa", 6 "gid": 0, 7 "group": "root", 8 "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 9 "mode": "0644", 10 "owner": "root", 11 "size": 0, 12 "src": "/root/.ansible/tmp/ansible-tmp-1516526258.65-233570741395818/source", 13 "state": "file", 14 "uid": 0 15 } 16 192.168.1.2 | SUCCESS => { 17 "changed": true, 18 "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 19 "dest": "/tmp/ahahaa", 20 "gid": 0, 21 "group": "root", 22 "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 23 "mode": "0644", 24 "owner": "root", 25 "size": 0, 26 "src": "/root/.ansible/tmp/ansible-tmp-1516526258.64-160953882755803/source", 27 "state": "file", 28 "uid": 0 29 } 30 192.168.1.1 | SUCCESS => { 31 "changed": false, 32 "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 33 "gid": 0, 34 "group": "root", 35 "mode": "0644", 36 "owner": "root", 37 "path": "/tmp/ahahaa", 38 "size": 0, 39 "state": "file", 40 "uid": 0 41 } 42 [root@node1 tmp]#
src本地文件路径
dest远程文件路径
script模块
在远程服务器上执行本地脚本
1 [root@node1 tmp]# ansible k8s -m script -a '/tmp/sh.sh' 2 192.168.1.2 | SUCCESS => { 3 "changed": true, 4 "rc": 0, 5 "stderr": "Shared connection to 192.168.1.2 closed.\r\n", 6 "stdout": "caojiaoyue\r\n", 7 "stdout_lines": [ 8 "caojiaoyue" 9 ] 10 } 11 192.168.1.3 | SUCCESS => { 12 "changed": true, 13 "rc": 0, 14 "stderr": "Shared connection to 192.168.1.3 closed.\r\n", 15 "stdout": "caojiaoyue\r\n", 16 "stdout_lines": [ 17 "caojiaoyue" 18 ] 19 } 20 192.168.1.1 | SUCCESS => { 21 "changed": true, 22 "rc": 0, 23 "stderr": "Shared connection to 192.168.1.1 closed.\r\n", 24 "stdout": "caojiaoyue\r\n", 25 "stdout_lines": [ 26 "caojiaoyue" 27 ] 28 } 29 [root@node1 tmp]#
参考文档http://blog.51cto.com/liuzhengwei521/1895480