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 ~]#
相关推荐
十年编程老舅2 小时前
Linux 多线程高并发编程:读写锁的核心原理与底层实现
linux·c++·linux内核·高并发·线程池·多线程·多进程
乌恩大侠2 小时前
【KrakenSDR】MATLAB接口
服务器·网络·matlab
qq_339191142 小时前
uv 设置系统默认版本, linux设置uv
linux·运维·uv
似水এ᭄往昔3 小时前
【Linux】--进程概念
linux·运维·服务器
IDIOT___IDIOT3 小时前
Linux 使用 `cp` 命令导致挂载点被覆盖问题记录
linux·运维·服务器
顶点多余3 小时前
线程互斥+线程同步+生产消费模型
java·linux·开发语言·c++
李彦亮老师(本人)3 小时前
Rocky Linux 9.x 安全加固实战指南:从系统初始化到生产级防护
linux·运维·安全·rocky
RisunJan3 小时前
Linux命令-mount(用于挂载Linux系统外的文件)
linux·运维·服务器
脆皮炸鸡7553 小时前
Linux开发工具~~~版本控制器Git以及调试工具GDB
linux·服务器·开发语言·经验分享·git·学习方法