Ansible 模块 —— 服务管理和设置定时任务

设置定时任务

ansible.builtin.cron:设置 cron 定时任务。

参数名 类型 默认值 描述
name str null cron 任务的注释
backup bool false 修改前是否备份原始 crontab 文件。
cron_file str null 指定编辑 /etc/cron.d/ 中的某个文件(不作用于用户 crontab)。
day str * 指定任务运行的“日”(1-31),如 115*/2
disabled bool false 是否禁用该任务,等同于注释该行。
env bool false 指定此行为环境变量而非任务。例如设置 PATH
hour str * 任务运行的小时(0-23),支持范围、列表、间隔等。
insertafter str null 插入任务时,在匹配该行之后插入。适用于编辑 cron_file
insertbefore str null 插入任务时,在匹配该行之前插入。适用于编辑 cron_file
job str null 实际要执行的命令,如 /usr/bin/backup.sh
minute str * 任务运行的分钟(0-59),支持范围、间隔、列表。
month str * 指定运行月份(1-12 或 Jan-Dec)。
special_time str null 特殊时间格式,如:rebootdailymonthlyhourlyweeklyyearly。一旦设置,此参数将替代分钟、小时等。
state str present 设为 present 创建或更新任务,设为 absent 删除任务。
user str 当前用户 指定任务所属用户,仅在 playbook 里以 root 用户运行时有效。
weekday str * 指定星期几运行(0-7 或 Sun-Sat),0 和 7 都代表星期日。
- name: Creates a cron file under /etc/cron.d
  ansible.builtin.cron:
    name: yum autoupdate
    weekday: "2"
    minute: "0"
    hour: "12"
    user: root
    job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
    cron_file: ansible_yum-autoupdate

- name: Removes a cron file from under /etc/cron.d
  ansible.builtin.cron:
    name: "yum autoupdate"
    cron_file: ansible_yum-autoupdate
    state: absent

- name: Removes "APP_HOME" environment variable from crontab
  ansible.builtin.cron:
    name: APP_HOME
    env: yes
    state: absent

- name: set PATH env
  ansible.builtin.cron:
    name: PATH
    env: yes
    job: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    user: root

这里要注意一点,设置 jobname 不能重复,Ansible 通过 name 来确定任务。

举个例子,当前 cron 如下:

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
#Ansible: cron test
* * * * * root echo $(date) >> /tmp/date.log

使用下边这个 YAML 进行删除:

- name: Remove cron job
  ansible.builtin.cron:
    name: cron test
    job: echo "test" >> /tmp/date.log
    user: root
    state: absent
    cron_file: cron_date

可以将 * * * * * root echo $(date) >> /tmp/date.log 任务删除,因为是通过 cron test 来进行匹配的。

backup 测试没生效,没用明白,不过这个应该用的不多。

服务管理

ansible.builtin.systemd

ansible.builtin.systemd 用于管理 systemd 管理的服务。

选项 类型 默认值 可选值 描述
daemon_reexec bool false null 执行 daemon-reexec,序列化 systemd 管理器状态后再执行其他操作。别名:daemon-reexec。2.8 版本新增。
daemon_reload bool false null 执行 daemon-reload,确保 systemd 读取配置变更。即使不启动或停止服务也会执行。别名:daemon-reload
enabled bool null null 设置服务是否开机自启动。stateenabled 至少要设置一个。
force bool null null 覆盖已有的 symlink 软链接。2.6 版本新增。
masked bool null null 设置服务是否被 mask。mask 后服务无法启动。
name str null null 指定要操作的单元名称(如 nginxcrond.service)。无后缀时默认 .service。别名:serviceunit
no_block bool false null 异步执行 systemd 操作,不阻塞等待完成。2.3 版本新增。
scope str system system, user, global 指定 systemctl 的作用域。user 模式下需确保用户 dbus 可用。2.7 版本新增。
state str null reloaded, restarted, started, stopped 控制单元状态:startedstopped 幂等,restarted 每次都会重启,reloaded 会重新加载且确保服务运行。

常用选项:

