Reputation: 125
I want to create a role to do a dynamic update of my Nagios configuration when I create a new VM.
So to do that, i already created a role which add a host definition at the end of my servers.cfg Nagios file, it looks like :
- name: Add {{ Host_Name }} in /etc/naemon/conf.d/hosts/servers.cfg
blockinfile:
dest: /etc/naemon/conf.d/hosts/servers.cfg
block: |
define host {
host_name {{ Host_Name }}
alias {{ Host_Name }}.uem.lan
address {{ Host_IP }}
use modele_host,host-pnp
}
marker: ""
backup: yes
It works well.
So now I would like to be able to add my "{{ Host_Name }}
" server directly at the end of lines of checks files.
Exemple : This is a check to monitore /data
partition :
define service {
service_description /data partition
host_name myserv1,myserv2,myserv3,myserv4,myserv5
use srv-pnp,modele_service_disk_linux_snmp
check_command check_snmp_storage!uem_snmp!/data$!90!95
}
And I would to add my "{{ Host_Name }}
" like that :
define service {
service_description /data partition
host_name myserv1,myserv2,myserv3,myserv4,myserv5,{{ Host_Name }}
use srv-pnp,modele_service_disk_linux_snmp
check_command check_snmp_storage!uem_snmp!/data$!90!95
}
Would anyone have a solution?
Thx :)
Upvotes: 1
Views: 1449
Reputation: 1326
My approach will be different, but it should working with Nagios 3 and 4.
Just edit your first Ansible task a little bit:
- name: Add {{ Host_Name }} in /etc/naemon/conf.d/hosts/servers.cfg
blockinfile:
dest: /etc/naemon/conf.d/hosts/servers.cfg
block: |
define host {
host_name {{ Host_Name }}
alias {{ Host_Name }}.uem.lan
address {{ Host_IP }}
hostgroup_name anything
use modele_host,host-pnp
}
marker: ""
backup: yes
Then replace your service definition with this:
define service {
service_description /data partition
hostgroup_name anything
use srv-pnp,modele_service_disk_linux_snmp
check_command check_snmp_storage!uem_snmp!/data$!90!95
}
Everytime when you add new host via Ansible to anything hostgroup and restart/reload Nagios service you will monitor your /data
partition without addition work.
Upvotes: 1
Reputation: 4168
You can give it a try with the following:
- name: 'LINEINFILE'
lineinfile:
path: 'service.cfg'
line: '{{item.line}}'
regexp: '{{item.regexp}}'
backrefs: True
loop:
- { line: '\1\2', regexp: '(\s*host_name.*),{{inventory_hostname}}(.*)' }
- { line: '\1,{{inventory_hostname}}', regexp: '(\s*host_name.*)' }
Pretty ugly, but idempotent.
Upvotes: 0