设置定时任务
ansible.builtin.cron
:设置 cron
定时任务。
参数名 |
类型 |
默认值 |
描述 |
name |
str |
null | cron 任务的注释 |
backup |
bool |
false |
修改前是否备份原始 crontab 文件。 |
cron_file |
str |
null | 指定编辑 |
day |
str |
* |
指定任务运行的“日”(1-31),如 |
disabled |
bool |
false |
是否禁用该任务,等同于注释该行。 |
env |
bool |
false |
指定此行为环境变量而非任务。例如设置 |
hour |
str |
* |
任务运行的小时(0-23),支持范围、列表、间隔等。 |
insertafter |
str |
null | 插入任务时,在匹配该行之后插入。适用于编辑 |
insertbefore |
str |
null | 插入任务时,在匹配该行之前插入。适用于编辑 |
job |
str |
null | 实际要执行的命令,如 |
minute |
str |
* |
任务运行的分钟(0-59),支持范围、间隔、列表。 |
month |
str |
* |
指定运行月份(1-12 或 Jan-Dec)。 |
special_time |
str |
null | 特殊时间格式,如: |
state |
str |
present |
设为 |
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
这里要注意一点,设置
job
时name
不能重复,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_reload |
bool |
false |
null | 执行 |
enabled |
bool |
null | null | 设置服务是否开机自启动。 |
force |
bool |
null | null | 覆盖已有的 symlink 软链接。2.6 版本新增。 |
masked |
bool |
null | null | 设置服务是否被 mask。mask 后服务无法启动。 |
name |
str |
null | null | 指定要操作的单元名称(如 |
no_block |
bool |
false |
null | 异步执行 systemd 操作,不阻塞等待完成。2.3 版本新增。 |
scope |
str |
system |
system , |
指定 systemctl 的作用域。 |
state |
str |
null | reloaded , |
控制单元状态: |
常用选项:
选项 |
类型 |
默认值 |
描述 |
daemon_reload |
bool |
false |
执行 |
enabled |
bool |
null | 设置服务是否开机自启动。 |
masked |
bool |
null | 设置服务是否被 mask。mask 后服务无法启动。 |
name |
str |
null | 指定要操作的单元名称(如 |
no_block |
bool |
false |
异步执行 systemd 操作,不阻塞等待完成。2.3 版本新增。 |
scope |
str |
system |
指定 systemctl 的作用域。 |
state |
str |
null | 控制单元状态: |
- 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 时被忽略。别名: |
enabled |
bool |
null | 服务是否开机自启动。 |
name |
str |
null | 要操作的服务名称。 |
pattern |
str |
null | 指定服务状态无法通过 status 命令获取时,用 |
runlevel |
str |
default |
OpenRC (如 Gentoo) 使用的运行级别。systemd 下无效。 |
sleep |
int |
null | restarted 时,stop 与 start 之间等待的秒数,避免某些 init 脚本立即返回导致问题。systemd 下无效。1.3 版本新增。 |
state |
str |
null | 控制服务状态。 |
use |
str |
auto |
强制指定使用特定服务管理模块,默认通过 |
- 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