IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Ansible有哪些常用模块

Ansible有哪些常用模块 详细介绍使用方法!(3)

时间:2023-09-17 20:35:04 来源:IT袋 作者:苏晓敏
导读:Ansible有哪些常用模块,3.5 fetch 模块 从远程主机获取文件到ansible管理节点,但是不支持目录操作 [root@bunian ansible-example]# ansible dbsrvs -m fetch -a "src=/etc/yum.repos.d/epel.repo dest=/usr/loc

Ansible有哪些常用模块

3.5 fetch 模块

从远程主机获取文件到ansible管理节点,但是不支持目录操作

[root@bunian ansible-example]# ansible dbsrvs -m fetch -a "src=/etc/yum.repos.d/epel.repo dest=/usr/local/src"
10.10.108.30 | CHANGED => {
    "changed": true,
    "checksum": "2feedd589b72617f03d75c4b8a6e328cc1aad918",
    "dest": "/usr/local/src/10.10.108.30/etc/yum.repos.d/epel.repo",
    "md5sum": "bddf35db56cf6be9190fdabeae71c801",
    "remote_checksum": "2feedd589b72617f03d75c4b8a6e328cc1aad918",
    "remote_md5sum": null
}
[root@bunian ansible-example]# ls -al /usr/local/src/10.10.108.30/etc/yum.repos.d/
total 4
drwxr-xr-x. 2 root root  23 Aug 11 15:05 .
drwxr-xr-x. 3 root root  25 Aug 11 15:05 ..
-rw-r--r--. 1 root root 664 Aug 11 15:05 epel.repo

3.6 file 模块

# 创建软连接
[root@bunian ansible-example]# ansible test -m file -a 'src=/etc/passwd path=/tmp/passwd.link state=link'
# 查看刚创建的/tmp下的软连接
[root@bunian ansible-example]# ansible all -m shell -a 'ls -l /tmp/passwd.link'
# 创建文件。如果文件已经存在,则会更新文件的时间戳
[root@bunian ansible-example]# ansible all -m file -a 'name=d.txt state=touch'
# 删除文件
[root@bunian ansible-example]# ansible test -m file -a 'path=/tmp/cc.txt state=absent'
# 创建目录(可以递归创建,直接加上文件名即可)
# 如果state=directory,那么如果目录不存在,那么所有的子目录将被创建(而且提供权限的创建),如果目录# 已经存在,则不进行任何操作。如果state=file,文件将不会被创建
[root@bunian ansible-example]# ansible test -m file -a 'path=/tmp/bj state=directory'
# 删除目录(可以递归删除,无需任何参数,直接加上)
[root@bunian ansible-example]# ansible test -m file -a 'path=/tmp/bj state=absent'
# 修改文件权限等属性
[root@bunian ansible-example]# ansible test -m file -a 'path=/tmp/bb.txt mode=700 owner=root group=root'
# 递归授权目录权限 
ansible dbsrvs -m file -a "path=/data owner=bgx group=bgx recurse=yes"

3.7 hostname 模块

管理远程主机上的主机名

# 查看主机名
[root@bunian ansible-example]# ansible test -m shell -a 'hostname'
# 更改主机名
[root@bunian ansible-example]# ansible test -m hostname -a 'name=master'

3.8 yum 模块

# 安装一个httpd服务,默认安装最新版
# 使用state=present来安装,多个包用','分割
[root@ansible-server ~]# ansible dbsrvs -m yum -a 'name=httpd'
[root@ayunw ansible-example]# ansible test -m yum -a 'name=httpd state=present'
# 检查是否安装成功
[root@ansible-server ~]# ansible dbsrvs -a 'rpm -qi httpd'

3.9 cron 模块

# 创建计划任务
[root@bunian ansible-example]# ansible test -m cron -a 'minute=*/5 name=Ajob job="/usr/sbin/ntpdate 172.16.8.100 &> /dev/null" state=present'
[root@bunian ansible-example]# ansible dbsrvs -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh test.sh'"
[root@bunian ansible-example]# ansible dbsrvs -m cron -a "job='/bin/sh /server/scripts/test.sh'"
# 设置定时任务注释信息,防止重复,name设定
ansible dbsrvs -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
# 注释相应定时任务,使定时任务失效
ansible dbsrvs -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh test.sh' disabled=yes"
# 删除相应定时任务(怎么创建的就要怎么删除)
[root@bunian ansible-example]# ansible test -m cron -a 'minute=*/5 name=Ajob job="/usr/sbin/ntpdate 172.16.8.100 &> /dev/null state=absent"'
# 查看计划任务
[root@bunian ansible-example]# ansible test -m shell -a "crontab -l"
172.16.20.115 | SUCCESS | rc=0 >>
#Ansible: Ajob
*/5 * * * * /usr/sbin/ntpdate 172.16.8.100 &> /dev/null 
# 删除任务计划
[root@bunian ansible-example]# ansible test -m shell -a "crontab -r"

相关阅读