Ansible 常用模块详解:cron、archive、unarchive实战

Ansible常用模块

cron模块

复制代码
-m cron
    管理被控端的定时、计划任务
指令参数 选项 说明
name 任务的名字
job 任务的工作
minute 0-59 分钟(默认)
hour 0-23 小时
day 1-31
mouth 1-12
weekday 0-6 周几,0代表周日
disabled yes, no 关闭
复制代码
[root@ansible ~]# ansible webservers -m cron -a 'name=fen job="ls -l /opt >> /opt/ceshi.txt"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "fen"
    ]
}
[root@ansible ~]# ansible webservers -m shell -a "crontab -l"
192.168.92.20 | CHANGED | rc=0 >>
#Ansible: fen
* * * * * ls -l /opt >> /opt/ceshi.txt
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/ceshi.txt'
192.168.92.20 | CHANGED | rc=0 >>
total 152
-rw-r--r-- 1 root root     96 Mar 27 10:34 a.txt
-rw-r--r-- 1 root root     14 Mar 27 10:37 b.txt
-rw-r--r-- 1 root root      0 Mar 27 21:03 ceshi.txt
-rwxr--r-- 1 1000 root     36 Mar 26 20:21 cp.txt
-rwxr--r-- 1 1000 root     32 Mar 26 20:18 cp.txt.2585.2026-03-26@20:21:13~
-rw-r--r-- 1 root root 130434 Mar 27 16:23 index.html
-rw-r--r-- 1 root root     66 Mar 27 11:39 replace.txt
-rw-r--r-- 1 root root      5 Mar 26 20:26 xxx.txt
total 156
-rw-r--r-- 1 root root     96 Mar 27 10:34 a.txt
-rw-r--r-- 1 root root     14 Mar 27 10:37 b.txt
-rw-r--r-- 1 root root    447 Mar 27 21:03 ceshi.txt
-rwxr--r-- 1 1000 root     36 Mar 26 20:21 cp.txt
-rwxr--r-- 1 1000 root     32 Mar 26 20:18 cp.txt.2585.2026-03-26@20:21:13~
-rw-r--r-- 1 root root 130434 Mar 27 16:23 index.html
-rw-r--r-- 1 root root     66 Mar 27 11:39 replace.txt
-rw-r--r-- 1 root root      5 Mar 26 20:26 xxx.txt
total 156
-rw-r--r-- 1 root root     96 Mar 27 10:34 a.txt
-rw-r--r-- 1 root root     14 Mar 27 10:37 b.txt
-rw-r--r-- 1 root root    894 Mar 27 21:04 ceshi.txt
-rwxr--r-- 1 1000 root     36 Mar 26 20:21 cp.txt
-rwxr--r-- 1 1000 root     32 Mar 26 20:18 cp.txt.2585.2026-03-26@20:21:13~
-rw-r--r-- 1 root root 130434 Mar 27 16:23 index.html
-rw-r--r-- 1 root root     66 Mar 27 11:39 replace.txt
-rw-r--r-- 1 root root      5 Mar 26 20:26 xxx.txt
total 156
-rw-r--r-- 1 root root     96 Mar 27 10:34 a.txt
-rw-r--r-- 1 root root     14 Mar 27 10:37 b.txt
-rw-r--r-- 1 root root   1341 Mar 27 21:05 ceshi.txt
-rwxr--r-- 1 1000 root     36 Mar 26 20:21 cp.txt
-rwxr--r-- 1 1000 root     32 Mar 26 20:18 cp.txt.2585.2026-03-26@20:21:13~
-rw-r--r-- 1 root root 130434 Mar 27 16:23 index.html
-rw-r--r-- 1 root root     66 Mar 27 11:39 replace.txt
-rw-r--r-- 1 root root      5 Mar 26 20:26 xxx.txt 
[root@ansible ~]# ansible webservers -m cron -a 'name=fen job="ls -l /opt >> /opt/ceshi.txt" disabled=yes'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "fen"
    ]
}
[root@ansible ~]# ansible webservers -m shell -a "crontab -l"
192.168.92.20 | CHANGED | rc=0 >>
#Ansible: fen
#* * * * * ls -l /opt >> /opt/ceshi.txt 
#如果有两个任务内容,有相同任务名称,那么新建的任务会覆盖之前的任务 
[root@ansible ~]# 案例需求:在凌晨1点和3点执行一个查询任务
-bash: 案例需求:在凌晨1点和3点执行一个查询任务: command not found
[root@ansible ~]# ansible webservers -m cron -a 'name=lingchen hour=1,3 job="ls -l /root >> /dev/null"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "fen",
        "lingchen"
    ]
}
[root@ansible ~]# ansible webservers -m shell -a "crontab -l"
192.168.92.20 | CHANGED | rc=0 >>
#Ansible: fen
#* * * * * ls -l /opt >> /opt/ceshi.txt
#Ansible: lingchen
* 1,3 * * * ls -l /root >> /dev/null 
[root@ansible ~]# ansible webservers -m cron -a 'name=lingchen hour=1,3 minute=30 job="ls -l /root >> /dev/null"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "fen",
        "lingchen"
    ]
}
[root@ansible ~]# ansible webservers -m shell -a "crontab -l"
192.168.92.20 | CHANGED | rc=0 >>
#Ansible: fen
#* * * * * ls -l /opt >> /opt/ceshi.txt
#Ansible: lingchen
30 1,3 * * * ls -l /root >> /dev/null

