# How to retrieve a specific result from multiple command registered to a variable

**URL:** <https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911>\
**Category:** Ansible Project\
**Created:** [March 17, 2016, 11:49pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911 "2016-03-17T23:49:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 17, 2016, 11:49pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/1 "2016-03-17T23:49:50Z")

</div>

I have a series of commands registered to the same var

`

- shell: ls -alh’{{ item }}’  
register:cmd\_output  
with\_items:
- ‘/home/1’
- ‘/home/2’  
`

For some plays, I only need to evaluate a specific stdout. Assuming I want to the results of ‘ls -alh /home/2’, my current method is to loop through cmd\_output and return stdout when the cmd matches my criteria:

`

- debug:  
msg: ‘{{ item.stdout }}’  
when: ( ‘{{ item.cmd }}’ == ‘ls -alh /home/1’ )  
with\_items: ‘{{ cmd\_output.results }}’  
`

Is there a way to do this instead of looping through cmd\_output.results?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 18, 2016, 2:10am UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/2 "2016-03-18T02:10:00Z")

</div>

no, but you can use map/select filters to loop over it instead of a `with_`

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 2:41am UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/3 "2016-03-18T02:41:25Z")

</div>

I think that's the clue I was looking for. I'll post a result tomorrow for others who come here looking for the same answer.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 2:18pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/4 "2016-03-18T14:18:37Z")

</div>

For anyone looking for a solution.

Given this code

`

- shell: ls -alh’{{ item }}’  
register:cmd\_output  
with\_items:
- ‘/home/1’
- ‘/home/2’  
`

I can retrieve the stdout of a specific command without looping through cmd\_output, like so:

`- debug:`  
`msg: '{{ cmd_output.results | selectattr("cmd", "equalto", "ls -alh /home/1" ) | map(attribute="stdout") | join("") }}'`

``

Some caveats … equalto wasn’t a valid test in the CentOS 7.2 Python 2.7.5 package. I ended up compiling Python 2.7.10, then using pip to install ansible, which was an original design requirement which I just got back to fulfilling.

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [March 18, 2016, 2:35pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/5 "2016-03-18T14:35:27Z")

</div>

Faced with a similar issue, I initially wrote a script to which I could pass all my parameters to be operated on, and then a custom module 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 18, 2016, 2:39pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/6 "2016-03-18T14:39:23Z")

</div>

that is actually a jinja2 issue, ‘equalto’ would have been available by just upgrading jinja2 itself.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 7:49pm UTC](https://forum.ansible.com/t/how-to-retrieve-a-specific-result-from-multiple-command-registered-to-a-variable/20911/7 "2016-03-18T19:49:46Z")

</div>

You aren’t wrong. But having spent a number of years sharing systems with Perl developers who also had sudo priviledges, makes me loathed to make changes to global packages. It’s just been better, in my experience, to isolate your environment when your needs start to diverge from the standard distros.
