2

I have a weird behavior on a crontab job I'm trying to run using Ansible's playbook.

First of all, I have similar crontab's lines using Ansible playbooks that works perfectly, so the issue is either this particular playbook or something is missing in the crontab options maybe.

Anyway, the playbook does the following:

1.- Ubuntu server (Local-host), which is the one running the playbook, compress "Documents" folder into a .zip file and stores it in /home/user.

2.- A ssh connection to NAS server (Remote-host) to Copy .zipped "Documents" folder to NAS and then I change the name of the .zip file using a local naming convention.

3.- Clean up .zip files in Ubuntu.

So, like I say in the title, when I run the playbook from terminal using:

$ sudo ansible-playbook ubuntu2nasBackup.yml

The playbook works perfectly: zip file creation -> copied via ssh to remote host -> clean up .zip file.

When I try to add to crontab job (root crontab I must add), the bullet #1 above is created, but then the zip file is not copied to remote host.

$ sudo crontab -e
22 20 * * * /usr/bin/ansible-playbook /home/user/Documents/ansible101/ubuntu2nasBackup.yml

I have tried to search (unsuccessfully) a way to t-shoot and have some feedback on the error or time-out that must be happening, but nothing seems to work.

Is there anything I'm missing? Is there any tip I can follow?

If the playbook is necessary to reveal, let me know, but like I said if I run thos from terminal, it works.

2
  • See complete example for how to run the Ansible playbook in cron. Commented May 9, 2022 at 1:05
  • Hi Vladimir, thanks for the info. Although I checked it out, I am not seeing anything that can help understand why is not working from crontab this particular playbook Commented May 10, 2022 at 2:18

2 Answers 2

2

I sort it out!

First of all, I had to figure out how to generate some output from crontab job into a .log or .txt file, in order to have some clue on why this odd behavior.

For that I used this answer.

So basically, I added this line in the crontab document:

* * * * * /usr/bin/ansible-playbook playbook.yml >> /PATH/TO/File.log 2>&1

Checking the output in File.log, I figured out that when I ran the playbook from the user, I also did it from a testing folder using an ansible.cfg within the test folder, which had a couple of properties in the ssh_connection section to achieve a correct ssh communication with remote-server.

When running from root's crontab, the ansible.cfg used is the one stored in /etc/ansible/ folder.

Basically, ansible.cfg follows this order:

The configuration file Changes can be made and used in a configuration file which will be searched for in the following order:

ANSIBLE_CONFIG (environment variable if set)

ansible.cfg (in the current directory)

~/.ansible.cfg (in the home directory)

/etc/ansible/ansible.cfg

Anyway, when I added the few extra lines missing in the /etc/ansible/ansible.cfg file, the cron job worked like a charm.

1
  • I'm having the same exact problem. what lines did you add? nvm, I went with an additional solution. Commented Sep 15, 2022 at 13:27
1

I was having the same exact problem. My yml files were working perfect and there was no ansible.cfg in that folder. The command I used for running the playbook was like:

ansible-playbook -i hosts.ini -l server_name my_file.yml

I don't know what to add to cfg file, so I did something a little tricky. Even though I gave the exact file paths in my crontab file, I added a cd /my_yml_location/ at the beginning of the script. Luckily it is working this way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .