1
votes

I have an ansible inventory with groups as follows:

  +hosts/
  +all/
  +group_vars/
    - linux.yml
    - webserver.yml
    - dbserver.yml
...

And I have a playbook that sets monitoring for hosts; and the kind of monitoring is done by plugins. So in each group y set a list monitoring_plugins that contain the plugin to be able t monitor each service. Inside each group yml I try to "append" to the list:

monitoring_plugins: {{ monitoring_plugins|default([]) + [ 'whatever_plugin_correspond_to_group' ] }}

But it doesn't work as expected, being expected that if a host belongs to more than one group, it should have the plugins corresponding to those groups.

Is there a way to acommplish this?

Thanks in advance.

1

1 Answers

2
votes

What you're describing should work as expected from within a task, but you cannot have executable code in a vars or group_vars yaml or json file -- those are static

So you will need to set a distinct name at the low level and then roll them up at the top level:

group_vars/
  dbserver.yml  # sets monitoring_plugins_dbserver: ["a", "b"]
  linux.yml     # sets monitoring_plugins_linux: ["c", "d"]

and then in your tasks, something like this (but be forewarned I haven't tested this specific example):

- set_fact:
    monitoring_plugins: >-
      {% set results = [] %}
      {% for g in groups.keys() %}
      {% set _ = results.extend(vars['monitoring_plugins_'+g]|d([])) %}
      {% endif %}
      {{ results }}