I would like to install Nagios plugins based on server role. The solution below works but I’m sure there is a more elegant/recommended way to achieve the same – any help would be appreciated. I don’t like this explicit dictionary in merge statement and nested
for
loop.Pillar:
nagios: nrpe: allowed_hosts: 127.0.0.1 plugins: all: - nagios-plugins-disk - nagios-plugins-load - nagios-plugins-procs - nagios-plugins-swap db: - nagios-plugins-mysql
State:
{% set plugins = salt['grains.filter_by']({ 'db': { 'db': salt['pillar.get']('nagios:nrpe:plugins:db') } }, grain='role', merge={ 'all': salt['pillar.get']('nagios:nrpe:plugins:all') }) %} nrpe_plugins_all: pkg.installed: - pkgs: {%- for plugin in plugins.values() %} {%- for pkg in plugin %} - {{ pkg }} {%- endfor %} {%- endfor %}
Answer
A different approach would be to use grains to identify which hosts should have nagios and various nagios plugins:
Minion Grains (/etc/salt/grains)
roles:
- nagios
- nagios_mysql
State Top File
base:
'G@roles:nagios':
- nagios/npre_install_basic
'G@roles:nagios_mysql':
- nagios/npre_install_mysql
Nagios State Files
npre_install_basic:
pkg.installed:
- pkgs:
- nagios-plugins-disk
- nagios-plugins-load
- nagios-plugins-procs
- nagios-plugins-swap
- require:
- pkg: nagios
Nagios State Files
npre_install_mysql:
pkg.installed:
- pkgs:
- nagios-plugins-mysql
- require:
- pkg: nagios
Attribution
Source : Link , Question Author : HTF , Answer Author : chugadie