gurbelunder
gurbelunder

Reputation: 305

Combine two lists in Ansible when one list could be empty

actually I've a problem by compining two lists in Ansible to one new in case of one list could be empty or not.

EDIT:

One list is defined in defaults

standardvolumes: '/usr/share/zoneinfo:/usr/share/zoneinfo:ro'

and the other list could be defined in group_vars.yml for the hostgroup.

volumes:
    - '/usr/data:/data'
    - '/usr/data2:/data2'
    - '/usr/data3:/data3'

At the task I comine the lists:

  - name: Combine volume lists
    set_fact:
      volumestostart: '{{ standardvolumes|default([]) + volumes|default([]) }}'

In play I get this error:

TASK [do-docker-deployment : Combine volume lists] *****************************
fatal: [shs_de_postd_server1]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ standardvolumes|default([]) + volumes|default([]) }}): coercing to Unicode: need string or buffer, list found"}

So I've tried to redefine my defaults list:

standardvolumes: 
  - '/usr/share/zoneinfo:/usr/share/zoneinfo:ro'

But then I get a docker format error:

failed: [shs_de_postd_server1] (item={u'key': u'qit', u'value': {u'cgrouptype': u'blech', u'nexususer': u'cbs-qit-user', u'notstartable': u'no', u'nexuspassword': u'48vhw63u', u'nexusport': u'8191', u'nexuspath': u'ftg/postd-server', u'graylogip': u'tcp://10.20.30.40:12201'}}) => {"ansible_loop_var": "item", "changed": false, "item": {"key": "qit", "value": {"cgrouptype": "blech", "graylogip": "tcp://10.20.30.40:12201", "nexuspassword": "48vhw63u", "nexuspath": "ftg/postd-server", "nexusport": "8191", "nexususer": "cbs-qit-user", "notstartable": "no"}}, "msg": "Found invalid volumes mode: ro']"}

The lists are docker volumes for a container which I will start with my role.

Anybody good ideas?

Thanks and regards, David

Upvotes: 2

Views: 5612

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68074

Try this

  volumes: "{{ volumes|default([]) + standardvolumes|default([]) }}"

For example

    - set_fact:
        list_A: "{{ list_A|default([]) + list_B|default([]) }}"
    - debug:
        var: list_A

give

  list_A: []

Upvotes: 6

Related Questions