Ansible 常用模块详解:yum、service/systemd、copy实战

Ansible常用模块

yum 模块

复制代码
-m yum
    提供管理各个被控端的系统的软件包
指令参数 选项 说明
name nginx、 httpd...... 指定安装软件的url
state prnsent, installed,latest, absent, removed prnsent 默认安装;installed安装;latest安装最新版本;absent、removed表示卸载。允许多个软件一次性安装,使用逗号隔开就可以
enablerepo epel、 base 指定从那些仓库获取软件,密钥等等
disablerepo epel、 base 禁止从哪些仓库获取软件等等
exclude httpd 排除某些指定包
download_only yes, no 只下载不安装
复制代码
[root@ansible ~]# ansible webservers -m yum -a 'name=nginx state=latest'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: nginx-core-2:1.20.1-24.el9.x86_64",
        "Installed: rocky-logos-httpd-90.16-1.el9.noarch",
        "Installed: nginx-2:1.20.1-24.el9.x86_64",
        "Installed: nginx-filesystem-2:1.20.1-24.el9.noarch"
    ]
}
[root@ansible ~]# ansible webservers -m yum -a 'name=nginx state=absent'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: nginx-2:1.20.1-24.el9.x86_64"
    ]
}

service/systemd模块

复制代码
-m service
-m systemd 
    二者都是管理被控端服务的状态,一般不使用systemd,一般使用service
指令参数 选项 说明
name nginx、httpd 指定需要控制的服务名称
state started, stopped, restarted, reloaded 指定服务状态
enabled yes, no 是否开机自启
daemon_reload yes 重启systemd服务
复制代码
[root@ansible ~]# ansible webservers -m yum -a 'name=httpd state=latest'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-bdb-1.6.1-23.el9.x86_64",
        "Installed: httpd-2.4.62-7.el9_7.3.x86_64",
        "Installed: apr-util-openssl-1.6.1-23.el9.x86_64",
        "Installed: httpd-core-2.4.62-7.el9_7.3.x86_64",
        "Installed: mod_http2-2.0.26-5.el9.x86_64",
        "Installed: httpd-filesystem-2.4.62-7.el9_7.3.noarch",
        "Installed: mod_lua-2.4.62-7.el9_7.3.x86_64",
        "Installed: httpd-tools-2.4.62-7.el9_7.3.x86_64",
        "Installed: apr-1.7.0-12.el9_3.x86_64",
        "Installed: apr-util-1.6.1-23.el9.x86_64",
        "Installed: mailcap-2.1.49-5.el9.0.2.noarch"
    ]
} 
[root@ansible ~]# ansible webservers -m service -a 'name=httpd state=started enabled=yes'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",

...... 
[root@ansible ~]# ansible webservers -m service -a 'name=httpd state=stopped'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "name": "httpd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "Thu 2026-03-26 16:03:46 CST",
        "ActiveEnterTimestampMonotonic": "10020965015",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
......

copy模块

复制代码
-m copy 
    提供文件复制操作功能
指令参数 选项 说明
src 本地主机指定目录的文件
dest 远程被控端的绝对路径
owner root 文件复制过去后,被控端默认属主是root
group root 文件复制过去后,被控端默认属组是root
mode file、 dir 文件复制过去后,文件或者目录的权限
backup yes 备份被修改之前的文件
content 新建文件并向文件内容增加内容,内容:content=""
复制代码
[root@ansible ~]# echo "这个是我测试copy的文件" >> cp.txt
[root@ansible ~]# ls
3.26.sh  anaconda-ks.cfg  cp.txt 
[root@ansible ~]# ansible webservers -m shell -a "useradd yun"
192.168.92.20 | CHANGED | rc=0 >>

