Ansible 常用模块详解:hostname、selinux 、file实战

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"
}
相关推荐
牛奶6 小时前
如何自己写一个浏览器插件?
前端·chrome·浏览器
亿元程序员7 小时前
为什么Cocos都4.0了还有人用2.x?
前端
MomentYY7 小时前
AI 到底是“懂”,还是在“猜”?
前端·人工智能·ai编程
鹏毓网络科技7 小时前
Cursor Rules 文件配置实战:3 个隐藏参数让我每月少写 40% 样板代码
前端·github
没烦恼3017 小时前
无痕模式下 HTTP\-First 拦截引发的“页面刷新”误判
前端
文心快码BaiduComate7 小时前
从个人提效到组织提效:Comate辅助构建自我进化的AI研发系统
前端·程序员
hunterandroid7 小时前
Compose 状态管理:remember、rememberSaveable 与状态提升
前端
星栈8 小时前
Dioxus 接数据库最容易写歪的 3 个地方:sqlx + SQLite 怎么接才顺
前端·rust·前端框架
晴虹8 小时前
vue3-scroll-more:横向滚动条-元素或页签过多滚动显示处理的组件
前端·vue.js
代码搬运媛8 小时前
Claude 全栈开发专用 Rules 配置
前端