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"
}
相关推荐
许彰午26 分钟前
12_ArrayList与LinkedList深度对比
java·前端·python
lichenyang45343 分钟前
鸿蒙练习 12:Provider/Consumer 跨层共享 + HAR 多模块拆分
前端
朱涛的自习室1 小时前
逃离“古法测试”:AI 测试的“三大定律”
android·前端·人工智能
糖果店的幽灵1 小时前
Claude Code 完全实战指南 - 第二章:CLI 命令大全
前端·chrome
ZC跨境爬虫1 小时前
跟着 MDN 学CSS day_45:媒体查询入门指南——从语法到移动优先实践
前端·css·ui·html·tensorflow·媒体
Hoey2 小时前
虚拟 DOM 和 DIFF 算法
前端·vue.js
bkspiderx2 小时前
HTTP协议:Web通信的“通用语言”解析
前端·网络协议·http
云水一下2 小时前
模块系统与 npm——万物皆模块
前端·npm·node.js
ZC跨境爬虫2 小时前
跟着 MDN 学CSS day_47:(移动优先实战——从手机到宽屏的响应式进化)
前端·css·html·tensorflow·媒体
小新1102 小时前
vue实战项目 计算器
前端·javascript·vue.js