7

I have a task which uses with_subelements but it's terrible slow for big list (especially that most of the elements has duplicates and I don't need to run it multiple times for them).

So I'm looking for a way to optimize it somehow. I wish to get all unique elements from that list - let say settings in the example below:

inventory:

my_list:
  - { name: foo, settings: ['x', 'y', 'z'] }      
  - { name: bar, settings: ['x', 'y', 'q', 'w'] }

tasks:

- name: get all settings
  set_fact:
    all_settings="{{ my_list|map(attribute='settings')|list }}"

- name: show results
  debug:
    var=all_settings

results:

"var": {
        "all_settings": [
            [
                "x",
                "y",
                "z"
            ],
            [
                "x",
                "y",
                "q"
                "w"
            ]
        ]
    }

I stuck at this point. How can I combine those list together ?

I'm looking for a way to get ['x', 'y', 'z', 'q', 'w']

6

You need to use a union.

Off the top of my head:

all_settings="{{ foo|map(attribute='settings')|union(bar|map(attribute='settings')) }}"
|improve this answer|||||
  • The problem is that I can't loop on my_list elements manually. It's just an example with two elements for simplicity but normally I have much more. – pawel7318 Nov 17 '15 at 16:07
  • Perhaps you should explain what you're really trying to do. – Michael Hampton Nov 17 '15 at 16:11
0

What about this ?

- name: get all settings
  set_fact:
    all_settings="{{ my_list|map(attribute='settings')|list|flatten|unique }}"
|improve this answer|||||

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.