-3

I have been trying to configure a cron job to run my ansible playbook every hour. I could not find any relevant examples on how to start the configuration . I have tried the below task in a separate ansible playbook and it shows the below output for crontab -l, but not execution seems to be happening. Help will be high appreciated.

root@ubuntu:/etc/ansible/# crontab -l
                         57 10 * * * ansible-playbook crontest.yaml
   - name: Run an ansible playbook"
     cron:
        name: "Run play"
        minute: "57"
        hour: "10"
        job: "ansible-playbook crontest.yaml"

Cron Logs:

Nov 24 07:35:01 ABC30VDEF290021 CRON[13951]: (root) CMD (echo "testing" > /etc/ansible/automation/logs/test.txt)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13948]: (root) CMD (                /usr/bin/ansible-playbook /etc/ansible/automation/crontest.yml)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13949]: (root) CMD (/usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml)

Crontab -e

#Ansible: Run play
*/1 * * * * /bin/sh -c '. ~/.profile; /usr/local/bin/ansible-playbook /etc/ansible/automation/crontest.yml

*/1 * * * * echo "testing" > /etc/ansible/automation/logs/test.txt

35 7 * * * /usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml
Vinny
  • 302
  • 2
  • 11
  • 4
    Does [how to get `cron` to call in the correct paths](https://stackoverflow.com/questions/2388087/) answer your question? According it you may try with `/usr/bin/ansible-playbook` as well `/path/to/crontest.yaml`. – U880D Oct 28 '21 at 09:19
  • 2
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 29 '21 at 20:59
  • @U880D, Sorry for the delay in my response. Your solution worked for a sample playbook, but it still doesnot work for my main playbook. I am adding the cron logs to show that the command is getting executed, but the playbook is not running. The main playbook which i would like to execute is **main.yaml**. But that is not getting executed and other 2 crons are getting executed – Vinny Nov 24 '21 at 06:38

1 Answers1

3

The following code will run the script every 30th minute of every hour

- name: Run CRON job to load data at every 30th minute of every hour.
  become: yes
  become_method: sudo
  cron:
    name: "load_data"
    user: "root"
    weekday: "*"
    minute: "30"
    hour: "*"
    job: "python3 /path/to/my/script/loadScript.py > /home/ec2-user/loadData_result 2>&1"
    state: present
Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42