0

An ansible-sensu-plugin-install role has been created in order to install sensu-plugins using this code:

- name: be sure sensu-plugins-{{ item.name }} is installed
  command: sensu-install -p {{ item.name }}:{{ item.version }}
  when: "'sensu-plugins-{{ item.name }} ({{ item.version }})' not in installed_plugins.stdout"
  with_items: "{{ sensu_plugin_install }}" 

There a two other roles that require sensu-plugins. The first role requires the mailer plugin:

sensu_plugin_install:
  - name: mailer
    version: 1.0.0

and the second one requires multiple plugins:

sensu_plugin_install:
  - name: cpu-checks
    version: 1.0.0
  - name: disk-checks
    version: 2.0.1
  - name: http
    version: 1.0.0
  - name: memory-checks
    version: 1.0.2
  - name: ntp
    version: 1.0.0

Role one and two have been included in one playbook.

Current outcome

Either the mailer defined in role one or the plugins defined in role two will be installed. Commenting out one of the sensu_plugin_install declarations will install plugins related to role one or two, e.g. only the mailer plugin will be installed:

TASK [030.sensu-plugin-install : be sure sensu-plugins-{{ item.name }} is
    installed] ***
    changed: [host.example.com] => (item={u'version': u'1.0.0', u'name':
    u'mailer'}) => {"changed": true, 

if the sensu_plugin_install of the second role has been commented out.

Expected outcome

The expected outcome is that all plugins will be installed, i.e. six in this case instead of one, i.e. mailer or five.

Discussion

It looks like that the arrays will not be merged as either one or five plugins will be installed instead of six and one of the two needs to be commented out in order to force the installation of a plugin.

One option to solve this issue is to merge these arrays, i.e. sensu_plugin_install + sensu_plugin_install. For example, in Puppet it is possible to merge arrays using hiera_array. How could this be done in Ansible?

Using Two different variables in order to solve the issue is not an option as the functionality of one role is used and it is not preferred to add another variable to this role if another role would like to install sensu plugins.

  • @MichaelHampton I would like to see that it is possible to do a kind of sensu_plugin_install + sensu_plugin_install instead of having two different variables. – 030 Sep 27 '16 at 7:48
  • 1
    I don't know of any way to do that. And it's probably not very clean to try. – Michael Hampton Sep 27 '16 at 7:49
  • @MichaelHampton In puppet one could do merge array and merge hash. – 030 Sep 27 '16 at 7:49
  • @techraf I have edited the question – 030 Sep 29 '16 at 8:13
0

Try this role from ansible galaxy https://galaxy.ansible.com/eugene0707/merge_custom_vars/

I did it some time ago for same reason. It can deep merge dictionaries from several vars files and set custom precedence of merging.

This role can work under Ansible 2.0+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.