大网数据广告活动
服务器运维问答

How to do an array or hash merge in Ansible?

· 52CE 我爱测

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 }} whe…

问题原文

原帖链接 · 030 · CC BY-SA 3.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.

已采纳解答

原帖链接 · 030 · CC BY-SA 4.0

First of all I had to define two different variables rather than using the same one, e.g. sensu_plugin_install. After giving the second dict a different name like sensu_plugin_install_other, the two arrays were able to merge:

 with_items: "{{ sensu_plugin_install }} + {{ sensu_plugin_install_other }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。