本文主要是介绍Ansible02-Ansible Modules模块详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 写在前面
- 4. Ansible Modules 模块
- 4.1 Ansible常用模块
- 4.1.1 Command模块
- 4.1.2 shell模块
- 4.1.3 scrpit模块
- 4.1.4 file模块
- 4.1.5 copy模块
- 4.1.6 lineinfile模块
- 4.1.7 systemd模块
- 4.1.8 yum模块
- 4.1.9 get_url模块
- 4.1.10 yum_repository模块
- 4.1.11 user模块
- 4.1.12 group模块
- 4.1.13 mount模块
- 4.1.14 cron模块
- 4.1.15 setup模块
- 4.1.16 synchronize模块
- 4.1.x 官网
写在前面
这是Ansible系列第二篇,内容为Ansible Modules的介绍、使用
序号连续上篇笔记 Ansible01-Ansible的概述、实验环境初始化、Inventory
回顾一下Ansible架构:
4. Ansible Modules 模块
Modules,类似于Linux中的命令,如:yum模块->yum命令;file模块;user模块
- Ansible格式:
ansible -i 指定清单 -m 指定模块 -a 指定模块中的选项
- 查看ansible所有模块:
ansible-doc -l
- 查看ansible某模块的参数选项:
ansible-doc -s 模块名# -s, --snippet Show playbook snippet for these plugin types: inventory, lookup,module
4.1 Ansible常用模块
模块类型 | 模块名 | 模块功能 |
---|---|---|
命令和脚本模块 | command模块 | 默认的模块,执行简单命令,不支持特殊符号 |
shell模块 | 类似command模块,但支持特殊符号 | |
script模块 | 分发脚本并执行 | |
文件 | file | 创建目录,文件,软连接 |
copy | 远程分发文件,修改权限,所有者,备份 | |
服务 | systemd | 服务管理 |
service | 服务管理,centos7之前使用 | |
软件包 | yum_repository | yum源 |
yum | yum命令 | |
get_url | 下载软件 | |
系统管理 | mount | 挂载 |
cron | 定时任务 | |
用户管理 | group | 管理用户组 |
user | 管理用户 | |
其他 | ping | 检查 ansible与其他节点连通性 |
debug | 用于检查/显示 变量 | |
docker/k8s/zabbix/grafana | 管理docker/k8s/zabbix/grafana | |
unarchive/rsync/mysql_db/mysql_user | 压缩解压缩/同步数据/数据库模块 | |
… | … |
4.1.1 Command模块
默认的模块,执行简单命令,不支持特殊符号
ansible -s command
:
选项 | 描述 |
---|---|
argv | 将命令作为列表而非字符串传递。使用argv可避免对原本会被误解的值(例如用户名中带空格的情况)进行引用。只能提供字符串或列表形式之一,不能同时提供。 |
chdir | 在执行命令前切换到此目录。 |
cmd | 要执行的命令。 |
creates | 如果该文件已经存在,此步骤不会执行。 |
removes | 如果该文件存在,此步骤将会执行。 |
stdin | 直接将指定的值设置为命令的stdin。 |
stdin_add_newline | 如果设置为yes,在stdin数据末尾追加换行符。 |
strip_empty_ends | 从stdout/stderr结果的结尾去除空行。 |
warn | 启用或禁用任务警告。 |
[root@manager ~]# ansible all -m command -a 'ip a s ens33'
192.168.100.149 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:e1:55:bb brd ff:ff:ff:ff:ff:ffaltname enp2s1inet 192.168.100.149/24 brd 192.168.100.255 scope global dynamic noprefixroute ens33valid_lft 1737sec preferred_lft 1737secinet6 fe80::20c:29ff:fee1:55bb/64 scope linkvalid_lft forever preferred_lft forever#切换到指定目录
[root@manager ~]# ansible front -m command -a 'chdir=/root ls'
192.168.100.148 | CHANGED | rc=0 >>
anaconda-ks.cfg
test1.log
4.1.2 shell模块
类似command模块,不过支持特殊符号
[root@manager ~]# ansible all -m shell -a "ip a s ens33 | awk -F'[/ ]+' 'NR==4{print \$3}'"
192.168.100.150 | CHANGED | rc=0 >>
192.168.100.150
192.168.100.148 | CHANGED | rc=0 >>
192.168.100.148
192.168.100.149 | CHANGED | rc=0 >>
192.168.100.149
4.1.3 scrpit模块
用于执行脚本的模块
#编写shell脚本
[root@manager ~]# cat sys_info.sh
#!/bin/bash
#author: tassel
#desc: ansible script modules test shellscript
hostname
hostname -I
ip a s ens33 |awk -F'[ /]+' 'NR==4{print $3}'
uptime
whoami
date +%F
sleep 10 #用于可以ps -ef和pstree查看ansible状态#测试
[root@manager ~]# ansible all -m script -a '/root/sys_info.sh'
192.168.100.149 | CHANGED => {"changed": true,"rc": 0,"stderr": "Shared connection to 192.168.100.149 closed.\r\n","stderr_lines": ["Shared connection to 192.168.100.149 closed."],"stdout": "backup\r\n192.168.100.149 \r\n192.168.100.149\r\n 20:05:14 up 24 min, 1 user, load average: 0.68, 0.58, 0.33\r\nroot\r\n2024-05-26\r\n","stdout_lines": ["backup","192.168.100.149 ","192.168.100.149"," 20:05:14 up 24 min, 1 user, load average: 0.68, 0.58, 0.33","root","2024-05-26"]
}#ps和pstree查看
ps -ef | grep ansible
root 2679 1728 55 20:07 pts/0 00:00:01 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2682 2679 5 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2683 2679 4 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2684 2679 4 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
## 不难发现,使用python运行
#pstree 也可以看到,是ssh执行脚本
─sshd(921)─┬─sshd(1721)───sshd(1726)───bash(1728)│ ├─sshd(1725)───sshd(1758)───sftp-server(1759)│ ├─sshd(2526)───sshd(2531)───bash(2533)───pstree(2674)│ └─sshd(2530)───sshd(2561)───sftp-server(2564)
4.1.4 file模块
file模块管理文件、目录、软连接。
相当于结合了touch、mkdir、rm、ln-s
- 常用参数介绍:
参数 | 解释 |
---|---|
src | Ansible端源文件或者目录,一般用于软连接; |
follow | 支持link文件拷贝; |
force | 覆盖远程主机不一致的内容; |
group | 设定远程主机文件夹的组名; |
mode | 指定远程主机文件及文件夹的权限; |
owner | 设定远程主机文件夹的用户名; |
path(必写) | 目标路径,也可以用dest,name代替; |
state | 状态包括:file(默认)更新文件,不存在不创建、link创建软连接、directory创建目录、hard、touch创建文件、absent递归删除; |
attributes | 文件或者目录特殊属性。 |
# 创建文件
[root@manager ~]# ansible front -m file -a 'path=/tmp/0526.txt state=touch'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"dest": "/tmp/0526.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0
}[root@front ~]# ls /tmp
0526.txt ######递归创建目录
[root@manager ~]# ansible front -m file -a 'path=/tmp/a/b/c/d/e state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/tmp/a/b/c/d/e","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0
}[root@front ~]# tree -F /tmp
/tmp
├── 0526.txt
├── a/
│ └── b/
│ └── c/
│ └── d/
│ └── e/######创建软连接
[root@manager ~]# ansible front -m file -a 'path=/tmp/hosts_sl src=/etc/hosts state=link'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"dest": "/tmp/hosts_sl","gid": 0,"group": "root","mode": "0777","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 10,"src": "/etc/hosts","state": "link","uid": 0
}[root@manager ~]# ansible front -m shell -a 'ls -l /tmp/hosts*'
192.168.100.148 | CHANGED | rc=0 >>
lrwxrwxrwx. 1 root root 10 May 26 20:40 /tmp/hosts_sl -> /etc/hosts######修改权限
[root@manager ~]# ansible front -m file -a 'path=/tmp/ansible-test mode=700 owner=nobody group=nobody state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 65534,"group": "nobody","mode": "0700","owner": "nobody","path": "/tmp/ansible-test","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 65534
}[root@manager ~]# ansible front -m shell -a 'ls -ld /tmp/ansible-test' 192.168.100.148 | CHANGED | rc=0 >>
drwx------. 2 nobody nobody 6 May 26 20:44 /tmp/ansible-test###### 删除目录(递归,若删除高层目录,其子目录也被删除)
[root@manager ~]# ansible front -m file -a 'path=/tmp/a/b/c/ state=absent'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"path": "/tmp/a/b/c/","state": "absent"
}[root@manager ~]# ansible front -m shell -a 'tree -F /tmp'
192.168.100.148 | CHANGED | rc=0 >>
/tmp
├── 0526.txt
├── a/
│ └── b/
4.1.5 copy模块
批量分发,类似于scp:1个节点(管理节点)发送文件或压缩包到所有被管理端.
注意:copy是单向的传输。
还有一个fetch 批量拉取
选项 | 解释 |
---|---|
src | source 源文件,管理端的某个文件. |
dest | destination 目标,被管理端的目录/文件. |
backup | backup=yes 则会在覆盖前进行备份,文件内容要有变化或区别. |
mode | 修改权限 |
owner | 修改为指定所有者 |
group | 修改为指定用户组 |
# 传输/etc/hosts到所有主机组的主机
[root@manager ~]# ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "/etc/hosts.4177.2024-05-26@20:53:57~","changed": true,"checksum": "54e822ead7405808873d7efeb6ae02f1b58b1962","dest": "/etc/hosts","gid": 0,"group": "root","md5sum": "a608fd387bb46d36a21adb75b01490c0","mode": "0644","owner": "root","secontext": "system_u:object_r:net_conf_t:s0","size": 183,"src": "/root/.ansible/tmp/ansible-tmp-1716728034.4922798-3386-265280371596300/source","state": "file","uid": 0
}[root@manager ~]# ansible front -m shell -a 'ls /etc/hosts*'
192.168.100.148 | CHANGED | rc=0 >>
/etc/hosts
/etc/hosts.7761.2024-05-26@20:53:57~
4.1.6 lineinfile模块
修改配置文件使用,类似sed 's///g’和sed ‘cai’
4.1.7 systemd模块
systemd模块类似systemctl命令
service模块,是centos7之前的service命令
systemd模块 | 说明 |
---|---|
name(必须写) | 用于指定服务名称 |
enabled | yes开机自启动 (yes/no) |
state | 表示服务开,关,重启 started 开启、state=stopped 关闭、state=reloaded 重读配置文件(服务支持)、state=restarted 重启(关闭再开启) |
daemon-reload | yes是否重新加载对应的服务的管理配置文件(书写systemctl配置文件) |
#开启并开机自启 nfs
[root@manager ~]# ansible storage -m systemd -a 'name=nfs-server enabled=yes state=started'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"enabled": true,"name": "nfs-server","state": "started",# 关闭并开机不自启 firewalld
[root@manager ~]# ansible front -m systemd -a 'name=firewalld enabled=no state=stopped'
192.168.100.148 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"enabled": false,"name": "firewalld","state": "stopped",# 重启nfs
[root@manager ~]# ansible storage -m systemd -a 'name=nfs-utils state=reloaded'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"name": "nfs-utils","state": "started",
4.1.8 yum模块
yum安装软件
选项 | 描述 |
---|---|
name | 必选项,所安装的包的名称 |
state | 安装-> present(installed) ; 安装最新版本的-> latest ;卸载包->absent(removed) |
update_cache | 强制更新yum的缓存 |
conf_file | 指定远程yum安装时所依赖的配置文件(安装本地已有的包)。 |
disable_pgp_check | 是否禁止GPG checking,只用于presentor latest。 |
disablerepo | 临时禁止使用yum库。 只用于安装或更新时。 |
enablerepo | 临时使用的yum库。只用于安装或更新时。 |
skip_borken | 跳过异常软件节点 |
autoremove | 当设置为yes且状态为absent时,自动移除不再被任何已安装包依赖的包。 |
#安装软件
ansible all -m yum -a 'name=tree,lrzsz,sshpass state=present'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"msg": "","rc": 0,"results": ["Installed: lrzsz-0.12.20-43.el8.x86_64","Installed: sshpass-1.09-4.el8.x86_64"]
}#更新软件包
ansible all -m yum -a "name=rsync state=latest"
4.1.9 get_url模块
相当于wget命令,需要所有主机均能访问互联网
建议在管理节点下载好软件包后,copy分发
选项 | 功能 |
---|---|
url | 指定要下载的地址 |
dest | 下载到哪个目录 |
#下载zabbix
https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm[root@manager ~]# ansible all -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm dest=/tmp/'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"checksum_dest": null,"checksum_src": "b65b658d3246bac2783436b45e499fa57c68a5ef","dest": "/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm","elapsed": 0,"gid": 0,"group": "root","md5sum": "cba9f70da1ef01574bd2608b43d39b31","mode": "0644","msg": "OK (599492 bytes)","owner": "root","secontext": "unconfined_u:object_r:admin_home_t:s0","size": 599492,"src": "/root/.ansible/tmp/ansible-tmp-1716729618.362924-3810-161846721092095/tmpb1f4j0t0","state": "file","status_code": 200,"uid": 0,"url": "https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm"
}[root@manager ~]# ansible all -m shell -a 'ls /tmp/zabbix*'
192.168.100.148 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm
192.168.100.149 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm
192.168.100.150 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm#调用yum模块安装ansible all -m yum -a 'name=/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm state=present'
4.1.10 yum_repository模块
更推荐写好yum配置文件,再copy推送
选项 | 解释 |
---|---|
name | yum源中名字 [epel] |
description | yum源的注释说明 对应的 是name的内容 |
baseurl | yum源中 baseurl 下载地址 |
enabled | 是否启动这个源 yes/no |
gpgcheck | 是否启动gpgcheck功能 no |
file | 指定yum源的文件 自动添加 .repo 默认与模块名字一致. |
- yum配置文件与yum-repository对比:
yum配置文件 | yum-repository选项 |
---|---|
[epel] | name=epel #默认yum源文件的名字与这个一致. |
name=Extra Pxxxxx | description=“Extra xxxxxxx” |
baseurl=http://mirrors.aliyun.com/epel/7/$basearch | baseurl=“http://mirrors.aliyun.com/epel/7/$basearch” |
enabled=1 | enabled=yes |
gpgcheck=0 | gpgcheck=no |
4.1.11 user模块
选项 | 描述 |
---|---|
comment | 用户的描述信息 |
create_home | 是否创建家目录(yes/no) |
force | 在使用state=absent时, 行为与userdel –force一致 |
group | 指定基本组 |
groups | 指定附加组,如果指定为(groups=)表示删除所有组 |
home | 指定用户家目录 |
move_home | 如果设置为home=时, 试图将用户主目录移动到指定的目录 |
name(重要) | 指定用户名 |
non_unique | 该选项允许改变非唯一的用户ID值 |
password(重要) | 指定用户密码 password={{‘密码’ |
remove | 在使用state=absent时, 行为是与userdel –remove一致 |
shell(重要) | 指定默认shell /bin/bash,/sbin/nologin |
state(重要) | 设置帐号状态,默认为present表示新建用户,指定值为absent表示删除 |
system | 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户 |
uid(重要) | 指定用户的uid |
update_password | 更新用户密码 |
expires | 指明密码的过期时间 |
append | 添加一个新的组 |
#创建一个用户,uid2000,nologin
[root@manager ~]# ansible all -m user -a 'name=ans uid=2000 shell=/sbin/nologin create_home=no state=present'
192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": false,"group": 2000,"home": "/home/ans","name": "ans","shell": "/sbin/nologin","state": "present","system": false,"uid": 2000
}# 更新密码
[root@manager ~]# ansible all -m user -a "name=test password={{'1'|password_hash('sha512','hashsalt')}} state=present"
[DEPRECATION WARNING]: Encryption using the Python crypt module is deprecated. The Python
crypt module is deprecated and will be removed from Python 3.13. Install the passlib library
for continued encryption functionality. This feature will be removed in version 2.17.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": true,"group": 2001,"home": "/home/test","name": "test","password": "NOT_LOGGING_PASSWORD","shell": "/bin/bash","state": "present","system": false,"uid": 2001
}
4.1.12 group模块
类比user模块
4.1.13 mount模块
mount选项 | 说明 |
---|---|
fstype | 文件系统类型,指定文件系统,如xfs, ext4, iso9660, nfs等 |
src | 源地址,例如NFS地址(例如172.16.1.31/data) |
path | 注意这里不是dest,挂载点,即要把源挂载到的目标路径 |
state | absent 卸载并修改fstab;unmounted 卸载不修改/etc/fstab;present 仅修改/etc/fstab 不挂载;mounted 挂载并修改/etc/fstab;remounted 重新挂载 |
#挂载nfs#1. 安装nfs
[root@manager ~]# ansible front -m yum -a 'name=nfs-utils state=present'
192.168.100.148 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"msg": "Nothing to do","rc": 0,"results": []
}#2.创建挂载文件夹
[root@manager ~]# ansible front -m file -a 'path=/ans-upload state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/ans-upload","secontext": "unconfined_u:object_r:default_t:s0","size": 6,"state": "directory","uid": 0
}#3.挂载nfs并检查
[root@manager ~]# ansible front -m mount -a 'src=192.168.100.150:/nfsupload/ path=/ans-upload/ fstype=nfs state=mounted'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "nfs","name": "/ans-upload/","opts": "defaults","passno": "0","src": "192.168.100.150:/nfsupload/"
}
[root@manager ~]# ansible front -a 'df -h /ans-upload'
192.168.100.148 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
192.168.100.150:/nfsupload 64G 2.3G 62G 4% /ans-upload
[root@manager ~]# ansible front -a 'grep upload /etc/fstab'
192.168.100.148 | CHANGED | rc=0 >>
192.168.100.150:/nfsupload/ /ans-upload/ nfs defaults 0 0# 4. 卸载nfs并删除fstab
[root@manager ~]# ansible front -m mount -a 'src=192.168.100.150:/nfsupload/ path=/ans-upload/ fstype=nfs state=absent'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "nfs","name": "/ans-upload/","opts": "defaults","passno": "0","src": "192.168.100.150:/nfsupload/"
}
[root@manager ~]# ansible front -a 'df -h /ans-upload' 192.168.100.148 | FAILED | rc=1 >>
df: /ans-upload: No such file or directorynon-zero return code
[root@manager ~]# ansible front -a 'grep upload /etc/fstab' 192.168.100.148 | FAILED | rc=1 >>
non-zero return code
4.1.14 cron模块
管理系统的定时任务
cron模块选项 | 说明 |
---|---|
name | 定时任务名字(一定要加上),对应下面注释的内容 |
minute | 分钟,例如 "*" 表示每分钟,"5" 表示第5分钟 |
hour | 小时,例如 "*" 表示每个小时,"12" 表示中午12点 |
day | 日期,例如 "*" 表示每天,"1" 表示每月1日 |
month | 月份,例如 "*" 表示每个月,"6" 表示6月 |
week | 周几,例如 "*" 表示每天,"0" 表示周日 |
job | 指定命令或脚本(重定向到空),例如 "/sbin/ntpdate ntp1.aliyun.com &>/dev/null" |
state | present 表示添加定时任务(默认);absent 表示删除 |
# 设置每3分钟定时同步时间
[root@manager ~]# ansible all -m cron -a 'name="sync time" minute="*/3" job="/sbin/ntpdate ntp1.aliyun.com &> /dev/null" state=present'
192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"envs": [],"jobs": ["sync time"]
}[root@manager ~]# ansible all -a 'crontab -l'
192.168.100.149 | CHANGED | rc=0 >>
#Ansible: sync time
*/3 * * * * /sbin/ntpdate ntp1.aliyun.com &> /dev/null#删除
[root@manager ~]# ansible all -m cron -a 'name="sync time" state=absent' 192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"envs": [],"jobs": []
}[root@manager ~]# ansible all -a 'crontab -l'
192.168.100.149 | CHANGED | rc=0 >>
4.1.15 setup模块
用于收集远程主机的信息,并将这些信息以facts的形式返回给Ansible控制节点。
这些facts可以包括系统变量(如操作系统类型、架构、网络配置、已安装软件包等),并且在Playbook执行期间可以被其他任务使用。
选项 | 描述 |
---|---|
fact_path | 用于存放本地Ansible事实(*.fact文件)的路径。此目录下的文件如果可执行,将被执行,其结果会被添加到ansible_local事实中;如果文件不可执行,则会被读取。适用于从2.1版本开始。文件/结果格式可以是JSON或INI格式。默认的fact_path可以在ansible.cfg中为自动调用setup作为gather_facts一部分时指定。Windows环境下有特定选项,请查看注释。 |
filter | 如果提供,仅返回匹配此shell风格(fnmatch通配符)的变量。这允许筛选出特定的facts进行查看或使用。 |
gather_subset | 如果提供,限制收集的额外事实子集。可能的值包括:all(全部)、min(最小集合)、hardware(硬件信息)、network(网络信息)、virtual(虚拟化信息)、ohai(类似Chef Ohai的扩展信息)、facter(使用Facter收集的信息)。可以指定值的列表来定义更大的子集。值前可加!来排除特定子集的收集,例如:!hardware,!network,!virtual,!ohai,!facter。如果指定!all,则只收集最小集合。要避免收集最小集合,可以指定!all,!min。要仅收集特定事实,使用!all,!min并指定特定的事实子集。如果只想隐藏某些收集到的事实,使用filter参数。 |
gather_timeout | 设置单个事实收集的默认超时时间(以秒为单位)。这有助于控制事实收集过程,避免因个别慢速收集导致整个任务超时。 |
#查看内存
[root@manager ~]# ansible front -m setup -a "filter='*mem*'"
192.168.100.148 | SUCCESS => {"ansible_facts": {"ansible_memfree_mb": 3028,"ansible_memory_mb": {"nocache": {"free": 3371,"used": 269},"real": {"free": 3028,"total": 3640,"used": 612},"swap": {"cached": 0,"free": 4027,"total": 4027,"used": 0}},"ansible_memtotal_mb": 3640,"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false
}
4.1.16 synchronize模块
使用rsync进行文件和目录同步的功能
选项 | 描述 |
---|---|
compress | 开启压缩,默认为开启 |
archive | 是否采用归档模式同步,保证源和目标文件属性一致 |
checksum | 是否效验文件的校验和 |
dirs | 以非递归的方式传输目录 |
links | 同步链接文件 |
recursive | 是否递归同步子目录,yes/no |
rsync_opts | 使用rsync的额外参数 |
copy_links | 同步的时候是否复制连接 |
delete | 删除源中没有而目标存在的文件 |
src | 源目录及文件 |
dest | 目标目录及文件 |
dest_port | 目标接受的端口 |
rsync_path | 服务的路径,指定rsync命令来在远程服务器上运行 |
rsync_timeout | 指定rsync操作的IP超时时间 |
set_remote_user | 设置远程用户名 |
–exclude=.log | 忽略同步.log结尾的文件 |
mode | 同步的模式,rsync同步的方式PUSH、PULL,默认都是推送push |
#将本地的/tmp同步到front组的/tmp
ansible front -m synchronize -a "src=/tmp/ dest=/tmp/"#完全同步,类似rsync的完全同步
ansible front -m synchronize -a 'src=/tmp/ dest=/tmp/ delete=yes'
4.1.x 官网
docs.ansible.com
这篇关于Ansible02-Ansible Modules模块详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!