# Delegated cronjobs with time vars per target host

**URL:** <https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 11:29pm UTC](https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929 "2014-04-02T23:29:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Bozic](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Martin\_Bozic](https://forum.ansible.com/u/Martin_Bozic)\
**Post date:** [April 2, 2014, 11:29pm UTC](https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929/1 "2014-04-02T23:29:09Z")

</div>

Hi,

I want to dynamically every new host to the backup server with rsnapshot. I have no problems putting SSH keys on backup target hosts and putting a template and exclude config for the target host into /etc/rsnapshot.d on backup host.

The problem is when I try to add the rsnapshot cronjob for the new host. And here's the trick - I'm trying to add different times for each host because I don't want them to all hit the backup disk at the same time. So I figured I could put several cron time vars for each host in host\_vars/ directory. Here's an example of what I'm trying to pull-off with cron module:

FILE: roles/common/backup.yml

---

<div class="post-metadata">

**Author:** ![Brent\_Langston](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Brent\_Langston](https://forum.ansible.com/u/Brent_Langston)\
**Post date:** [April 4, 2014, 3:39am UTC](https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929/2 "2014-04-04T03:39:04Z")

</div>

I did this a while back, thinking I’d replace it with something more elegant when I have time… honestly, it’s been working so well, I haven’t thought about it again, until you reminded me.

I have a script saved in my ansible git repo: $GIT/ansible/bin/cron\_scheduler.sh

#!/bin/bash

PATH=/bin:/usr/bin

helptext() {  
echo “Usage: $0 -i ip\_address -f frequency” \>&2  
echo “Example: $0 -i 10.10.5.160 -f 15 # run once every 15 minutes” \>&2  
exit 1  
}

if [“$#” -ne 4] || [“$1” = “-h”] || [“$1” = “–help”]; then  
helptext  
fi

while getopts i:f: option  
do  
case “${option}”  
in  
i) ip\_address=${OPTARG};;  
f) frequency=${OPTARG};;  
\*) helptext;;  
esac  
done

decimal=$(echo $ip\_address | sed ‘s/./+/g’ | bc)  
ip\_rand=$(expr $decimal % $frequency)

x=$ip\_rand  
declare -a minutes=$ip\_rand

count=0  
while [$x -lt 60]  
do  
next=$(expr “${minutes[$count]}” + $frequency)  
if [$next -lt 60] ; then  
minutes=(“${minutes[@]}” “$next”)  
fi  
(( count++ ))  
x=$next  
done

echo -n ${minutes[@]} | tr " " ,

So running this:

cron\_scheduler.sh -i 10.10.5.160 -f 15

results in this output:  
5,20,35,50  
(every 15 minutes)

So just decide how frequently you want to schedule the task, and pass in the IP address. voila.

- name: pick a minute to run example  
script: bin/cron\_scheduler.sh -i {{ ansible\_default\_ipv4.address }} -f 10  
register: cron\_minutes

- name: configure example  
template: src=example/example.cron dest=/etc/cron.d/example.cron owner=root group=root mode=0644

and the template would have something like this:

{{cron\_minutes.stdout}} \* \* \* \* root /usr/local/bin/example

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [April 4, 2014, 3:49am UTC](https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929/3 "2014-04-04T03:49:27Z")

</div>

Where are you using the rsnap\_daily\_hour variable? The task you pasted doesn’t have it included.

The vars file you included is host specific for ‘newhost’, if you are trying to use those variables in a play involving the backuphost (not a delegated\_to task), you’ll either need to use hostvars or restructure your variables in a different manner.

So, using hostvars, accessing rsnap\_daily\_hour in a play on backuphost would be {{ hostvars[‘newhost’][‘rsnap\_daily\_hour’] }}.

If it was me, I’d use group\_vars with separate groups for each time slot, and then make the backuphost a member of each time slot group. That may or may not scale well, but I try to avoid using hostvars if at all possible. I’d change my mind if I was in an environment absolutely destined to remain small & stable of course.

Brian

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 4, 2014, 3:08pm UTC](https://forum.ansible.com/t/delegated-cronjobs-with-time-vars-per-target-host/14929/4 "2014-04-04T15:08:13Z")

</div>

There have been pull requests for a random Jinja2 filter, but they were pretty complicated in terms of syntax (i.e. not pretty syntax wise) and I wanted to see them pared down.
