Create Key value pair

13 views
Skip to first unread message

Sivaraman P

unread,
Feb 24, 2020, 8:06:55 AM2/24/20
to Ansible Project
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:
- 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}}"

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

Stefan Hornburg (Racke)

unread,
Feb 24, 2020, 8:13:45 AM2/24/20
to ansible...@googlegroups.com
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
Racke

--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Sivaraman P

unread,
Feb 24, 2020, 8:16:32 AM2/24/20
to ansible...@googlegroups.com
Thank you for your response. I just Showed it for example only... Actually I am getting those keys from register variable(out1)

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9a55e3a9-0ec5-56c4-da52-ca8ab74c623e%40linuxia.de.

Dick Visser

unread,
Feb 24, 2020, 8:42:54 AM2/24/20
to ansible...@googlegroups.com
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.
> --
> 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 ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/16eb196f-324c-4944-ac21-94c1a0b98e75%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Sivaraman P

unread,
Feb 24, 2020, 8:59:19 AM2/24/20
to ansible...@googlegroups.com
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}}"

Stefan Hornburg (Racke)

unread,
Feb 24, 2020, 9:04:34 AM2/24/20
to ansible...@googlegroups.com
On 2/24/20 2:58 PM, Sivaraman P wrote:
> 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
Racke
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/16eb196f-324c-4944-ac21-94c1a0b98e75%40googlegroups.com.
>
>
>
> --
> Dick Visser
> Trust & Identity Service Operations Manager
> GÉANT
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAHS7Q%2BvOU-tkbKtFVGgt5YeY%2BQb4y7dXxVKwJOH6_YjOFZoR-g%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAHS7Q%2BvOU-tkbKtFVGgt5YeY%2BQb4y7dXxVKwJOH6_YjOFZoR-g%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Karl Auer

unread,
Feb 24, 2020, 9:32:28 AM2/24/20
to ansible-project
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.



--

Karl Auer { manager, systems support }
P: 1300 759 975
E: ka...@2pisoftware.com
2pisoftware.com

GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

Stefan Hornburg (Racke)

unread,
Feb 24, 2020, 9:47:59 AM2/24/20
to ansible...@googlegroups.com
On 2/24/20 3:31 PM, Karl Auer wrote:
> 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).

Regards
Racke

>
> On Tue, Feb 25, 2020 at 12:59 AM Sivaraman P <shivar...@gmail.com <mailto:shivar...@gmail.com>> wrote:
>
> 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}}"
>
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/16eb196f-324c-4944-ac21-94c1a0b98e75%40googlegroups.com.
>
>
>
> --
> Dick Visser
> Trust & Identity Service Operations Manager
> GÉANT
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAL8fbwMvo6%2BF0ZJJNgDO3UNjTCw6d%3D4jFRDncHFvpn19MkcXAg%40mail.gmail.com.
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> *
> *
> *Karl Auer* { manager, systems support }
> P: 1300 759 975
> E: ka...@2pisoftware.com <mailto:ka...@2pisoftware.com>
> 2pisoftware.com <https://2pisoftware.com>
>
> GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
> Previous: 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08QuPV7-ZA-%3DV5DC6T36v_-pTpeqEiPZjDDMzcO0Kn%2BCUA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08QuPV7-ZA-%3DV5DC6T36v_-pTpeqEiPZjDDMzcO0Kn%2BCUA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Vladimir Botka

unread,
Feb 24, 2020, 11:40:33 AM2/24/20
to Sivaraman P, ansible...@googlegroups.com
On Mon, 24 Feb 2020 05:06:55 -0800 (PST)
Sivaraman P <shivar...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages