ansible常用模块详解

1.模块介绍
明确一点:模块的执行就类似是linux命令的一条命令,就单单的是为了执行一条语句,不是批量的操作,批量操作需要用到playbook内类似shell编写脚本进行批量。
1.1 模块的使用方法
查看ansible支持的所有模块:ansible-doc -l 然后在 ansible-doc -l | grep [ module_name ] 找到想用的模块名字 ;再使用ansible-doc -s [module_name]来查看此模块的用法,也可以使用ansible-doc help module来查看该模块更详细的信息。下面给出

官方模块列表和说明:https://docs.ansible.com/ansible/latest/modules_by_category.html

1.2 模块注意事项
关于模块的使用方法,需要注意的是"state"。很多模块都会有该选项,且其值几乎都包含有"present"和"absent",表示肯定和否定的意思,

另ansible的多数的模块都是具有幂等性的,只有极少数模块如shell和command模块不具备幂等性。所谓的幂等性是指多次执行同一个操作不会影响最终结果,ansible具有幂等性的模块在执行时,都会自动判断是否要执行。

例如: file模块创建目录时,如果创建过的目录已经创建过了,则再次或多次执行安装操作都不会真正的执行下去。

再例如:copy模块拷贝文件时,如果目标主机上已经有了完全相同的文件,则多次执行copy模块不会真正的拷贝。

2.常用模块
2.1,ping模块:测试节点连通性示例如下
ansible常用模块详解
2.2 ,hostname模块,用于更改节点主机名称示例如下
ansible常用模块详解
注意:

(1)Host模块不会修改/etc/hosts文件中的主机名解析,注意修改

(2)批量修改主机名时最好加变量,防止所有主机名一致
2.3 .2.4 copy复制模块
功能:复制文件到远端主机,这里插张图
ansible常用模块详解
将本地的nginx文件 复制到远端的节点根目录下,如果文件存在就优先备份。示例如下
ansible常用模块详解
验证是否成功
ansible常用模块详解
content 简单生成文件内容并验证:
ansible常用模块详解
2.4 File文件模块
功能:创建目录,管理目录和文件属性。
ansible常用模块详解
注意:

file模块可以递归创建目录,但是不能在不存在的目录中创建文件,只能先创建目录,再在此目录中创建文件。
示例如下
创建目录
ansible常用模块详解
创建文件
ansible常用模块详解
删除文件
ansible常用模块详解
删除目录:
12 ansible all -m file -a ‘name=/data/dir1 state=absent’
13 创建软连接
14 ansible all -m file -a ‘src=/etc/fstab dest=/data/fstab.link state=link’
15 ansible all -m file -a ‘dest=/data/fstab.link state=absent’
16 创建文件指定所有者,权限:
17 ansible all -m file -a ‘path=/root/a.sh owner=liych mode=755’
18 ansible all -m file -a ‘src=/data/testfile dest=/data/testfile-link state=link’
2.5 Fetch 拉取文件模块
功能:拉取远端文件到本地控制端,和copy工作模式相反。
ansible常用模块详解
从节点上拉去1.txt
ansible常用模块详解
yum安装vsftpd包:(默认state=installd)
列出和ansible相关的包。
ansible all -m yum -a “list=ansible”
ansible all -m yum -a ‘name=nginx’
安装多个包用逗号隔开:
ansible all -m yum -a ‘name=nginx,vsftpd’
显示所有已安装的包:
ansible all -m yum -a ‘name=vsftpd list=installd’
卸载vsftpd包:
ansible all -m yum -a ‘name=nvsftpd state=removed’
安装从互联网下载的包:
ansible all -m yum -a ‘name=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm state=present’
安装本地的包,且排除某些包不安装。
ansible all -m yum -a “name=/tmp/.rpm exclude=unix state=present"
更新缓存:
ansible all -m yum -a ‘update_cache=yes’
更新缓存同时安装dstat包
ansible all -m yum -a ‘name=dstat update_cache=yes’
name=[Package name] 需要和state使用。状态。(‘present’、‘installed’,‘latest’)用于安装包,(‘absent’、‘removed’)用于移除已安装包。在命令介绍的时候就说过,state的状态在很多模块中都会用到,需要特别注意。
service模块
功能:管理服务
ansible常用模块详解
停止httpd服务:
ansible all -m service -a ‘name=httpd state=stopped’
开启httpd服务:
ansible all -m service -a ‘name=httpd state=started’
重新加载httod服务:
ansible all -m service -a ‘name=httpd state=reloaded’
重启httpd服务:
ansible all -m service -a ‘name=httpd state=restarted’
开启ftp服务,同时设置开机自动启动:
ansible all -m service -a ‘name=vsftpd state=started enabled=yes’
重启ftp服务:
ansible all -m service -a ‘name=vsftpd state=restarted’
cron 定时任务
功能:设置管理定时任务。
ansible常用模块详解
ansible常用模块详解
创建计划任务:每周1,3,5,每分钟打印warning,任务名称:test
ansible all -m cron -a 'minute=
weekday=1,3,5 job=”/usr/bin/wall warning" name=test’
注释禁用cronname=test的计划任务:也可以用yes/no
ansible all -m cron -a ‘disabled=true job="/usr/bin/wall warning" name=test’
给cronname=test的计划任务去掉注释:也可以用yes/no
ansible all -m cron -a ‘disabled=false job="/usr/bin/wall warning" name=test’
删除计划任务:
ansible all -m cron -a ‘job="/usr/bin/wall warning" name=test state=absent’
查看计划任务:
ansible all -m shell -a ‘crontab -l’
执行过程:

