On Thu, 15 Oct 2020 09:32:04 -0700 (PDT)
jean-christophe manciot <[email protected]> wrote:

> As you can see from example above, the <workstations> key is not always set.
> A major constraint is to avoid "json_query" as it cannot deal properly with 
> keys  containing a colon (as it may happen in my real context), last time I 
> checked
> I always get the error msg: 'dict object' has no attribute 'workstations'.

Add an empty list if the attribute is missing. For example

    - include_vars:
        file: domain_definition.yml
        name: domain_definition_raw
    - set_fact:
        domain_definition: "{{ domain_definition|default([]) +
                               [my_item] }}"
      loop: "{{ domain_definition_raw.domain_definition }}"
      vars:
        empty_workstations:
          workstations: []
        my_item: "{{ (item.workstations is defined)|
                     ternary(item, item|
                     combine(empty_workstations)) }}"
    - debug:
        var: domain_definition

give

  domain_definition:
  - cluster: cluster0
    name: server01
    port: '8070'
    workstations: []
  - cluster: cluster1
    name: server11
    port: '8080'
    workstations:
    - name: debian1
      revision: '2016-06-21'
      url: http://debian1.example.com:9911/2016-06-21
    - name: ubuntu1
      revision: '2017-08-16'
      url: http://ubuntu1.example.com:9912/2017-08-16
  - cluster: cluster2
    name: server12
    port: '8090'
    workstations:
    - name: debian2
      revision: '2018-04-15'
      url: http://debian2.example.com:9921/2018-04-15
    - name: ubuntu2
      revision: '2020-01-12'
      url: http://ubuntu2.example.com:9922/2020-01-12

This should solve the problems.

-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20201015204715.265df5cd%40gmail.com.

Attachment: pgpZAUjUzAphq.pgp
Description: OpenPGP digital signature

Reply via email to