archive 模块

复制代码
-m archive 
    管理被控端打包或者压缩的功能
指令参数 选项 说明
path 被控端需要压缩或者打包的文件目录
dest 压缩后存放的位置
format bz2、gz、zip、tar 制定压缩类型,默认gz
remove yes, no 打包之后,是否删除源文件,默认no
复制代码
[root@ansible ~]# ansible webservers -m shell -a 'ls /opt'
192.168.92.20 | CHANGED | rc=0 >>
a.txt
b.txt
ceshi.txt
cp.txt
cp.txt.2585.2026-03-26@20:21:13~
index.html
replace.txt
xxx.txt
[root@ansible ~]# ansible webservers -m archive -a 'path=/opt/a.txt dest=/opt/a.tar.gz'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "archived": [
        "/opt/a.txt"
    ],
    "arcroot": "/opt/",
    "changed": true,
    "dest": "/opt/a.tar.gz",
    "dest_state": "compress",
    "expanded_exclude_paths": [],
    "expanded_paths": [
        "/opt/a.txt"
    ],
    "gid": 0,
    "group": "root",
    "missing": [],
    "mode": "0644",
    "owner": "root",
    "size": 92,
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'ls /opt'
192.168.92.20 | CHANGED | rc=0 >>
a.tar.gz
a.txt
b.txt
ceshi.txt
cp.txt
cp.txt.2585.2026-03-26@20:21:13~
index.html
replace.txt
xxx.txt
[root@ansible ~]# ansible webservers -m archive -a 'path=/opt/b.txt dest=/opt/b.tar.gz remove=yes'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "archived": [
        "/opt/b.txt"
    ],
    "arcroot": "/opt/",
    "changed": true,
    "dest": "/opt/b.tar.gz",
    "dest_state": "compress",
    "expanded_exclude_paths": [],
    "expanded_paths": [
        "/opt/b.txt"
    ],
    "gid": 0,
    "group": "root",
    "missing": [],
    "mode": "0644",
    "owner": "root",
    "size": 30,
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'ls /opt'
192.168.92.20 | CHANGED | rc=0 >>
a.tar.gz
a.txt
b.tar.gz
ceshi.txt
cp.txt
cp.txt.2585.2026-03-26@20:21:13~
index.html
replace.txt
xxx.txt
[root@ansible ~]#

unarchive 模块

复制代码
-m unarchive 
    解压缩控制端的压缩包,然后下发到被控端
指令参数 选项 说明
src 压缩包文件
dest 解压后文件存放路径
remote_src yes,no 本地压缩包是否分发给被控端,默认no
owner 设置属主
group 设置属组
mode 权限
复制代码
[root@ansible ~]# tar czf cp.tar.gz cp.txt
[root@ansible ~]# ls
3.26.sh  a.txt  an-4.yml  anaconda-ks.cfg  bianliang.yml  cp.tar.gz  cp.txt  replace.txt 
[root@ansible ~]# ansible webservers -m dnf -a 'name=tar state=present'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: tar-2:1.34-9.el9_7.x86_64"
    ]
}
[root@ansible ~]# ansible webservers -m unarchive -a 'src=cp.tar.gz dest=/opt'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "dest": "/opt",
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar",
            "--extract",
            "-C",
            "/opt",
            "-z",
            "-f",
            "/root/.ansible/tmp/ansible-tmp-1774619438.237648-36228-31056999503388/source"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "size": 174,
    "src": "/root/.ansible/tmp/ansible-tmp-1774619438.237648-36228-31056999503388/source",
    "state": "directory",
    "uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'ls /opt'
192.168.92.20 | CHANGED | rc=0 >>
a.tar.gz
a.txt
b.tar.gz
ceshi.txt
cp.txt
cp.txt.2585.2026-03-26@20:21:13~
index.html
replace.txt
xxx.txt
[root@ansible ~]#
相关推荐
u01090535912 分钟前
NAS远程访问内网穿透方案之使用神卓N600实现不限速方法
服务器·网络·数据库
闲云自留地14 分钟前
Neutron 实战教程:物理 vs 虚拟网络,OVN 环境创建网络与安全策略
服务器·网络·openstack
为思念酝酿的痛20 分钟前
传输层协议UDP
linux·网络·udp
Elnaij23 分钟前
Linux系统与系统编程(16)——日志、线程池的实现与线程安全
linux
szarron34 分钟前
HTOOL‑SA12 + HT06 近场探头组合实操|手持设备工位 EMI 预兼容排查完整流程
运维·服务器·开发语言·网络·射频工程·频谱仪
杨云龙UP34 分钟前
DB2 HADR 主备架构活动日志参数优化操作手册
linux·运维·服务器·数据库·db2·db2 hadr·日志参数调整
我是大猴子39 分钟前
MyBatis‑Plus & MyBatis‑Flex 区别
java·服务器·数据库
csdn_aspnet1 小时前
如何从电脑主机拷东西到VWmare虚拟机
linux·运维·服务器·windows·vmware·虚拟机
码农幻想梦1 小时前
“找不到 www.bilibili.com 的服务器 IP 地址”解决办法
运维·服务器·tcp/ip
Vcaker1 小时前
Linux学习28-Kubernetes service
linux·运维·学习