# saving command output from a loop in different variables

**URL:** <https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731>\
**Category:** Ansible Developer\
**Created:** [September 2, 2020, 8:43pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731 "2020-09-02T20:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kevin\_Hoyt](https://forum.ansible.com/u/Kevin_Hoyt)\
**Post date:** [September 2, 2020, 8:43pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731/1 "2020-09-02T20:43:13Z")

</div>

Hi,

I’m trying to run a command on a remote server, multiple times and need to set a fact or register different names for each. Here’s the code I have for running the command:

- name: ‘run opctest status on PG’  
win\_command: “opctest /cust {{ tenant\_code }} /node {{ item }}”  
args:  
stdin: status  
register: opctest\_out  
ignore\_errors: yes  
with\_items:
- “{{ pg\_list }}”

pg\_list is a listing of 3 strings (‘PG1A’, ‘PG100A’,‘PG101A’). I’d like to name them after each of these, for example, something along the lines of “opctest\_out\_pg101a”

I can’t find a way to do that. Does anyone have any suggestions?

Thanks,  
Kevin

---

<div class="post-metadata">

**Author:** ![Mike\_Sawyers](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mike\_Sawyers](https://forum.ansible.com/u/Mike_Sawyers)\
**Post date:** [September 2, 2020, 9:45pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731/2 "2020-09-02T21:45:36Z")

</div>

I haven’t tried it myself, but maybe you can make a dict object using your item as the key?

[https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results](https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results)

---

<div class="post-metadata">

**Author:** ![Kevin\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kevin\_Hoyt](https://forum.ansible.com/u/Kevin_Hoyt)\
**Post date:** [September 3, 2020, 1:21pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731/3 "2020-09-03T13:21:54Z")

</div>

Hi Mike,

Thanks, I’ll see if I can make that work.

Kevin

---

<div class="post-metadata">

**Author:** ![Kevin\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kevin\_Hoyt](https://forum.ansible.com/u/Kevin_Hoyt)\
**Post date:** [September 4, 2020, 4:12pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731/4 "2020-09-04T16:12:34Z")

</div>

Hi Mike,

I’ve been staring at this and maybe I’m missing it. I’m not sure how to get from the command output to the dict. Setting a fact makes sense, but how do I do it as part of the data collection task?

Thanks,  
Kevin

---

<div class="post-metadata">

**Author:** ![Kevin\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kevin\_Hoyt](https://forum.ansible.com/u/Kevin_Hoyt)\
**Post date:** [September 4, 2020, 6:28pm UTC](https://forum.ansible.com/t/saving-command-output-from-a-loop-in-different-variables/33731/5 "2020-09-04T18:28:33Z")

</div>

I was able to get a fact set with my data. Here is the task and the fact. My last problem is how to make it cacheable.

- name: ‘run opctest status on PG’  
win\_command: “opctest /cust {{ tenant\_code }} /node {{ item }}”  
args:  
stdin: status  
register: opctest\_out  
ignore\_errors: yes  
with\_items:

- “{{ pg\_list }}”

- name: my\_test  
set\_fact: {“healthcheck\_pg\_{{ item.item }}”: “{{ item.stdout }}”}  
loop: “{{ opctest\_out | json\_query(‘results[\*]’) }}”

that creates the following:  
“ansible\_facts”: {  
“healthcheck\_pg\_PG100B”: “12:13:37 Trace: EMT Creating Mutex Glo…”

}

but since it’s not cacheable it’s not in the mongodb at the backend.

Thanks,  
Kevin
