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)]
……
……