ansible内置了丰富的模块供用户使用,但是经常使用到的模块却不多。本文主要记录了ansible的一些常用模块以及详细参数 、注意事项等 ,供大家学习。
命令模块 适合使用简单的命令 无法支持"<",">","|",";","&"等符号。
官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module
[root@pokes01 ansible]# ansible server -m command -a "hostname"
192.168.1.2 | CHANGED | rc=0 >>
pokes02
192.168.1.3 | CHANGED | rc=0 >>
pokes03
192.168.1.5 | CHANGED | rc=0 >>
pokes05
192.168.1.4 | CHANGED | rc=0 >>
pokes04
[root@m01 ~]# ansible dkaiyun -m command -a "chdir=/data ls -l"
web01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 158 Jan 12 11:11 hosts
backup01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 4 Jan 13 18:06 lol.txt
nfs01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 13 Jan 17 18:45 bbb.txt
[root@m01 ~]# ansible dkaiyun -m command -a "touch /data/lol.txt creates=/data/lol.txt"
[WARNING]: Consider using the file module with state=touch rather than running touch. If you need to use command because file is insufficient you can add
warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
nfs01 | CHANGED | rc=0 >>
backup01 | SUCCESS | rc=0 >>
skipped, since /data/lol.txt exists
web01 | CHANGED | rc=0 >>
[root@m01 ~]# ansible dkaiyun -m command -a "rm -f /data/hosts removes=/data/hosts"
nfs01 | SUCCESS | rc=0 >>
skipped, since /data/hosts does not exist
backup01 | SUCCESS | rc=0 >>
skipped, since /data/hosts does not exist
[WARNING]: Consider using the file module with state=absent rather than running rm. If you need to use command because file is insufficient you can add
warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
web01 | CHANGED | rc=0 >>
类似command模块升级版—万能模块
官方文档:https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module
[root@m01 ~]# ansible dkaiyun -m shell -a "ps -ef |grep /[s]sh"
backup01 | CHANGED | rc=0 >>
root 2042 1 0 09:06 ? 00:00:00 /usr/sbin/sshd -D
nfs01 | CHANGED | rc=0 >>
root 1258 1 0 08:32 ? 00:00:00 /usr/sbin/sshd -D
web01 | CHANGED | rc=0 >>
root 1197 1 0 11:39 ? 00:00:00 /usr/sbin/sshd -D
注:其它参数参考command模块 使用方法一致
概要
主要用于将管理主机上的数据信息传送给多台主机
官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
备注 (required)为必须使用的参数
*为默认参数
copy模块在复制数据时,如果数据为软链接文件,会将链接指定源文件进行复制
修改权限时候 需要加0 例如:chmod 0644 0755
[root@m01 ~]# ansible web01 -m copy -a "src=./anaconda-ks.cfg dest=/data"
web01 | CHANGED => {
"changed": true,
"checksum": "9d791df2961e299fac1206c2e1c6ab1cde2c86a2",
"dest": "/data/anaconda-ks.cfg",
"gid": 0,
"group": "root",
"md5sum": "221e5656c9b59aec6c7596568fff8ad3",
"mode": "0644",
"owner": "root",
"size": 1499,
"src": "/root/.ansible/tmp/ansible-tmp-1548229670.84-2879942383233/source",
"state": "file",
"uid": 0
}
[root@m01 ~]# ansible web01 -m copy -a "src=./anaconda-ks.cfg dest=/data backup=yes"
web01 | CHANGED => {
"backup_file": "/data/anaconda-ks.cfg.4263.2019-01-23@15:52:43~",
"changed": true,
"checksum": "9d791df2961e299fac1206c2e1c6ab1cde2c86a2",
"dest": "/data/anaconda-ks.cfg",
"gid": 0,
"group": "root",
"md5sum": "221e5656c9b59aec6c7596568fff8ad3",
"mode": "0644",
"owner": "root",
"size": 1499,
"src": "/root/.ansible/tmp/ansible-tmp-1548229931.86-180942706957431/source",
"state": "file",
"uid": 0
}
[root@web01 ~]# ll /data/
total 8
-rw-r--r-- 1 root root 1499 Jan 23 15:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 1505 Jan 23 15:52 anaconda-ks.cfg.4263.2019-01-23@15:52:43~
[root@m01 ~]# ansible web01 -m copy -a "src=./anaconda-ks.cfg dest=/data owner=www group=www mode=0644"
web01 | CHANGED => {
"changed": true,
"checksum": "9d791df2961e299fac1206c2e1c6ab1cde2c86a2",
"dest": "/data/anaconda-ks.cfg",
"gid": 1086,
"group": "www",
"md5sum": "221e5656c9b59aec6c7596568fff8ad3",
"mode": "0644",
"owner": "www",
"size": 1499,
"src": "/root/.ansible/tmp/ansible-tmp-1548230667.08-106764271060692/source",
"state": "file",
"uid": 1086
}
[root@web01 data]# ll
total 4
-rw-r--r-- 1 www www 1499 Jan 23 16:04 anaconda-ks.cfg
[root@m01 ~]# ansible web01 -m copy -a "content=test dest=/data/anaconda-ks.cfg "
web01 | CHANGED => {
"changed": true,
"checksum": "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
"dest": "/data/anaconda-ks.cfg",
"gid": 1086,
"group": "www",
"md5sum": "098f6bcd4621d373cade4e832627b4f6",
"mode": "0644",
"owner": "www",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1548231000.52-150895010308573/source",
"state": "file",
"uid": 1086
}
[root@web01 data]# cat anaconda-ks.cfg
test[root@web01 data]#
注:content添加内容不会添加回车符
概要
抓取文件到管理机上
官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
[root@m01 ~]# ansible web01 -m fetch -a "src=/root/lol.txt dest=/root"
web01 | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/web01/root/lol.txt",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"remote_md5sum": null
}
[root@m01 ~]# tree ~
/root
└── web01
└── root
└── lol.txt
2 directories, 1 file
[root@m01 ~]#
概要
实现创建/删除文件信息 对数据权限进行修改
官方文档:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
[root@host31 ~]# ansible host32 -m command -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 31 Jul 30 05:59 /tmp/hello.sh
[root@host31 ~]# ansible host32 -m file -a "path=/tmp/hello.sh owner=admin group=admin mode=0777"
host32 | SUCCESS => {
"changed": true,
"gid": 1000,
"group": "admin",
"mode": "0777",
"owner": "admin",
"path": "/tmp/hello.sh",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 31,
"state": "file",
"uid": 1000
}
[root@host31 ~]# ansible host32 -m command -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxrwxrwx. 1 admin admin 31 Jul 30 05:59 /tmp/hello.sh
概要
使用yum软件包管理器安装,升级,降级,删除和列出软件包和组。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html#yum-repository-module
[root@m01 ~]# ansible web01 -m yum -a "name=httpd-tools state=installed"
概要
用于管理服务运行状态
官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
[root@m01 ~]# ansible web01 -m service -a "name=crond state=started enabled=yes"
概要
用于批量管理主机进行挂载卸载操作
官方文档:https://docs.ansible.com/ansible/latest/modules/mount_module.html#mount-module
在进行挂载的时候,使用state=mounted
在进行卸载的时候,使用state=absent
[root@m01 ~]# ansible web01 -m mount -a "src=172.16.1.31:/data/ path=/mnt fstype=nfs state=present"
以上信息只是在/etc/fstab文件中添加了配置信息,不会真正进行挂载(mount -a)
[root@m01 ~]# ansible web01 -m mount -a "src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted"
以上信息是在/etc/fstab文件中添加了配置信息,并且也会真正进行挂载
概要
定时任务模块
官方文档:https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module
备注:时间参数不写时,默认为 *
每五分钟同步一次时间
[root@m01 ~]# ansible web01 -m cron -a "name='ntpdate time' minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' "
web01 | CHANGED => {
"changed": true,
"envs": [],
"jobs": [
"None",
"ntpdate time"
]
}
结果
[root@web01 data]# crontab -l
#Ansible: ntpdate time
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
删除定时任务
[root@m01 ~]# ansible web01 -m cron -a "name='ntpdate time' state=absent"
web01 | CHANGED => {
"changed": true,
"envs": [],
"jobs": []
}
注释定时任务
注意:注释和取消注释时必须填写 job和时间 参数
[root@m01 ~]# ansible web01 -m cron -a "name='ntpdate time' minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' disabled=yes"
web01 | CHANGED => {
"changed": true,
"envs": [],
"jobs": [
"ntpdate time"
]
}
结果
[root@web01 data]# crontab -l
#Ansible: ntpdate time
#*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
取消注释
[root@m01 ~]# ansible web01 -m cron -a "name='ntpdate time' minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' disabled=no"
web01 | CHANGED => {
"changed": true,
"envs": [],
"jobs": [
"ntpdate time"
]
}
结果
[root@web01 data]# crontab -l
#Ansible: ntpdate time
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
概要
远程批量创建用户组信息
官方文档:https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module
创建一个指定的用户组dkaiyun gid=1055
ansible web01 -m group -a "name=dkaiyun gid=1055"
删除一个指定的用户组dkaiyun gid=1055
ansible web01 -m group -a "dkaiyun gid=1055 state=absent"
概要
远程批量创建用户信息
官方文档:https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module
备注:password设置密码时不能使用明文方式,只能使用密文方式
可以给用户设置密码 还可以给用户修改密码
概要
官方文档:https://docs.ansible.com/ansible/latest/modules/unarchive_module.html#unarchive-module
解压ansible管理机上的压缩文件到远程主机
ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=yes"
解压远程主机上的文件到目录
ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=no"