sudoedit.com!

Run it later with atd

Schedule Ansible playbook to run later with atd

Just a quick note today. I used Ansible as an example for this but you can use any command really.

If you have a need to run an Ansible playbook at a specific time but do not need to schedule it to run on a reoccurring basis. You can use the at daemon to schedule your playbook to run “at” a specific time or date.

Use the EOF (Check for end of file condition) to make at schedule jobs non-interactively:

For Example if you had a playbook you wanted to run tomorrow at 6am you could have script set up like this and then check the play.result file later to make sure it ran ok.

The script

#!/bin/bash
## schedule ansible playbook to run tommorrow at 6AM
/usr/bin/at 0600 tomorrow <<EOF
ansible-playbook <playbook_name>.yml -i inventory_file >> play.result
EOF
## Schedule another playbook to run on Sunday at 11PM
/usr/bin/at 2300 sunday <<EOF
ansible-playbook <playbook_name>.yml -i inventory_file >> play2.result
EOF

Why not just schedule it interactively?

Well you can and usually that’s fine. I’m just demonstrating how to use at in a script.

Where this comes in handy is if you have several “one off” changes that need to be pushed over a weekend at different times and you keep those change times in an external source that you can draw from. Something as complex as Service Now or something as simple as a csv file that has the schedule built into it.

Having a script build out the schedule ensures the time and date will be correct and it allows you to create many jobs simultaneously.


If you found this useful please support the blog.

Fastmail

I use Fastmail to host my email for the blog. If you follow the link from this page you'll get a 10% discount and I'll get a little bit of break on my costs as well. It's a win win.


Backblaze

Backblaze is a cloud backup solution for Mac and Windows desktops. I use it on my home computers, and if you sign up using the link on this page you get a free month of service through backblaze, and so do I. If you're looking for a good backup solution give them a try!

Thanks!

Luke