[root@ansible ~]# ansible webservers -m copy -a "src=/root/cp.txt dest=/opt/cp.txt owner=yun mode=744"
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "a5bce5e46a06f8fa30ca18d3d1db00ab024fbf2f",
    "dest": "/opt/cp.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "a054ad112570524cd5cf3150ace1bfd9",
    "mode": "0744",
    "owner": "yun",
    "size": 32,
    "src": "/root/.ansible/tmp/ansible-tmp-1774527498.2669854-3089-102050268865803/source",
    "state": "file",
    "uid": 1000
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/cp.txt'
192.168.92.20 | CHANGED | rc=0 >>
这个是我测试copy的文件
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/cp.txt | ls -l /opt'
192.168.92.20 | CHANGED | rc=0 >>
total 8
-rw-r--r-- 1 root root 49 Mar 26 15:35 a.txt
-rwxr--r-- 1 yun  root 32 Mar 26 20:18 cp.txt 
[root@ansible ~]#  echo "aaa" >> cp.txt
[root@ansible ~]# ansible webservers -m copy -a "src=/root/cp.txt dest=/opt/cp.txt owner=yun mode=744 backup=yes"
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup_file": "/opt/cp.txt.2585.2026-03-26@20:21:13~",
    "changed": true,
    "checksum": "894a1f72ed2f60efb1eeadf70a1cf89f55362671",
    "dest": "/opt/cp.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "96bef3d733dcaf112c641530b4efd8a2",
    "mode": "0744",
    "owner": "yun",
    "size": 36,
    "src": "/root/.ansible/tmp/ansible-tmp-1774527672.4405096-4185-41499436453149/source",
    "state": "file",
    "uid": 1000
}
[root@ansible ~]# ansible webservers -m shell -a 'ls -l /opt'
192.168.92.20 | CHANGED | rc=0 >>
total 12
-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~
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/cp.txt'
192.168.92.20 | CHANGED | rc=0 >>
这个是我测试copy的文件
aaa
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/cp.txt.2585.2026-03-26@20:21:13~'
192.168.92.20 | CHANGED | rc=0 >>
这个是我测试copy的文件
[root@ansible ~]# ansible webservers -m copy -a 'dest=/opt/xxx.txt content="yun02"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "432dae28db047392c645776e3211bfe3784376ef",
    "dest": "/opt/xxx.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "5dcbc7af1d4d95b916da7b4b9aa04168",
    "mode": "0644",
    "owner": "root",
    "size": 5,
    "src": "/root/.ansible/tmp/ansible-tmp-1774527995.7517238-6182-135722670914289/source",
    "state": "file",
    "uid": 0
} 
[root@ansible ~]# ansible webservers -a 'cat /opt/xxx.txt'
192.168.92.20 | CHANGED | rc=0 >>
yun02
相关推荐
charlie1145141916 小时前
嵌入式Linux驱动开发——新 API 字符设备驱动完整教程 - 从设备结构体到应用测试
linux·运维·驱动开发
消失的旧时光-19436 小时前
C语言对象模型系列(四)《Linux 内核里的 container_of 到底是什么黑魔法?》—— 一篇讲透 Linux 内核的“对象模型”核心技巧
linux·c语言·算法
SWAGGY..7 小时前
Linux系统编程:(二)基础指令详解
linux·运维·服务器
kdxiaojie7 小时前
U-Boot分析【学习笔记】(3)
linux·笔记·学习
烛衔溟7 小时前
TypeScript 接口继承与混合类型
linux·ubuntu·typescript
卧室小白8 小时前
ansible自动化
ansible
蜡笔婧萱8 小时前
Linux--远程登录服务ssh
linux·服务器·ssh
雾岛心情8 小时前
小铭邮件管理工具箱的界面(公司版)
运维·服务器·工具·o365·小铭邮件工具箱(公司版)
伏加特遇上西柚8 小时前
Loki+Alloy+Grafana日志采集部署
java·linux·服务器·spring boot·grafana·prometheus
zl_dfq8 小时前
服务器设计细节 之 【eventfd、struct stat、stat接口】
服务器