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
相关推荐
Ruiery10 分钟前
Linux 6.6内核 CPU 深度解析(七):调度器启动 — 从单核到多核,调度器分阶段点亮
linux·运维·服务器
50万马克的面包13 分钟前
CSDN-栈队列数组-知识点整理
linux·运维·服务器
wjkjpcba2 小时前
PCBA烧录程序是什么:PCBA包工包料厂家解析烧录与测试
linux·数据库·人工智能·smt贴片加工·pcba贴片加工厂
꯭自꯭闭꯭2 小时前
达梦事物特性及MVCC
linux·运维·数据库
91刘仁德3 小时前
HTTP协议详解:从URL到HTTP服务器的完整实战
服务器·网络·笔记·网络协议·http
羔羊++4 小时前
18_实验十七_其他命令与自定义启动变量
linux
DYWorker0016 小时前
Linux驱动:RK3568 GMAC + YT8531C 千兆以太网全流程解剖
linux
lisanmengmeng7 小时前
NRPE 添加命令(一)
linux·运维·服务器
树下水月8 小时前
Typora破解
linux·服务器·前端
沫璃染墨9 小时前
《从零入门Linux系统篇(五十七):线程篇·十——生产者消费者模型进阶:从环形缓冲区到POSIX信号量》
linux·运维·服务器·开发语言·c++·系统架构·信号处理