Below is the main.yml
---
# your_playbook.yml
- hosts: all
become: yes
gather_facts: no
become_method: sudo
roles:
- include_role:
name: cron_job
vars:
cron_jobs:
- script_path: "/export/storage_mount.sh"
script_arguments: "action"
- script_path: "/export/service_status.sh"
script_arguments: ""
cron role:
---
- name: Create cron job
ansible.builtin.cron:
name: "Run script every 5 minutes"
minute: "5"
job: "/bin/bash {{ item.script_path }} {{ item.script_arguments }}"
state: present
loop: "{{ cron_jobs }}"
Role running output:
changed: [host01] => (item={'script_path': '/export/apps/scripts/storage_mount.sh', 'script_arguments': 'action'})
changed: [host01] => (item={'script_path': '/export/apps/scripts/service_status.sh', 'script_arguments': ''})
It only creates one entry not two entries in crontab
#Ansible: Run script every 5 minutes
5 * * * * /bin/bash /export/service_status.sh
How to fix this?
How to loop on Ansible
cron
module?
This is probably because you've used the same parameter name: "Run script every 5 minutes"
. This will make the same entry twice whereby the second run wins.
You could
name: "Job {{ ansible_loop.index }} - Run script every 5 minutes"
from Extended loop variablesname: "Run {{ item.script_path | basename }} every 5 minutes"
with basename
filter – get a path’s base name