1.05 Ansible远程执行脚本
1 首先创建一个shell脚本 2 vim /tmp/test.sh //加入内容 3 #!/bin/bash 4 echo `date` > /tmp/ansible_test.txt 5 然后把该脚本分发到各个机器上 6 ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" 7 最后是批量执行该shell脚本 8 ansible testhost -m shell -a "/tmp/test.sh" 9 shell模块,还支持远程执行命令并且带管道 10 ansible testhost -m shell -a "cat /etc/passwd|wc -l " 11 12 13[root@Dasoncheng ~]# ansible testhost -m copy -a 'src=/tmp/test.sh dest=/tmp/test.sh mode=755' 14127.0.0.1 | SUCCESS => { 15 "changed": true, 16 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 17 "dest": "/tmp/test.sh", 18 "gid": 0, 19 "group": "root", 20 "mode": "0755", 21 "owner": "root", 22 "path": "/tmp/test.sh", 23 "size": 48, 24 "state": "file", 25 "uid": 0 26} 27192.168.60.12 | SUCCESS => { 28 "changed": true, 29 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 30 "dest": "/tmp/test.sh", 31 "gid": 0, 32 "group": "root", 33 "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 34 "mode": "0755", 35 "owner": "root", 36 "size": 48, 37 "src": "/root/.ansible/tmp/ansible-tmp-1524218004.15-84846665116766/source", 38 "state": "file", 39 "uid": 0 40} 41cdn002 | SUCCESS => { 42 "changed": false, 43 "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 44 "dest": "/tmp/test.sh", 45 "gid": 0, 46 "group": "root", 47 "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 48 "mode": "0755", 49 "owner": "root", 50 "size": 48, 51 "src": "/root/.ansible/tmp/ansible-tmp-1524218004.2-83967531119849/source", 52 "state": "file", 53 "uid": 0 54} 55[root@Dasoncheng ~]# ansible 127.0.0.1,cdn002 -m shell -a '/tmp/test.sh' 56cdn002 | SUCCESS | rc=0 >> 57127.0.0.1 | SUCCESS | rc=0 >> 58[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /tmp/ansible_test.txt' 59cdn002 | SUCCESS | rc=0 >> 60Fri Apr 20 17:54:57 CST 2018 61[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /etc/passwd | wc -l' 62cdn002 | SUCCESS | rc=0 >> 6323
1.06 ansible管理任务计划
1 ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6" 2 3 若要删除该cron 只需要加一个字段 state=absent 4 ansible testhost -m cron -a "name='test cron' state=absent" 5 6 其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month 7 8 9[root@Dasoncheng ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/1212.txt' weekday=6" 10127.0.0.1 | SUCCESS => { 11 "changed": true, 12 "envs": [], 13 "jobs": [ 14 "test_cron" 15 ] 16} 17192.168.60.12 | SUCCESS => { 18 "changed": true, 19 "envs": [], 20 "jobs": [ 21 "test_cron" 22 ] 23} 24cdn002 | SUCCESS => { 25 "changed": true, 26 "envs": [], 27 "jobs": [ 28 "test_cron" 29 ] 30} 31[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l' 32cdn002 | SUCCESS | rc=0 >> 33#Ansible: test_cron 34* * * * 6 /bin/touch /tmp/1212.txt 35 36[root@Dasoncheng ~]# ansible cdn002 -m cron -a "name='test_cron' state=absent" 37cdn002 | SUCCESS => { 38 "changed": true, 39 "envs": [], 40 "jobs": [] 41} 42[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l' 43cdn002 | SUCCESS | rc=0 >>
1.07 Ansible安装rpm包/管理服务
1 ansible testhost -m yum -a "name=httpd" 2 在name后面还可以加上state=installed/removed 3 ansible testhost -m service -a "name=httpd state=started enabled=yes" 4 这里的name是centos系统里的服务名,可以通过chkconfig --list查到。 5 Ansible文档的使用 6 ansible-doc -l 列出所有的模块 7 ansible-doc cron 查看指定模块的文档 8 9 10[root@Dasoncheng ~]# ansible cdn002 -m yum -a "name=httpd state=installed" 11cdn002 | SUCCESS => { 12 "changed": true, 13 "msg": "", 14 "rc": 0, 15 "results": 16…… 17…… 18[root@Dasoncheng ~]# ansible cdn002 -m service -a "name=httpd state=started enabled=yes" 19cdn002 | SUCCESS => { 20 "changed": true, 21 "enabled": true, 22 "name": "httpd", 23 "state": "started", 24 "status": { 25…… 26…… 27[root@Dasoncheng ~]# ansible cdn002 -m command -a 'systemctl status httpd' 28cdn002 | SUCCESS | rc=0 >> 29● httpd.service - The Apache HTTP Server 30 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) 31 Active: active (running) since Fri 2018-04-20 18:37:55 CST; 8s ago 32…… 33……
1.08 Ansible文档的使用
1[root@Dasoncheng ~]# ansible-doc -l 2a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund... 3a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund... 4a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThund... 5[root@Dasoncheng ~]# ansible-doc cron 6> CRON (/usr/lib/python2.7/site-packages/ansible/modules/system/cron.py) 7 8 Use this module to manage crontab and environment variables entries. This 9 module allows you to create environment variables and named crontab 10 entries, update, or delete them. When crontab jobs are managed: the 11 module includes one line with the description of the crontab entry 12 `"#Ansible: <name>"' corresponding to the "name" passed to the module, 13 which is used by future ansible/module calls to find/check the state. The 14 "name" parameter should be unique, and changing the "name" value will 15 result in a new cron task being created (or a different one being 16 removed). When environment variables are managed: no comment line is 17 added, but, when the module needs to find/check the state, it uses the 18 "name" parameter to find the environment variable definition line. When 19 using symbols such as %, they must be properly escaped. 20 21OPTIONS (= is mandatory): 22 23- backup 24 If set, create a backup of the crontab before it is modified. The 25 location of the backup is returned in the `backup_file' variable by this 26 module. 27 [Default: no] 28 type: bool 29 30- cron_file 31 If specified, uses this file instead of an individual user's crontab. If 32 this is a relative path, it is interpreted with respect to /etc/cron.d. 33 (If it is absolute, it will typically be /etc/crontab). Many linux 34 distros expect (and some require) the filename portion to consist solely 35 of upper- and lower-case letters, digits, underscores, and hyphens. To 36 use the `cron_file' parameter you must specify the `user' as well. 37 [Default: (null)] 38…… 39……