[email protected] ~]# ansible all -m cron -a ‘minute=* weekday=1,3,5 job="/usr/bin/wall warning" name=test’ -o
192.168.188.136 | SUCCESS => {“changed”: true, “envs”: [], “jobs”: [“test”]}
[[email protected] ~]# ansible all -m shell -a ‘crontab -l’
192.168.188.136 | SUCCESS | rc=0 >>
#Ansible: test

        • 1,3,5 /usr/bin/wall warning

[[email protected] ~]# ansible all -m cron -a ‘disabled=true job="/usr/bin/wall warning" name=test’
192.168.188.136 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“test”
]
}
[[email protected] ~]# ansible all -m shell -a ‘crontab -l’
192.168.188.136 | SUCCESS | rc=0 >>
#Ansible: test
#* * * * * /usr/bin/wall warning

[[email protected] ~]# ansible all -m cron -a ‘disabled=false job="/usr/bin/wall warning" name=test’
192.168.188.136 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“test”
]
}
[[email protected] ~]# ansible all -m shell -a ‘crontab -l’
192.168.188.136 | SUCCESS | rc=0 >>
#Ansible: test

          • /usr/bin/wall warning

[[email protected] ~]# ansible all -m cron -a ‘job="/usr/bin/wall warning" name=test state=absent’
192.168.188.136 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: []
}

[[email protected] ~]# ansible all -m shell -a ‘crontab -l’
192.168.188.136 | SUCCESS | rc=0 >>
1.2 自定义的定时任务不能使用crontab-l查看
创建一个job,每5分钟进行一次时间同步,并且自定义cron_file。
ansible all -m cron -a ‘name=“ntpdate_time” job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron user=root minute=/5’ -o
移除一个job,要求name必须匹配,需要同时指定cron_file和user。
ansible all -m cron -a ‘name=“ntpdate_time” state=absent cron_file=ntpdate_cron user=root’ -o
#查看定时任务
ansible all -m shell -a ‘cat /etc/cron.d/ntpdate_cron’
执行过程:
[[email protected] ~]# ansible all -m cron -a 'name=“ntpdate_time” job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron user=root minute=
/5’ -o
192.168.188.136 | SUCCESS => {“changed”: true, “cron_file”: “ntpdate_cron”, “envs”: [], “jobs”: [“ntpdate_time”]}
[[email protected] ~]# ansible all -m shell -a ‘cat /etc/cron.d/ntpdate_cron’
192.168.188.136 | SUCCESS | rc=0 >>
#Ansible: ntpdate_time
*/5 * * * * root /usr/sbin/ntpdate ntp1.aliyun.com
[[email protected] ~]# ansible all -m cron -a ‘name=“ntpdate_time” state=absent cron_file=ntpdate_cron user=root’ -o
192.168.188.136 | SUCCESS => {“changed”: true, “cron_file”: “ntpdate_cron”, “envs”: [], “jobs”: []}
[[email protected] ~]# ansible all -m shell -a ‘cat /etc/cron.d/ntpdate_cron’
192.168.188.136 | SUCCESS | rc=0 >>
参考连接:
https://www.cnblogs.com/liych/p/11526937.html