Have been using Ansible, but only scratching the surface. I’m new to this community.
I know you can schedule CRON jobs on remote systems with a playbook. I already do this. But is it possible to run an ansible-playbook command FROM the Ansible host itself, with Cron? For example, I can run the command (From the Ansible server) such as "ansible-playbook MyPlaybooknameHere.yml Works perfect. However, I want to run this on a schedule from the Ansible server via cron.
If you have AWX or AAP (AKA: Tower), you create execution schedules for jobs.
If you are executing from CLI on a Linux/Unix system, you should be able to create a cron job that runs on whatever schedule you need.
Instead of executing the cron job as you have called out, I would probably have the cron job call a bash script that sets up the working environment (python virtual environment, environment variables, working directory, etc.). At the end of the bash script, have it call the ansible-playbook.
Lastly, I’d setup the cron job to log all stdout and stderr to a log file so you can see the results of the daily executions.
Thanks for the feedback. Do you have an example bash script to do this? I’m new to this and a working example to work off of would be perfect for me to learn. Do you have anything like this you’d be willing to share?
This is a very rough, very incomplete bash script you could use as a start.
# Set your environment variables
export ANSIBLE_VAULT_PASSWORD_FILE=/home/dustin/venvs/IaC/my_ansible_vault_password.txt
export ANSIBLE_CONTROLLER=https://myawx.mydomain.com
export ANSIBLE_USERNAME=myansibleusername
# Change the current working directory to your playbook folder
cd /path/to/playbooks_directory
# Execute your playbook
/path/to/ansible-playbook /path/to/myplaybook.yml -i /path/to/inventory
Notes:
If you are unsure where your “ansible-playbook” executable is located, use the which command from the same environment you normally run your playbooks manually.
(IaC) dustin@mypc:~/venvs/IaC$ which ansible-playbook
/home/dustin/venvs/IaC/bin/ansible-playbook
(IaC) dustin@mypc:~/venvs/IaC$
Make sure to set the “execute” permission on the bash script file to avoid any permissions issues with crontab.
When starting from the beginning, it is very tempting to just put your passwords in clear text in your scripts. What I put in the bash example above hints at how you can put your necessary passwords in an ansible-vault. Definitely put the effort into understanding how ansible-vault works, and find a way to keep from putting your passwords into files as clear text.
#!/bin/bash
# Change the current working directory to your playbook folder
cd /etc/ansible/playbooks
# Execute your playbook
/usr/bin/ansible-playbook /etc/ansible/playbooks/qualify_trunk.yml
Then changed the permissions to the appropriate execute permissions, etc. I can run the file directly on the Ansible host, and it works fine. But it does not work via a cronjob. Cronjob example below.
Is the server time correct. Maybe it is running, but not at the time you think it is.
Check Server Time
Your cron says it will run 44 minutes after noon, local time to the server.
Use the date command to validate the time on the server.
[my_server~]$date
Tue Feb 13 14:45:37 EST 2024
[my_server~]$
Are their errors when you run your command manually.
Run Manually and Look For Errors
Simply copy your entire cron command, excluding the schedule 44 12 * * *, and paste it into the CLI terminal.
See if there are any errors or helpful information displayed.
Write all STDOUT and STDERR to a log file and look for errors there.
Write all output to log
Instead of dumping all output to null with >/dev/null 2>&1, create a log file to contain your cron results and dump all logs there.
Then, you can check that log to see if it ever executed and if it had any errors during execution.
I’ll give that a try as well. having trouble getting it to run, and I have a feeling it is because of changing to the ansible folder. I installed ansible using pip, so not sure how to get the location properly. using “which ansible-playbook” gives me a location, but that does not seem to work.
Thank you for your help! It is working now, and I am able to have it log properly as well. However the script does not get written to if I do it from the cronjob. If I direct the output to a log file from inside the script, it properly outputs to the log file.
This has been very helpful and a learning exercise for me. Thank you!