# How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

**URL:** <https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688>\
**Category:** Ansible Project\
**Created:** [July 6, 2015, 1:42pm UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688 "2015-07-06T13:42:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [July 6, 2015, 1:42pm UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/1 "2015-07-06T13:42:16Z")

</div>

Hi all,

I have the two following ac`tions:`

`

- name: Getting the IDs for the VMs running under the user  
sudo: True  
sudo\_user: oneadmin  
shell: onevm list | grep {{ item.value.login\_name }} | awk ‘BEGIN {FS=" "}{print $1}’  
with\_dict: disable\_users\_account  
register: VMIDs\_per\_user  
ignore\_errors: yes  
tags:

- delete\_VMs

- name: Delete the VMs running under the user  
sudo: True  
sudo\_user: oneadmin  
command: onevm delete {{ item.stdout }}  
with\_items: VMIDs\_per\_user.results  
tags:

- delete\_VMs

`

This is an output example from the the first action:

`

674  
675

`

The problem is that the second action is not working due to the command expects to received just one argument per time, I mean, I need to execute:

`

onevm delete 674

onevm delete 675  
`

`And right now it is not working because it is executing: ```onevm delete` 674 675 , which it does not work because the command does not admit to pass more than one argument.`

`Therefore, my question is, do you know how to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.`

`Thanks in advance,`  
`Esteban`

``

---

<div class="post-metadata">

**Author:** ![Tom\_Paine1](https://avatars.discourse-cdn.com/v4/letter/t/3da27b/32.png) [@Tom\_Paine1](https://forum.ansible.com/u/Tom_Paine1)\
**Post date:** [July 6, 2015, 8:59pm UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/2 "2015-07-06T20:59:14Z")

</div>

[https://docs.ansible.com/playbooks\_loops.html#iterating-over-the-results-of-a-program-execution](https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution)

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [July 7, 2015, 7:31am UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/3 "2015-07-07T07:31:29Z")

</div>

Hi Tom,

Thanks a lot for your answer 🙂

I already tried this yesterday but it is not working for me. Probably, I am not understanding it correctly. This is what I tried:

First, I tried this task:

```auto

- name: Getting the IDs for the VMs running under the user
 sudo: True
 sudo_user: oneadmin
 shell: onevm list | grep {{ item.value.login_name }} | awk 'BEGIN {FS=" "}{print $1}'
 with_dict: disable_users_account
 register: VMIDs_per_user
 ignore_errors: yes
 tags:
 - delete_VMs

- name: Delete the VMs running under the user
 sudo: True
 sudo_user: oneadmin
 command: onevm delete {{ item }}
 with_items: "{{VMIDs_per_user.stdout_lines}}"
 tags:
 - delete_VMs

But I got the following error:

ansible-playbook -i inventory/production site.yml -vvvvvvv --limit opennebula --tags=delete_VMs
Traceback (most recent call last):
 File "/usr/bin/ansible-playbook", line 324, in <module>
 sys.exit(main(sys.argv[1:]))
 File "/usr/bin/ansible-playbook", line 264, in main
 pb.run()
 File "/usr/lib/python2.7/site-packages/ansible/playbook/ __init__.py", line 310, in run
 play = Play(self, play_ds, play_basedir, vault_password=self.vault_password)
 File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 194, in __init__
 self._tasks = self._load_tasks(self._ds.get('tasks', []), load_vars)
 File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 669, in _load_tasks
 loaded = self._load_tasks(data, mv, role_params, default_vars, included_become_vars, list(included_additional_conditions), original_file=include_filename, role_name=new_role)
 File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 650, in _load_tasks
 (k,v) = t.split("=", 1)
ValueError: need more than 1 value to unpack

Also tried with stdout split, I don't remember now the exactly thing that I tried it but it did not work.

This is my ansible version:
 

ansible --version
ansible 1.9.1
 configured module search path = None

Thanks in advance,
Esteban

```

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [July 7, 2015, 7:44am UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/4 "2015-07-07T07:44:44Z")

</div>

This is what I tried yesterday:

[https://docs.ansible.com/playbooks\_conditionals.html](https://docs.ansible.com/playbooks_conditionals.html)

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [July 8, 2015, 2:31pm UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/5 "2015-07-08T14:31:46Z")

</div>

Hi Tom, all

I had a typo on my main task but anyway, I don’t get to manage to do this yet.

If I tried it as in the link provided by you, I get the following issue:

`  
with\_items: “{{VMIDs\_per\_user.stdout\_lines}}”

`

`

TASK: [ON4\_project\_managment | Delete the VMs running under the user] \*\*\*\*\*\*\*\*\*  
fatal: [m-opennebula4] =\> with\_items expects a list or a set

`

Then, I have tried the following things:

`  
shell: onevm delete {{ item.stdout.split(‘\n’) }}  
shell: onevm delete “{{ item.stdout.split(‘\n’) }}”  
shell: onevm delete {{ item.stdout.split(“\n”) }}  
shell: onevm delete {{ item.stdout.splitlines() }}

`

`  
with\_items: “{{VMIDs\_per\_user.stdout\_lines}}”

`

But I got the following issue:

`  
failed: [m-opennebula4] =\> (item={u’cmd’: u’onevm list | grep testing-admin | awk 'BEGIN {FS=" “}{print $1}'‘, u’end’: u’2015-07-08 15:26:43.028904’, u’stderr’: u’‘, u’stdout’: u’685\n686’, u’changed’: True, u’rc’: 0, ‘item’: {‘value’: {‘login\_name’: ‘testing-admin’}, ‘key’: ‘user’}, u’warnings’: , u’delta’: u’0:00:00.778189’, ‘invocation’: {‘module\_name’: u’shell’, ‘module\_args’: u’onevm list | grep testing-admin | awk 'BEGIN {FS=” "}{print $1}'‘}, ‘stdout\_lines’: [u’685’, u’686’], u’start’: u’2015-07-08 15:26:42.250715’}) =\> {“changed”: true, “cmd”: “onevm delete [u’685’, u’686’]”, “delta”: “0:00:01.095980”, “end”: “2015-07-08 15:26:44.508902”, “item”: {“changed”: true, “cmd”: “onevm list | grep testing-admin | awk ‘BEGIN {FS=" "}{print $1}’”, “delta”: “0:00:00.778189”, “end”: “2015-07-08 15:26:43.028904”, “invocation”: {“module\_args”: “onevm list | grep testing-admin | awk ‘BEGIN {FS=" "}{print $1}’”, “module\_name”: “shell”}, “item”: {“key”: “user”, “value”: {“login\_name”: “testing-admin”}}, “rc”: 0, “start”: “2015-07-08 15:26:42.250715”, “stderr”: “”, “stdout”: “685\n686”, “stdout\_lines”: [“685”, “686”], “warnings”: }, “rc”: 255, “start”: “2015-07-08 15:26:43.412922”, “warnings”: }  
stdout: VM named [u685 not found.  
command delete: argument 0 must be one of range, vmid\_list

`

So, it seems it is splitting the lines according to `stdout_lines': [u'685', u'686'] and ``"stdout": "685\n686", "stdout_lines": ["685", "686"] . The problem is `that it is taking the [" characteres as [u and I don’t know how to remove that.

Any idea is welcome 🙂

Thanks in advance,  
Esteban

---

<div class="post-metadata">

**Author:** ![santosh\_reddy](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@santosh\_reddy](https://forum.ansible.com/u/santosh_reddy)\
**Post date:** [January 21, 2016, 6:46am UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/6 "2016-01-21T06:46:53Z")

</div>

Hi Esteban,  
Were you able to find a solution for this, even i’m having the same problem. I’m using ansible 2.0.0.2 but couldn’t find a solution to this.

Thanks,  
Santosh

---

<div class="post-metadata">

**Author:** ![Carlos\_Maria\_Cornejo](https://avatars.discourse-cdn.com/v4/letter/c/5e9695/32.png) [@Carlos\_Maria\_Cornejo](https://forum.ansible.com/u/Carlos_Maria_Cornejo)\
**Post date:** [September 8, 2017, 11:28am UTC](https://forum.ansible.com/t/how-to-get-item-by-item-from-the-item-stdout-so-you-can-execute-a-command-for-each-item-on-the-item-stdout/18688/7 "2017-09-08T11:28:08Z")

</div>

Hi folks,

Just in case this help someone else out

- name: Retrieve existing vm’s  
vars:  
\_vm\_iaas\_username: “{{ [vm\_iaas\_username, data.vm\_iaas\_username, ‘oneadmin’]|reject(‘undefined’)| first }}”  
shell: “onevm list | grep {{ \_vm\_iaas\_username }} | awk ‘BEGIN {FS=" "}{print $1}’”  
register: vm\_ids  
become: True  
delegate\_to: front

- name: Delete existing vm’s  
shell: “onevm terminate {{ item }}”  
become: true  
delegate\_to: front  
with\_items:

- "{{ vm\_idst.stdout\_lines }}

Regards,  
Carlos M.
