# Create Key value pair

**URL:** <https://forum.ansible.com/t/create-key-value-pair/32315>\
**Category:** Ansible Project\
**Created:** [February 24, 2020, 1:06pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315 "2020-02-24T13:06:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 24, 2020, 1:06pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/1 "2020-02-24T13:06:55Z")

</div>

I have list of items to be executed in the command and I have to save the result in stdout for each item to the same item.

Ex:  
hosts: dev  
tasks:  
&nbsp;&nbsp;&nbsp;- name: get variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: cat file #this file contain the list of items that I need to run the command  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out1

&nbsp;&nbsp;&nbsp;- name: export  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: command {{item}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out2 #This contains results of each item from out1.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{out1.stdout\_lines}}"

The question is I have to get the stdout of each item result and I have to store as value for that item.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 24, 2020, 1:13pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/2 "2020-02-24T13:13:09Z")

</div>

My question would be what the actual tasks are and why there are stored in a file on the target.  
It would also help to see the format of this file.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 24, 2020, 1:16pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/3 "2020-02-24T13:16:02Z")

</div>

Thank you for your response. I just Showed it for example only… Actually I am getting those keys from register variable(out1)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 24, 2020, 1:42pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/4 "2020-02-24T13:42:21Z")

</div>

Hi Sivaraman

Could you please state clearly:

- What goal you are trying to achieve.  
- How you are doing this.  
- What problems you encounter.  
- Which command did you run, and what actual output did you get from  
that (copied as plain text - not as images, attachments, or  
screenshots)  
- What the relevant tasks/playbooks/code/variables look like.

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 24, 2020, 1:58pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/5 "2020-02-24T13:58:47Z")

</div>

Hi, I just want to

Ex:  
hosts: dev  
tasks:

- name: get variable  
shell: some command  
register: out1 # i want to use stdout\_lines of these out1 in my next task

- name: export  
shell: command {{item}}  
register: out2 # i want to use stdout kines of each item in my next task  
with\_items: “{{out1.stdout\_lines}}”

#After this I tried to combine out1 as key and out2 as value to the key, But I am getting error

- set\_fact:  
my\_env: “{{my\_env | default ({}) | combine ({{item.0}}: {{item.1}})}}”  
with\_nested:
- “{{out1.stdout\_lines}}”
- “{{out2 | json\_query('results[\*].stdout\_lines}}”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 24, 2020, 2:03pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/6 "2020-02-24T14:03:59Z")

</div>

> Hi, I just want to
> 
> Ex:  
> hosts: dev  
> tasks:  
> - name: get variable  
> shell: some command  
> register: out1 # i want to use stdout\_lines of these out1 in my next task
> 
> - name: export  
> shell: command {{item}}  
> register: out2 # i want to use stdout kines of each item in my next task  
> with\_items: "{{out1.stdout\_lines}}"
> 
> #After this I tried to combine out1 as key and out2 as value to the key, But I am getting error
> 
> - set\_fact:  
> my\_env: "{{my\_env | default ({}) | combine ({{item.0}}: {{item.1}})}}"  
> with\_nested:  
> - "{{out1.stdout\_lines}}"  
> - "{{out2 | json\_query('results[\*].stdout\_lines}}"

Sorry, I don't think this deserves an answer - you were explicitly asked two times to provide  
sufficient information - aside from your actual tasks.

Bye  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [February 24, 2020, 2:31pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/7 "2020-02-24T14:31:59Z")

</div>

Hi. We need to know _specifically_ what you are trying to do. Not generally.

Create a minimal playbook that implements (or at least tries to implement) your goal. When you’ve got as far as you can, run it and postthe playbook and the output in full here.

In FULL.

Anything else is wasting your time and ours.

Right now this looks to me as if you are a student trying to get someone else to do your homework.

Regards, K.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 24, 2020, 2:47pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/8 "2020-02-24T14:47:26Z")

</div>

> Hi. We need to know \*specifically\* what you are trying to do. Not generally.
> 
> Create a minimal playbook that implements (or at least tries to implement) your goal. When you've got as far as you can,  
> run it and postthe playbook and the output in full here.  
> In FULL.
> 
> Anything else is wasting your time and ours.
> 
> Right now this looks to me as if you are a student trying to get someone else to do your homework.
> 
> Regards, K.

I concur, in addition to that the shell module should only be used if there is no other module around doing the task  
\*and\* the command module doesn't do the trick (see [https://docs.ansible.com/ansible/latest/modules/shell\_module.html#notes](https://docs.ansible.com/ansible/latest/modules/shell_module.html#notes)).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 24, 2020, 4:40pm UTC](https://forum.ansible.com/t/create-key-value-pair/32315/9 "2020-02-24T16:40:16Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out2\_dict: "{{ dict(my\_keys|zip(my\_values)) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_keys: "{{ out2.results|json\_query('.item') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_values: "{{ out2.results|json\_query('.stdout') }}"

This can be simplified to single json\_query

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out2\_dict: "{{ dict(out2.results|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest)

HTH,

&nbsp;&nbsp;-vlado