选项 类型 默认值 描述
daemon_reload bool false 执行 daemon-reload,确保 systemd 读取配置变更。即使不启动或停止服务也会执行。别名:daemon-reload
enabled bool null 设置服务是否开机自启动。stateenabled 至少要设置一个。
masked bool null 设置服务是否被 mask。mask 后服务无法启动。
name str null 指定要操作的单元名称(如 nginxcrond.service)。无后缀时默认 .service。别名:serviceunit
no_block bool false 异步执行 systemd 操作,不阻塞等待完成。2.3 版本新增。
scope str system 指定 systemctl 的作用域。user 模式下需确保用户 dbus 可用。2.7 版本新增。(可选值 system, user, global
state str null 控制单元状态:startedstopped 幂等,restarted 每次都会重启,reloaded 会重新加载且确保服务运行。
- name: Make sure a service unit is running
  ansible.builtin.systemd_service:
    state: started
    name: httpd

- name: Stop service cron on debian, if running
  ansible.builtin.systemd_service:
    name: cron
    state: stopped

- name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes
  ansible.builtin.systemd_service:
    state: restarted
    daemon_reload: true
    name: crond

- name: Reload service httpd, in all cases
  ansible.builtin.systemd_service:
    name: httpd.service
    state: reloaded

- name: Enable service httpd and ensure it is not masked
  ansible.builtin.systemd_service:
    name: httpd
    enabled: true
    masked: no

- name: Enable a timer unit for dnf-automatic
  ansible.builtin.systemd_service:
    name: dnf-automatic.timer
    state: started
    enabled: true

- name: Just force systemd to reread configs (2.4 and above)
  ansible.builtin.systemd_service:
    daemon_reload: true

- name: Just force systemd to re-execute itself (2.8 and above)
  ansible.builtin.systemd_service:
    daemon_reexec: true

- name: Run a user service when XDG_RUNTIME_DIR is not set on remote login
  ansible.builtin.systemd_service:
    name: myservice
    state: started
    scope: user
  environment:
    XDG_RUNTIME_DIR: "/run/user/{{ myuid }}"

ansible.builtin.service

ansible.builtin.service 通用型服务管理模块(自动识别 init/systemd/upstart),适用于 RHEL 6 以及之前的系统。

选项 类型 默认值 描述
arguments str '' 命令行附加参数。使用 systemd 时被忽略。别名:args
enabled bool null 服务是否开机自启动。stateenabled 至少要设置一个。
name str null 要操作的服务名称。
pattern str null 指定服务状态无法通过 status 命令获取时,用 ps 命令输出匹配的字符串作为替代判断。systemd 下无效。0.7 版本新增。
runlevel str default OpenRC (如 Gentoo) 使用的运行级别。systemd 下无效。
sleep int null restarted 时,stop 与 start 之间等待的秒数,避免某些 init 脚本立即返回导致问题。systemd 下无效。1.3 版本新增。
state str null 控制服务状态。startedstopped 是幂等操作,restarted 每次都会重启,reloaded 会重新加载并确保服务运行。stateenabled 至少设置一个。
use str auto 强制指定使用特定服务管理模块,默认通过 ansible_service_mgr 自动检测。2.2 版本新增。
- name: Start service httpd, if not started
  ansible.builtin.service:
    name: httpd
    state: started

- name: Stop service httpd, if started
  ansible.builtin.service:
    name: httpd
    state: stopped

- name: Restart service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: restarted

- name: Reload service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: reloaded

- name: Enable service httpd, and not touch the state
  ansible.builtin.service:
    name: httpd
    enabled: yes

- name: Start service foo, based on running process /usr/bin/foo
  ansible.builtin.service:
    name: foo
    pattern: /usr/bin/foo
    state: started

- name: Restart network service for interface eth0
  ansible.builtin.service:
    name: network
    state: restarted
    args: eth0
Ansible 模块 —— 服务管理和设置定时任务
https://www.linuxstudynotes.com/2025/05/15/ansible-%e6%a8%a1%e5%9d%97/ansible-%e6%a8%a1%e5%9d%97-%e6%9c%8d%e5%8a%a1%e7%ae%a1%e7%90%86%e5%92%8c%e8%ae%be%e7%bd%ae%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1/
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