Playing with playbooks

If there is a pinnacle topic among others in the systems administration’s field, it is automation, and nowadays ansible is a synonym of automation.

I’m nothing but a newbie regarding ansible, but it doesn’t mean I’m not aware of its potential and how it can help sysadmins on repetitive tasks.

This is a world divided (not taking into account containers) on physical servers, virtual machines, BSD jails, vzcontainers and a whole universe of devices where you can ssh in, so the variety of systems where you have to perform some tasks is huge. And the ease of creating new virtual environments makes deployments of projects spread over several VMs.

Now a simple task can mean repeating the same command or copying the same file or restarting the same service over multiple VMs. That was my case today.

I needed to copy a nagios check to ten identical VMs, include a line in the nrpe.cfg file and restart the nrpe service. I had all in place to use ansible, this wasn’t the first time on that deployment, but I have never done those tasks so I researched a bit and found out the lineInfile module was what I needed to check for the line, and insert it if needed, copy module to transfer the check and service module to restart the service.

Putting all together ended up in a simple but efficient playbook:

- name: Add nrpe check
  hosts: all
  tasks:
  - name: Copy nagios check
    copy: 
    src: /usr/lib64/nagios/plugins/check_procr.sh
      dest: /usr/lib64/nagios/plugins/check_procr.sh
      owner: root
      group: root
      mode: '0755'
  - name: Add check_command to nrpe.cfg
      lineinfile:
      path: /etc/nagios/nrpe.cfg
      regexp: '^check_command\[check_procr\]='
      line: 'check_command[check_procr]=/usr/lib64/nagios/plugins/check_procr.sh $ARG1$ $ARG2$'
      register: nrpe_cfg
  - name: Restart nrpe
      service:
      name: nrpe
      state: restarted
      when: nrpe_cfg.changed

I haven’t use register and when before, but I saw them in an example and looked pretty straightforward to me.

And the best of all, it worked! at that point the next task was adding the same service to ten hosts on nagios using my old colleague GroundWork Monitoring Architect.