1.05 Ansible遠程執行腳本
首先建立一個shell腳本
vim /tmp/test.sh //加入內容
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
而後把該腳本分發到各個機器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最後是批量執行該shell腳本
ansible testhost -m shell -a "/tmp/test.sh"
shell模塊,還支持遠程執行命令而且帶管道
ansible testhost -m shell -a "cat /etc/passwd|wc -l "
[root@Dasoncheng ~]# ansible testhost -m copy -a 'src=/tmp/test.sh dest=/tmp/test.sh mode=755'
127.0.0.1 | SUCCESS => {
"changed": true,
"checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test.sh",
"size": 48,
"state": "file",
"uid": 0
}
192.168.60.12 | SUCCESS => {
"changed": true,
"checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "edfaa4371316af8c5ba354e708fe8a97",
"mode": "0755",
"owner": "root",
"size": 48,
"src": "/root/.ansible/tmp/ansible-tmp-1524218004.15-84846665116766/source",
"state": "file",
"uid": 0
}
cdn002 | SUCCESS => {
"changed": false,
"checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "edfaa4371316af8c5ba354e708fe8a97",
"mode": "0755",
"owner": "root",
"size": 48,
"src": "/root/.ansible/tmp/ansible-tmp-1524218004.2-83967531119849/source",
"state": "file",
"uid": 0
}
[root@Dasoncheng ~]# ansible 127.0.0.1,cdn002 -m shell -a '/tmp/test.sh'
cdn002 | SUCCESS | rc=0 >>
127.0.0.1 | SUCCESS | rc=0 >>
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /tmp/ansible_test.txt'
cdn002 | SUCCESS | rc=0 >>
Fri Apr 20 17:54:57 CST 2018
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /etc/passwd | wc -l'
cdn002 | SUCCESS | rc=0 >>
23
1.06 ansible管理任務計劃
ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
若要刪除該cron 只須要加一個字段 state=absent
ansible testhost -m cron -a "name='test cron' state=absent"
其餘的時間表示:分鐘 minute 小時 hour 日期 day 月份 month
[root@Dasoncheng ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/1212.txt' weekday=6"
127.0.0.1 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test_cron"
]
}
192.168.60.12 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test_cron"
]
}
cdn002 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test_cron"
]
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>
#Ansible: test_cron
* * * * 6 /bin/touch /tmp/1212.txt
[root@Dasoncheng ~]# ansible cdn002 -m cron -a "name='test_cron' state=absent"
cdn002 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>
1.07 Ansible安裝rpm包/管理服務
ansible testhost -m yum -a "name=httpd"
在name後面還能夠加上state=installed/removed
ansible testhost -m service -a "name=httpd state=started enabled=yes"
這裏的name是centos系統裏的服務名,能夠經過chkconfig --list查到。
Ansible文檔的使用
ansible-doc -l 列出全部的模塊
ansible-doc cron 查看指定模塊的文檔
[root@Dasoncheng ~]# ansible cdn002 -m yum -a "name=httpd state=installed"
cdn002 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results":
……
……
[root@Dasoncheng ~]# ansible cdn002 -m service -a "name=httpd state=started enabled=yes"
cdn002 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
……
……
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'systemctl status httpd'
cdn002 | SUCCESS | rc=0 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-04-20 18:37:55 CST; 8s ago
……
……
1.08 Ansible文檔的使用
[root@Dasoncheng ~]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThund...
[root@Dasoncheng ~]# ansible-doc cron
> CRON (/usr/lib/python2.7/site-packages/ansible/modules/system/cron.py)
Use this module to manage crontab and environment variables entries. This
module allows you to create environment variables and named crontab
entries, update, or delete them. When crontab jobs are managed: the
module includes one line with the description of the crontab entry
`"#Ansible: <name>"' corresponding to the "name" passed to the module,
which is used by future ansible/module calls to find/check the state. The
"name" parameter should be unique, and changing the "name" value will
result in a new cron task being created (or a different one being
removed). When environment variables are managed: no comment line is
added, but, when the module needs to find/check the state, it uses the
"name" parameter to find the environment variable definition line. When
using symbols such as %, they must be properly escaped.
OPTIONS (= is mandatory):
- backup
If set, create a backup of the crontab before it is modified. The
location of the backup is returned in the `backup_file' variable by this
module.
[Default: no]
type: bool
- cron_file
If specified, uses this file instead of an individual user's crontab. If
this is a relative path, it is interpreted with respect to /etc/cron.d.
(If it is absolute, it will typically be /etc/crontab). Many linux
distros expect (and some require) the filename portion to consist solely
of upper- and lower-case letters, digits, underscores, and hyphens. To
use the `cron_file' parameter you must specify the `user' as well.
[Default: (null)]
……
……