Pages

Monday, February 22, 2016

Ansible Monitor-Alert

Share it Please
At an infra and application level we need to have several monitoring metrics configured right from CPU, memory to application leavel such as Heap and mumber of Database connections. Also, from an infrastructure automation point of view, you need to make sure you start a strong feedback loop by integrating automation with your monitoring and alerting system or systems through constant reporting.

In this article we will how we can configure monitor and alert when a playbook ran. Lets write a sample playbook as,

---
- hosts: cent

  tasks:
    - name: run echo Command
      command: /bin/echo Hello Sample PlayBook

    - name: get the IP address
      shell: hostname
      register: host_name

    - name: Send Mail Alert
      local_action: mail
                    host="127.0.0.1"
                    subject="[Ansible] Testing Mail"
                    body="Hello the Play book ran successfully on {{ host_name.stdout }}"
                    to="jagadesh.manchala@gmail.com"
                    from="root"     

In the above playbook we have multiple tasks as to run a command on remote machine and also get the hostname of the remote machine.

The last one is the important one as we are sending the mail. We have used the local_action element which will make to exeute the mail command on the local machine. We can also configure our playbook in such as to execute the mail command based on the response of the commands above. I just got the hostname and added that to the mail Subject. Now once you run the playbook we can see,


[root@vx111a mail]# ansible-playbook sample-playbook.yml

PLAY [cent] *******************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [run echo Command] ******************************************************
changed: [172.16.202.96]

TASK: [get the IP address] ****************************************************
changed: [172.16.202.96]

TASK: [Send Mail Alert] *******************************************************
ok: [172.16.202.96 -> 127.0.0.1]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=4    changed=2    unreachable=0    failed=0  

Now if we check our mail account we can see some thing like this,
We received a mail once the playbook is ran. We can add various logic to extract details from remote machine and add them to our alerting mechanism. In the next article we will see how we can use other monitoring and alerting methods provided by Ansible.

No comments :

Post a Comment