Ansible常用模块
hostname模块
复制代码
-m hostname
修改被控端主机名称
[root@ansible ~]# ansible 192.168.92.20 -m hostname -a 'name=web20h'
192.168.92.20 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "web20h",
"ansible_hostname": "web20h",
"ansible_nodename": "web20h",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "web20h"
}
[root@web20 ~]# bash
[root@web20h ~]#
selinux模块
复制代码
-m selinux
负责管理内核
| 指令参数 |
选项 |
说明 |
| state |
enforcing, disabled |
设置内核模式 |
复制代码
[root@ansible ~]# ansible webservers -m selinux -a 'state=disabled'
192.168.92.20 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"configfile": "/etc/selinux/config",
"msg": "",
"policy": "targeted",
"reboot_required": false,
"state": "disabled"
}
file模块
复制代码
-m file
在被控端创建文件或者目录,支持权限的设定等
| 指令参数 |
选项 |
说明 |
| path |
|
指定被控端的路径 |
| recurse |
|
递归方式操作 |
| state |
touch, directory, link |
文件或者目录的操作状态 |
| absent |
|
删除文件\目录 |
| owner |
root |
文件或者目录创建后,被控端默认属主是root |
| group |
root |
文件或者目录创建后,被控端默认属组是root |
| mode |
file、 dir |
文件复制过去后,文件或者目录的权限 |
复制代码
[root@ansible ~]# ansible webservers -m file -a 'path=/opt/qq.txt state=touch'
192.168.92.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"dest": "/opt/qq.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'ls -l /opt'
192.168.92.20 | CHANGED | rc=0 >>
total 16
-rw-r--r-- 1 root root 49 Mar 26 15:35 a.txt
-rwxr--r-- 1 yun root 36 Mar 26 20:21 cp.txt
-rwxr--r-- 1 yun root 32 Mar 26 20:18 cp.txt.2585.2026-03-26@20:21:13~
-rw-r--r-- 1 root root 0 Mar 26 21:42 qq.txt
-rw-r--r-- 1 root root 5 Mar 26 20:26 xxx.txt
[root@ansible ~]# ansible webservers -m file -a 'path=/opt/yun state=directory'
192.168.92.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/opt/yun",
"size": 6,
"state": "directory",
"uid": 0
}
[root@ansible ~]# ansible webservers -m file -a 'path=/opt/qq.txt state=absent'
192.168.92.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/opt/qq.txt",
"state": "absent"
}
[root@ansible ~]# ansible webservers -m file -a 'path=/opt/yun state=absent'
192.168.92.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/opt/yun",
"state": "absent"
}