On Mon, 24 Feb 2020 05:06:55 -0800 (PST)
Sivaraman P <[email protected]> wrote:

>    - name: get variable
>      shell: cat file #this file contain the list of items that I need to run 
> the command
>      register: out1
> 
>    - name: export
>      shell: command {{item}}
>      register: out2 #This contains results of each item from out1.
>      with_items: "{{out1.stdout_lines}}"
> 
> ... get the stdout of each item result and I have to store as value for that 
> item.

Registered variable "out2" keeps in the list "results" both the keys(items)
and values(stdout). Let's create two lists of keys and values, respectively.
Then use the filters "dict" and "zip" to combine them. For example

    - set_fact:
        out2_dict: "{{ dict(my_keys|zip(my_values)) }}"
      vars:
        my_keys: "{{ out2.results|json_query('[].item') }}"
        my_values: "{{ out2.results|json_query('[].stdout') }}"

This can be simplified to single json_query

    - set_fact:
        out2_dict: "{{ dict(out2.results|
                            json_query('[].[item, stdout]')) }}"

See "Combining items from multiple lists". It's frequently repeating pattern.
https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest

HTH,

        -vlado

-- 
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/20200224174016.37097bc0%40gmail.com.

Attachment: pgpI4Ds7LWf5H.pgp
Description: OpenPGP digital signature

Reply via email to