ansible-role-nagios/tasks/configure.yml

139 lines
3.9 KiB
YAML

---
- name: Check if docker group exists
shell: grep docker /etc/groups
register: docker_group
- name: Add nagios to docker group if it exists
user:
name: nagios
append: true
groups:
- docker
when: docker_group.rc == 0
- name: Ensure folder for Nagios configurations exists
file:
path: /etc/nagios4/conf.d
state: directory
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
- name: Ensure nagios is configured
lineinfile:
path: /etc/nagios4/nagios.cfg
line: "{{ item.key }}={{ item.value }}"
regexp: "^{{ item.key }}="
backup: yes
create: yes
#validate: '/usr/sbin/nagios4 --verify-config %s'
loop: "{{ lookup('dict', nagios4_config) }}"
notify: Restart Nagios4
- name: Ensure nagios CGI is configured
lineinfile:
path: /etc/nagios4/cgi.cfg
line: "{{ item.key }}={{ item.value }}"
regexp: "^{{ item.key }}="
backup: yes
create: yes
#validate: '/usr/sbin/nagios4 --verify-config %s'
loop: "{{ lookup('dict', cgi_config) }}"
notify: Restart Nagios4
- name: Ensure commands are configured
template:
dest: /etc/nagios4/objects/commands.cfg
src: templates/commands.cfg.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
- name: Ensure sudo commands are added
community.general.sudoers:
name: "allow_sudo_{{ item.command_name }}"
state: present
user: nagios
commands: "{{ item.command_line.split(' ')[1] | replace('$USER1$', '/usr/lib/nagios/plugins') }}"
nopassword: true
loop: "{{ commands }}"
when: "'sudo ' in item.command_line"
- name: Ensure hostgroups are configured
template:
dest: /etc/nagios4/conf.d/hostgroups.cfg
src: templates/hostgroups.cfg.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
- name: Ensure contacts and contactgroups are configured
template:
dest: /etc/nagios4/objects/contacts.cfg
src: templates/contacts.cfg.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
- name: Ensure hosts are configured
template:
dest: "/etc/nagios4/conf.d/{{ item.host_name }}.cfg"
src: templates/host_template.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
with_items: "{{ monitored_hosts }}"
- name: Ensure templates are configured
template:
dest: "/etc/nagios4/objects/templates.cfg"
src: templates/templates_template.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
- name: Ensure timeperiods are configured
template:
dest: "/etc/nagios4/objects/timeperiods.cfg"
src: templates/timeperiods.cfg.j2
owner: "{{ nagios_user }}"
group: "{{ nagios_group }}"
mode: 0660
backup: yes
#validate: '/usr/sbin/nagios4 --verify-config /etc/nagios4/nagios.cfg #%s'
notify: Restart Nagios4
- name: Ensure nagios is not loading the locahost.cfg file
lineinfile:
path: /etc/nagios4/nagios.cfg
line: "#cfg_file=/etc/nagios4/objects/localhost.cfg"
regexp: "^cfg_file=/etc/nagios4/objects/localhost.cfg$"
backup: yes
create: yes
#validate: '/usr/sbin/nagios4 --verify-config %s'
notify: Restart Nagios4
- name: Ensure locahost.cfg file is absent
file:
path: /etc/nagios4/objects/localhost.cfg
state: absent
- name: Ensure Nagios service is enabled and started
systemd:
name: nagios4
enabled: yes
state: started