3.1:Ansible模块简介
2015年底270多个模块,2016年达到540个,2018年01月12日有1378个模块,2018年07月15日1852个模块,2019年05月25日(ansible 2.7.10)时2080个模块,2020年03月02日有3387个模块虽然模块众多,但最常用的模块也就2,30个而已,针对特定业务只用10几个模块常用模块。
帮助文档参考:https://doc
3.2:更换默认模块
注意:默认模块为command,如果要修改则需要修改如下配置文件,例如我这里将shell
代理command
设为默认模块,修改后无需重启服务。
~]# vim /etc/ansible/ansible.cfg


3.3:Command 模块
- 功能:在远程主机执行命令,此为默认模块,可忽
原文链接:https://www.dqzboy.com 略-m选项 - 注意:此命令不支持
/ARNAME<>|;&
等,用shell模块实现
~]# ansible dbservers -m command -a 'cat /etc/redhat-release'
192.168.66.151 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)
~]# ansible dbservers -m command -a 'chdir=/etc/ cat redhat-release'
192.168.66.151 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)
- 参数说明:
3.4:Shell模块
- 功能:和command相似,用shell执行命令
- 注意1:执行命令中有变量则用单引号引起来;
- 注意2:调用bash执行命令类似
cat /etc/passwd |awk -F ':' '{print $1,$3}' &>/tmp/example.txt
这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器
~]# ansible dbservers -m shell -a 'echo ${HOSTNAME}'

~]# ansible dbservers -m shell -a 'echo hello > /root/dqz.txt'

3.5:Script模块
- 功能:在远程主机上运行ansible服务器上的脚本
[root@ansible-server ~]# cat test.sh
#!/bin/bash
echo ${HOSTNAME}
[root@ansible-server ~]# ansible webservers -m script -a '/root/test.sh'

- 注意:script模块执行过程,其实也是ansible服务端将脚本拷贝至远程客户端进行执行,执行完成后则会删除;即便在执行中我们强
文章来源(Source):浅时光博客 制ctrl + c
结束,ansible也会将远程机器上的执行脚本删除
3.6:Copy模块
- 功能:从ansible服务器主控端复制文件到远程主机
~]# ansible webservers -m copy -a "src=/root/test.sh dest=/tmp/test2.sh owner=root mode=755 backup=yes"

~]# ansible webservers -m copy -a "content='dqzboy' dest=/tmp/test.txt"#
~]# ansible webservers -a 'cat /tmp/test.txt'

- 注意:
owner=dqz
,远端主机需有这个用户;copy模块拷贝大量文件和目录很慢
~]# ansible webservers -m copy -a "src=/etc/ dest=/tmp owner=dqz mode=755"
~]# ansible webservers -m copy -a "src=/etc/redhat-release dest=/tmp/os.txt"

3.7:Fetch模块
- 功能:从远程主机提取文件至ansible的主控端,与copy相反,目前不支持目录
[root@ansible-server ~]# ansible webservers -m fetch -a 'src=/etc/redhat-release dest=/data/'



