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
相关推荐
Sokach10154 分钟前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
AlfredZhao16 小时前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3102 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒4 天前
TShark:Wireshark CLI 功能
linux
A小辣椒4 天前
TShark:基础知识
linux
AlfredZhao4 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao5 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334665 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪5 天前
linux 拷贝文件或目录到指定的位置
linux