Hello Guys,
I was working on a writing a simple playbook of making sure that the nginx service should be up and running. while writing i realise that just making sure that service is up and running is not enough so I have added one more module which make sure firewall port 80 is opened permanently I have added the same. I have also added handlers to reload the firewall to make sure changes are permanent.
Then I have realise I need to also make sure that if the Linux host is unreachable then also i should get an alert stating server is unreachable
i know its a limited use case but i am using it with EDA (event driven ansible) .
Below is the playbook i have came up with
---
- name: Restarting the Nginx if port 80 is down
hosts: all
gather_facts: false
force_handlers: true
ignore_unreachable: true
tasks:
- name: Ping the host
ansible.builtin.ping:
register: ping_result
- name: Ping is not successful
ansible.builtin.debug:
msg: "{{inventory_hostname}} is not from Ansible Controller...!"
when: ping_result.unreachable is defined
- name: Add unreachable hosts to a list
ansible.builtin.set_fact:
unreachable_hosts: "{{ unreachable_hosts | default([]) + [inventory_hostname] }}"
when: ping_result.unreachable is defined
- name: Firewalld |Open port 80 using firewalld
ansible.posix.firewalld:
port: 80/tcp
permanent: yes
state: enabled
notify: Reload firewalld to apply changes
when: ping_result.unreachable is not defined
- name: Make sure service is up and running | Nginx service
ansible.builtin.service:
name: nginx
state: started
become: true
register: nginx_restart
when: ping_result.unreachable is not defined
- name: Genrate Email content to Send in Email | Server is unreachable
ansible.builtin.template:
src: email_alert.html.j2
dest: /tmp/alert_email.html
run_once: true
delegate_to: 127.0.0.1
when: ping_result.unreachable is defined
- name: Email Alert if fail server is unreachable
when: ping_result.unreachable is defined
mail:
host: smtp.gmail.com
port: 587
subtype: html
to:
- "nrathi@redhat.com"
subject: "Alert: Host not reachable on SSH {{ inventory_hostname }}"
body: "{{ lookup('file', '/tmp/alert_email.html') }}"
username: "abc@gmail.com"
password: "your_secure_password"
run_once: true
delegate_to: 127.0.0.1
changed_when: True
handlers:
- name: Reload firewalld | To apply changes
ansible.builtin.service:
name: firewalld
state: reloaded
below is the html template i am using
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Host Down</title>
<style>
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
background-color: #FF0000;
margin: 0 auto;
}
th, td {
padding: 12px;
border: 1px solid #ddd;
text-align: left;
}
th {
background-color: #17469E;
color: white;
text-transform: uppercase;
font-size: 14px;
}
td {
background-color: #f9f9f9;
font-size: 14px;
}
td.label {
font-weight: bold;
background-color: #e0e0e0;
}
.title {
text-align: center;
font-size: 18px;
font-weight: bold;
margin-bottom: 20px;
color: #333;
}
.container {
width: 80%;
margin: 0 auto;
}
</style>
</head>
<body>
<p>Dear Team,</p>
<p>This is an automated alert to inform you:</p>
<p>Host isnot reachable from ansible on required ssh or WinRM </p>
<table>
<tr>
<th>Host</th>
</tr>
<tr>
<td>{{ inventory_hostname }}</td>
</tr>
</table>
<p>Best regards,<br/>abc@gmail.com</p>
</body>
</html>
#ansible-playbook -i inventory restart_nginx.yml