3.8:File模块
- 功能:设置文件属性I
#创建空文件
ansible webservers -m file -a 'path=/data/test.txt state=touch'
ansible webservers -m file -a 'path=/data/test.txt state=absent'
ansible webservers -m file -a "path=/root/test.sh oner=wang mode=755"
#创建目录
ansible webservers -m file -a "path=/data/mysql state=directory owner=mysql group-mysql"
#创建软链接
ansible webservers -m file -a 'src=/data/testfile dest=/data/testfile-link state=link'
3.9:Hostname模块
- 功能:管理主机名
[root@ansible-server ~]# ansible webservers -m hostname -a 'name=web-server'
#检查是否更改
[root@ansible-server ~]# ansible webservers -a 'hostname'
192.168.66.151 | CHANGED | rc=0 >>
web-server
192.168.66.152 | CHANGED | rc=0 >>
web-server
#只改主机配置清单中的某台主机的主机名
[root@ansible-server ~]# ansible 192.168.66.151 -m hostname -a 'name=ansible-01'
#检查是否更改
[root@ansible-server ~]# ansible 192.168.66.151 -a 'hostname'
192.168.66.151 | CHANGED | rc=0 >>
ansible-01
3.10:Cron模块
- 功能:计划任务
- 支持时间:minute,hour,day,month,weekday
#脚本内容如下:
~]# vim mysql_bak.sh
#!/bin/bash
mysqldump -uroot -p123456 -R -E -B test > /data/mysql_`date +%F_%T`.sql
#将脚本拷贝到远程主机的/data目录下,并添加执行权限
[root@ansible-server ~]# ansible dbservers -m copy -a 'src=/root/mysql_bak.sh dest=/data/ mode=0755'
[root@ansible-server ~]# ansible dbservers -a 'ls -l /data/mysql*'
192.168.66.151 | CHANGED | rc=0 >>
-rwxr-xr-x. 1 root root 94 11月 1 14:04 /data/mysql_bak.sh
#通过ansible创建定时任务
[root@ansible-server ~]# ansible dbservers -m cron -a 'hour=2 minute=30 weekday=1-5 name="back mysql" job=/data/mysql_bak.sh'
任务说明:
周一至周五的每天2点30分执行名称为“back mysql”的定时任务
#查看远程主机是否创建了计划任务
[root@ansible-server ~]# ansible dbservers -a 'crontab -l'
192.168.66.151 | CHANGED | rc=0 >>
#Ansible: back mysql
30 2 * * 1-5 /data/mysql_bak.sh
#禁用计划任务,临时暂停计划任务,参数disabled=yes
[root@ansible-server ~]# ansible dbservers -m cron -a 'hour=2 minute=30 weekday=1-5 name="back mysql" job=/data/mysql_bak.sh disabled=yes'
[root@ansible-server ~]# ansible dbservers -a 'crontab -l'
192.168.66.151 | CHANGED | rc=0 >>
#Ansible: back mysql
#30 2 * * 1-5 /data/mysql_bak.sh
#启动计划任务,参数disabled=no
[root@ansible-server ~]# ansible dbservers -m cron -a 'hour=2 minute=30 weekday=1-5 name="back mysql" job=/data/mysql_bak.sh disabled=no'
#删除计划任务,参数state=absent
[root@ansible-server ~]# ansible dbservers -m cron -a 'name="back mysql" state=absent'
#检查下是否删除成功
[root@ansible-server ~]# ansible dbservers -a 'crontab -l'
192.168.66.151 | CHANGED | rc=0 >>
3.11:Yum模块
- 功能:管理软件包,只支持RHEL、Cent OS、fedora,不支持Ubuntu
#查看webservers下的主机清单
[root@ansible-server ~]# ansible webservers --list
hosts (2):
192.168.66.151
192.168.66.152
#安装一个httpd服务,默认安装最新版
#使用state=present来安装,多个包用','分割;安装时state=present参数可不加
[root@ansible-server ~]# ansible webservers -m yum -a 'name=httpd'
#检查是否安装成功
[root@ansible-server ~]# ansible webservers -a 'rpm -qi httpd'
#接下来我们进行卸载操作,参数state=absent
[root@ansible-server ~]# ansible webservers -m yum -a 'name=httpd state=absent'
3.12:Service模块
- 功能:管理服务
#安装服务
[root@ansible-server ~]# ansible webservers -m yum -a 'name=httpd'
#启动服务
[root@ansible-server ~]# ansible webservers -m service -a 'name=httpd state=started'
#开机自启动,参数enabled=yes
[root@ansible-server ~]# ansible webservers -m service -a 'name=httpd state=started enabled=yes'
#检查启动状态
[root@ansible-server ~]# ansible webservers -a 'ss -tnlp|grep 80'
#停止服务
[root@ansible-server ~]# ansible webservers -m service -a 'name=httpd state=stopped'
#重启服务
[root@ansible-server ~]# ansible webservers -m service -a 'name=httpd state=restarted'
3.13:User模块
- 功能:管理用户
#创建用户
[root@ansible-server ~]# ansible webservers -m user -a 'name=dqzboy.com'
#创建用户,指定用户名,用户UID,用户家目录,用户属组,以及comment信息
[root@ansible-server ~]# ansible webservers -m user -a 'name=dqzboy comment="dqzboy.com" uid=1024 home=/homa/appuser group=root'
#检查创建的用户信息
[root@ansible-server ~]# ansible webservers -a 'id 1024'
192.168.66.152 | CHANGED | rc=0 >>
uid=1024(dqzboy) gid=0(root) 组=0(root)
192.168.66.151 | CHANGED | rc=0 >>
uid=1024(dqzboy) gid=0(root) 组=0(root)
#创建用户但不创建家目录,uid可重复,参数create_home=no
[root@ansible-server ~]# ansible webservers -m user -a 'name=nginx comment="nginx" uid=1024 group=root shell=/sbin/nologin system=yes create_home=no non_unique=yes'
- 参数说明:
shell=/sbin/nologin
:指定shell类型system=yes
:是否为系统账号create_home=no
:是否创建家目录,默认是创建家目录non_unique=yes
:用户UID是否具有唯一性,yes表示可重复
#删除用户以及家目录数据,参数remove=yes
[root@ansible-server ~]# ansible webservers -m user -a 'name=dqzboy.com state=absent remove=yes'
3.14:Group模块
- 功能:管理组
#创建组
[root@ansible-server ~]# ansible webservers -m group -a 'name=nginx'
#检查组信息
[root@ansible-server ~]# ansible webservers -a 'tail -1 /etc/group'
192.168.66.151 | CHANGED | rc=0 >>
nginx:x:1001:
192.168.66.152 | CHANGED | rc=0 >>
nginx:x:1001:
#创建组并指定GID和设置为系统组
[root@ansible-server ~]# ansible webservers -m group -a 'name=appuser gid=1005 system=yes'
#删除组
[root@ansible-server ~]# ansible webservers -m group -a 'name=nginx state=absent'
#检查是否删除
[root@ansible-server ~]# ansible webservers -a "tail /etc/group"
3.15:Lineinfile模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到寺珠符号进行替换时,存问题,无法正常进行替换。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换
- 功能:相当于sed,可以修改文件内容
#检查SELinux当前的配置,可以看到目前selinux配置状态为开启
[root@ansible-server ~]# ansible webservers -a 'cat /etc/selinux/config|grep ^SELINUX='
192.168.66.152 | CHANGED | rc=0 >>
SELINUX=enforcing
192.168.66.151 | CHANGED | rc=0 >>
SELINUX=enforcing
#通过lineinfifle模块修改SELinux的配置信息,改为disable
[root@ansible-server ~]# ansible webservers -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
#再次检查远程主机的SELinux配置信息
[root@ansible-server ~]# ansible webservers -a 'cat /etc/selinux/config|grep ^SELINUX='
192.168.66.152 | CHANGED | rc=0 >>
SELINUX=disabled
192.168.66.151 | CHANGED | rc=0 >>
SELINUX=disabled
- 删除某个文件中特定的行,比如删除/etc/fstab文件中以#号开头的行

[root@ansible-server ~]# ansible webservers -m lineinfile -a "dest=/etc/fstab state=absent regexp='^#'"

3.16:Replace模块
- 该模块有点类以于sed命令,主要也是基于正则进行匹配和替换
#给/etc/fstab文件中以UUID开头的行,添加注释
[root@ansible-server ~]# ansible webservers -m replace -a "path=/etc/fstab regexp=^(UUID.*) replace='#\1'"
- 参数说明:
\1
:表示引用前面的小括号内容

#现在在将注释的UUID信息恢复
[root@ansible-server ~]# ansible webservers -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'"

3.17:Setup模块
- 功能:setup模块来收集主机的系统信息,这些
facts
信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用gather_facts:no
来禁止Ansible收集facts信息
#查看主机的配置信息
[root@ansible-server ~]# ansible 192.168.66.151 -m setup
#过滤匹配的信息,例如查看主机清单中的主机IPV4信息
[root@ansible-server ~]# ansible webservers -m setup -a 'filter=ansible_default_ipv4'
10.15.3.41 | FAILED | rc=1 >>
cat: /etc/selinux/config|grep: 没有那个文件或目录 3.15看看呢