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 
    负责管理内核
指令参数选项说明
stateenforcing, 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递归方式操作
statetouch, directory, link文件或者目录的操作状态
absent删除文件\目录
ownerroot文件或者目录创建后,被控端默认属主是root
grouproot文件或者目录创建后,被控端默认属组是root
modefile、 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"
}
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