Ansible Cron모듈을 통해 Nginx Reload 작업을 등록한다.
--- CreateCronJobs.yaml
- name: Create Cron Jobs
hosts:
- localhost
become: true
vars:
cron_job_minute: '0'
cron_job_hour: '3'
cron_job_day: '*'
cron_job_month: '*'
cron_job_weekday: '*'
cron_job_command: '/usr/sbin/nginx -s reload'
tasks:
- name: Determine package manager based on OS
set_fact:
cron_package: "{{ 'cron' if ansible_distribution == 'Ubuntu' else 'cronie' if ansible_distribution in ['CentOS', 'RedHat', 'Amazon', 'Fedora'] }}"
- name: Install cron package
package:
name: "{{ cron_package }}"
state: present
when: cron_package != ""
- name: Add Nginx Reload Job
cron:
name: Reload nginx
job: "{{ cron_job_command }}"
state: present
minute: "{{ cron_job_minute }}"
hour: "{{ cron_job_hour }}"
day: "{{ cron_job_day }}"
month: "{{ cron_job_month }}"
weekday: "{{ cron_job_weekday }}"
- name: Get Crontab List
command: 'crontab -l'
register: crontab_list
- name: Print Crontab List
debug:
var: crontab_list.stdout_lines
[ansible@localhost playbook]$ ansible-playbook CreateCronJobs.yaml
PLAY [Create Cron Jobs] **************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Determine package manager based on OS] *****************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Install cron package] **********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Add Nginx Reload Job] **********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Get Crontab List] **************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Print Crontab List] ************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"crontab_list.stdout_lines": [
"# Edit this file to introduce tasks to be run by cron.",
"# ",
"# Each task to run has to be defined through a single line",
"# indicating with different fields when the task will be run",
"# and what command to run for the task",
"# ",
"# To define the time you can provide concrete values for",
"# minute (m), hour (h), day of month (dom), month (mon),",
"# and day of week (dow) or use '*' in these fields (for 'any').# ",
"# Notice that tasks will be started based on the cron's system",
"# daemon's notion of time and timezones.",
"# ",
"# Output of the crontab jobs (including errors) is sent through",
"# email to the user the crontab file belongs to (unless redirected).",
"# ",
"# For example, you can run a backup of all your user accounts",
"# at 5 a.m every week with:",
"# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/",
"# ",
"# For more information see the manual pages of crontab(5) and cron(8)",
"# ",
"# m h dom mon dow command",
"#Ansible: Reload nginx",
"0 3 * * * /usr/sbin/nginx -s reload"
]
}
PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0