常用模块1.copy模块 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上[root@localhost ~]# ansible web -m copy -a 'src=/root
常用模块1.copy模块 目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上[root@localhost ~]# ansible web -m copy -a 'src=/root/ansible-master dest=/tmp/'192.168.222.129 | SUCCESS => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/ansible-master", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1501666699.41-133742932037676/source", "state": "file", "uid": 0}[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'192.168.222.129 | SUCCESS | rc=0 >>-rw-r--r-- 1 root root 0 Aug 2 17:38 /tmp/ansible-master2.file模块: 目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root[root@localhost ~]# ansible web -m file -a 'dest=/tmp/ansible-master mode=755 owner=root group=root'192.168.222.129 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/ansible-master", "size": 0, "state": "file", "uid": 0}[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'192.168.222.129 | SUCCESS | rc=0 >>-rwxr-xr-x 1 root root 0 Aug 2 17:38 /tmp/ansible-master[root@localhost ~]# ansible web -m file -a 'dest=/tmp/ansible-master mode=755 owner=test group=test'192.168.222.129 | SUCCESS => { "changed": true, "gid": 500, "group": "test", "mode": "0755", "owner": "test", "path": "/tmp/ansible-master", "size": 0, "state": "file", "uid": 500}[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'192.168.222.129 | SUCCESS | rc=0 >>-rwxr-xr-x 1 test test 0 Aug 2 17:38 /tmp/ansible-master3.cron模块 目的:在指定节点上定义一个计划任务,每隔3分钟到windows时间服务器更新一次时间[root@localhost ~]# ansible web -m cron -a 'name="custom job" minute=*/3 hour=* month=* weekday=* job="/usr/sbin/ntpdate time.windows.com"' 192.168.222.129 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "custom job" ]}[root@localhost ~]# ansible web -m command -a 'crontab -l'192.168.222.129 | SUCCESS | rc=0 >>#Ansible: custom job*/3 * * * * /usr/sbin/ntpdate time.windows.com4.group模块目的:在所有节点上创建一个组名为linux,gid为2048的组[root@localhost ~]# ansible web -m group -a 'gid=2048 name=linux' 192.168.222.129 | SUCCESS => { "changed": true, "gid": 2048, "name": "linux", "state": "present", "system": false}5.user模块目的:在指定节点上创建一个用户名为linux,组为linux的用户[root@localhost ~]# ansible web -m user -a 'name=linux group=linux state=present'192.168.222.129 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 2048, "home": "/home/linux", "name": "linux", "shell": "/bin/bash", "state": "present", "system": false, "uid": 501}[root@localhost ~]# ansible web -m command -a 'id linux'192.168.222.129 | SUCCESS | rc=0 >>uid=501(linux) gid=2048(linux) groups=2048(linux)删除用户[root@localhost ~]# ansible web -m user -a 'name=linux state=absent remove=yes'192.168.222.129 | SUCCESS => { "changed": true, "force": false, "name": "linux", "remove": true, "state": "absent"}[root@localhost ~]# ansible web -m command -a 'id linux'192.168.222.129 | FAILED | rc=1 >>id: linux: No such user6.yum模块目的:在指定节点上安装 lrzsz 服务[root@localhost ~]# ansible web -m yum -a 'state=present name=lrzsz'192.168.222.129 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "lrzsz-0.12.20-27.1.el6.x86_64 providing lrzsz is already installed" ]}7.service模块目的:启动指定节点上的 httpd服务,并让其开机自启动[root@localhost ~]# ansible web -m service -a 'name=httpd state=restarted enabled=yes'192.168.222.129 | SUCCESS => { "changed": true, "enabled": true, "name": "httpd", "state": "started"}[root@localhost ~]# ansible web -m command -a '/etc/init.d/httpd status'192.168.222.129 | SUCCESS | rc=0 >>httpd (pid 6680) is running...[root@localhost ~]# ansible web -m raw -a 'chkconfig --list|grep httpd'192.168.222.129 | SUCCESS | rc=0 >>httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off8.sctipt模块 目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)[root@localhost ~]# ansible web -m script -a '/root/a.sh'192.168.222.129 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.222.129 closed./r/n", "stdout": "salt/r/n", "stdout_lines": [ "salt" ]}9.ping模块目的:检查指定节点机器是否还能连通[root@localhost ~]# ansible web -m ping192.168.222.129 | SUCCESS => { "changed": false, "ping": "pong"}10.command模块目的:在指定节点上运行hostname命令[root@localhost ~]# ansible web -m command -a 'hostname'192.168.222.129 | SUCCESS | rc=0 >>salt注意不能使用含有|的符号11.raw模块目的:在web角色的所有节点上运行hostname命令[root@localhost ~]# ansible web -m raw -a 'hostname|tee'192.168.222.129 | SUCCESS | rc=0 >>saltShared connection to 192.168.222.129 closed.注意可以包含|符号12.get_url模块目的:将http://releases.ansible.com/ansible/ansible-latest.tar.gz文件下载到指定节点的/tmp目录下[root@localhost ~]# ansible web -m get_url -a 'url=http://releases.ansible.com/ansible/ansible-latest.tar.gz dest=/tmp/'192.168.222.129 | SUCCESS => { "changed": true, "checksum_dest": null, "checksum_src": "7c5aa4e8908a0110904f3886000172cbb81c98a4", "dest": "/tmp/ansible-latest.tar.gz", "gid": 0, "group": "root", "md5sum": "b1be8f05864a07c06b8a767dcd48ba1b", "mode": "0644", "msg": "OK (4263357 bytes)", "owner": "root", "size": 4263357, "src": "/tmp/tmpQ5UQe6", "state": "file", "status_code": 200, "uid": 0, "url": "http://releases.ansible.com/ansible/ansible-latest.tar.gz"}[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-latest.tar.gz'192.168.222.129 | SUCCESS | rc=0 >>-rw-r--r-- 1 root root 4263357 Aug 2 18:07 /tmp/ansible-latest.tar.gz13.synchronize模块目的:将主控方/root/a目录推送到指定节点的/tmp目录下[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'192.168.222.129 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", "msg": "cd+++++++++ a//n<f+++++++++ a/1.png/n<f+++++++++ a/1.txt/n<f+++++++++ a/10.png/n<f+++++++++ a/10.txt/n<f+++++++++ a/2.png/n<f+++++++++ a/2.txt/n<f+++++++++ a/3.png/n<f+++++++++ a/3.txt/n<f+++++++++ a/4.png/n<f+++++++++ a/4.txt/n<f+++++++++ a/5.png/n<f+++++++++ a/5.txt/n<f+++++++++ a/6.png/n<f+++++++++ a/6.txt/n<f+++++++++ a/7.png/n<f+++++++++ a/7.txt/n<f+++++++++ a/8.png/n<f+++++++++ a/8.txt/n<f+++++++++ a/9.png/n<f+++++++++ a/9.txt/n", "rc": 0, "stdout_lines": [ "cd+++++++++ a/", "<f+++++++++ a/1.png", "<f+++++++++ a/1.txt", "<f+++++++++ a/10.png", "<f+++++++++ a/10.txt", "<f+++++++++ a/2.png", "<f+++++++++ a/2.txt", "<f+++++++++ a/3.png", "<f+++++++++ a/3.txt", "<f+++++++++ a/4.png", "<f+++++++++ a/4.txt", "<f+++++++++ a/5.png", "<f+++++++++ a/5.txt", "<f+++++++++ a/6.png", "<f+++++++++ a/6.txt", "<f+++++++++ a/7.png", "<f+++++++++ a/7.txt", "<f+++++++++ a/8.png", "<f+++++++++ a/8.txt", "<f+++++++++ a/9.png", "<f+++++++++ a/9.txt" ]}[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/a'192.168.222.129 | SUCCESS | rc=0 >>total 0-rw-r--r-- 1 root root 0 Aug 2 18:09 10.png-rw-r--r-- 1 root root 0 Aug 2 18:09 10.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 1.png-rw-r--r-- 1 root root 0 Aug 2 18:09 1.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 2.png-rw-r--r-- 1 root root 0 Aug 2 18:09 2.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 3.png-rw-r--r-- 1 root root 0 Aug 2 18:09 3.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 4.png-rw-r--r-- 1 root root 0 Aug 2 18:09 4.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 5.png-rw-r--r-- 1 root root 0 Aug 2 18:09 5.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 6.png-rw-r--r-- 1 root root 0 Aug 2 18:09 6.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 7.png-rw-r--r-- 1 root root 0 Aug 2 18:09 7.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 8.png-rw-r--r-- 1 root root 0 Aug 2 18:09 8.txt-rw-r--r-- 1 root root 0 Aug 2 18:09 9.png-rw-r--r-- 1 root root 0 Aug 2 18:09 9.txtcompress=yes 开启压缩,默认为开启delete=yes 使两边的内容一样(即以推送方为主)mode=push 默认推送过去可以修改为pull模式从客户端拉取过来[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes delete=yes'192.168.222.129 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", "msg": ".d..t...... a//n*deleting a/9.png/n*deleting a/8.png/n*deleting a/7.png/n*deleting a/6.png/n*deleting a/5.png/n*deleting a/4.png/n*deleting a/3.png/n*deleting a/2.png/n*deleting a/10.png/n*deleting a/1.png/n", "rc": 0, "stdout_lines": [ ".d..t...... a/", "*deleting a/9.png", "*deleting a/8.png", "*deleting a/7.png", "*deleting a/6.png", "*deleting a/5.png", "*deleting a/4.png", "*deleting a/3.png", "*deleting a/2.png", "*deleting a/10.png", "*deleting a/1.png" ]}目录下的子目录也是可以同步过去的[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes delete=yes'192.168.222.129 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", "msg": ".d..t...... a//ncd+++++++++ a/b//ncd+++++++++ a/b/c//ncd+++++++++ a/b/c/d//n", "rc": 0, "stdout_lines": [ ".d..t...... a/", "cd+++++++++ a/b/", "cd+++++++++ a/b/c/", "cd+++++++++ a/b/c/d/" ]}从客户端拉取到主控端[root@localhost ~]# ansible web -m synchronize -a 'src=/tmp/a dest=/tmp/ compress=yes delete=yes mode=pull'192.168.222.129 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L 192.168.222.129:/tmp/a /tmp/", "msg": "cd+++++++++ a//n>f+++++++++ a/1.txt/n>f+++++++++ a/10.txt/n>f+++++++++ a/2.txt/n>f+++++++++ a/3.txt/n>f+++++++++ a/4.txt/n>f+++++++++ a/5.txt/n>f+++++++++ a/6.txt/n>f+++++++++ a/7.txt/n>f+++++++++ a/8.txt/n>f+++++++++ a/9.txt/ncd+++++++++ a/b//ncd+++++++++ a/b/c//ncd+++++++++ a/b/c/d//n", "rc": 0, "stdout_lines": [ "cd+++++++++ a/", ">f+++++++++ a/1.txt", ">f+++++++++ a/10.txt", ">f+++++++++ a/2.txt", ">f+++++++++ a/3.txt", ">f+++++++++ a/4.txt", ">f+++++++++ a/5.txt", ">f+++++++++ a/6.txt", ">f+++++++++ a/7.txt", ">f+++++++++ a/8.txt", ">f+++++++++ a/9.txt", "cd+++++++++ a/b/", "cd+++++++++ a/b/c/", "cd+++++++++ a/b/c/d/" ]}