Posted on

using Ansible to maintain remote files and crontabs

Using Ansible to maintain the checks discussed in an earlier post and update the crontab.

This expects the servername(s) and the minutes for the crontab because I don’t want all cron’s running at the same time. I can then call this for each host, with the times for the crontab maintained in the config management tool.
eg

ansible-playbook remote-checks.yml --extra-vars "hosts=$1 minutes=$2"

And remote-checks.yml copies the check file for each server from a source location:

---
- hosts: '{{ hosts }}'
become_user: root
become: yes
vars:
servername: "{{ansible_hostname|lower}}"
minutes: "{{minutes}}"
tasks:
- name: copy script containing list of checks
copy: src=/home/ansible/source/checks/{{servername}}/10minutechecks.sh dest=/usr/local/nagios/passive/10minutechecks.sh owner=nagios group=nagios mode=0755
- name: "passive 10min checks"
cron:
name: "passice checks every 10 minutes"
user: nagios
minute: "{{minutes}}"
hour: "*"
job: "/usr/local/nagios/passive/10minutechecks.sh"