AnsiblePart2


Ansible playbook

This playbook gets the epel release repo, removes nginx if present,installs nginx and restart it via the notify event handler

playbook.yml

---
- hosts: webservers
  become: yes
  become_user: root

  tasks:
   - name: epel-release
     yum:
       name: epel-release
       state: present

   - name: remove nginx
     yum:
       name: nginx
       state: absent

   - name: nginx install
     yum:
       name: nginx
       state: latest
       
     notify: restart nginx
 
  handlers:
   - name: restart nginx
     service: 
       name: nginx
       state: restarted

Ansible roles

Create a role

sudo ansible-galaxy init base_httpd

this will create a folder structure like

base_httpd - defaults - files - handlers - meta - README.md - tasks - templates - tests - vars

tasks/main.yml

---
# tasks file for base_httpd
- name: install httpd 
  yum: 
    name: httpd
    state: latest

- name: start httpd
  service:
     name: httpd
     state: started

- name: copy index file
  copy: 
    src: /home/vagrant/roles/base_httpd/files/index.html 
    dest: /var/www/html
    mode: 0644
  notify: restart httpd

handlers/main.yml

---
- name: restart httpd
  become: yes
  become_user: root
  service: 
    name: httpd 
    state: restarted
 

files/index.html

<html>
<body>
<h1>
Hello World Test!
</h1>
</body>
</html>
sudo ansible-galaxy install cloudalchemy.prometheus

this is installed in /etc/ansible/roles

now create a playbook named ansible-playbook prometheus.yml to run this

---
hosts: webservers
roles:
 - cloudalchemy.prometheus
 
 
then run 

ansible-playbook prometheus.yml

this will install prometheus, test using curl

curl http://localhost:9090
sudo ansible-galaxy install cloudalchemy.alertmanager

now create a playbook named ansible-playbook alertmanager.yml to run this

---
hosts: webservers
roles:
 - cloudalchemy.alertmanager

then run

ansible-playbook alertmanager.yml

location is /etc/ansible/roles/cloudalchemy.alertmanager/templates these 2 templates need to be as below so then only alert manager service seems to start

Templates

create directories for alertmanager data storage

mkdir data
cd /daata
mkdir alertmanager

alertmanager.service.j2

[Unit]
Description=Prometheus Alertmanager
After=network-online.target
StartLimitInterval=0
StartLimitIntervalSec=0

[Service]
Type=simple
PIDFile=/var/run/alertmanager.pid
User=alertmanager
Group=alertmanager
ExecReload=/bin/kill -HUP $MAINPID
WorkingDirectory=/etc/alertmanager/
ExecStart=/usr/local/bin/alertmanager --config.file=/etc/alertmanager/alertmanager.yml --web.external-url http://localhost:9093 --storage.path=/data/alertmanager



SyslogIdentifier=alertmanager
Restart=always
RestartSec=5

CapabilityBoundingSet=CAP_SET_UID
LockPersonality=true
NoNewPrivileges=true
MemoryDenyWriteExecute=true
PrivateTmp=true
ProtectHome=true
ReadWriteDirectories=/var/lib/alertmanager
RemoveIPC=true
RestrictSUIDSGID=true

{% if alertmanager_systemd_version | int >= 232 %}
PrivateUsers=true
ProtectControlGroups=true
ProtectKernelModules=true
ProtectKernelTunables=yes
ProtectSystem=strict
{% else %}
ProtectSystem=full
{% endif %}

[Install]
WantedBy=multi-user.target
alertmanager.yml.j2
{{ ansible_managed | comment }}

global:
  resolve_timeout: {{ alertmanager_resolve_timeout | quote}}
{% for key, value in alertmanager_smtp.items() %}
  smtp_{{ key }}: {{ value | quote }}
{% endfor %}
{% if alertmanager_slack_api_url | length %}
  slack_api_url: {{ alertmanager_slack_api_url | quote }}
{% endif %}
{% if alertmanager_http_config | length %}
  http_config:
{% endif %}
{% for key, value in alertmanager_http_config.items() %}
    {{ key }}: {{ value | quote }}
{% endfor %}

templates:
- '{{ alertmanager_config_dir }}/templates/*.tmpl'
{% if alertmanager_receivers | length %}
receivers:
{{ alertmanager_receivers | to_nice_yaml(indent=2) }}
{% endif %}
{% if alertmanager_inhibit_rules | length %}
inhibit_rules:
{{ alertmanager_inhibit_rules | to_nice_yaml(indent=2) }}
{% endif %}
route:
  {{ alertmanager_route | to_nice_yaml(indent=2) | indent(2, False) }}
{% if alertmanager_child_routes | length %}
  routes:
  {{ alertmanager_child_routes | to_nice_yaml(indent=2) | indent(2, False) }}
{% endif %}

Test alert manager

sudo systemctl status alertmanager
curl http://localhost:9093

These writings represent my own personal views alone.
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.