[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2017-09-08 Thread Carlos María Cornejo Crespo
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.

On Thursday, 21 January 2016 14:35:43 UTC+1, santosh wrote:
>
> 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
>
> On Wednesday, 8 July 2015 20:01:46 UTC+5:30, Esteban Freire wrote:
>>
>> 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
>>
>> El lunes, 6 de julio de 2015, 22:59:14 (UTC+2), Tom Paine escribió:
>>>
>>>
>>> https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>>>
>>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c3e3cf8d-2ecd-4752-80fe-ce93386d775a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2016-01-21 Thread santosh reddy
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

On Wednesday, 8 July 2015 20:01:46 UTC+5:30, Esteban Freire wrote:
>
> 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
>
> El lunes, 6 de julio de 2015, 22:59:14 (UTC+2), Tom Paine escribió:
>>
>>
>> https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>>
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/392fb248-bcc9-4c2f-a1ea-0075bf6d214c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2015-07-08 Thread Esteban Freire
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

El lunes, 6 de julio de 2015, 22:59:14 (UTC+2), Tom Paine escribió:
>
>
> https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0f68d3f1-d355-47ce-8dbd-c627524b7049%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2015-07-07 Thread Esteban Freire
This is what I tried yesterday:

https://docs.ansible.com/playbooks_conditionals.html

El martes, 7 de julio de 2015, 9:31:29 (UTC+2), Esteban Freire escribió:
>
> 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:
>
> - 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 -vvv --limit opennebula 
> --tags=delete_VMs
> Traceback (most recent call last):
>  File "/usr/bin/ansible-playbook", line 324, in 
>  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
>
>
>
>
>  
>
> El lunes, 6 de julio de 2015, 22:59:14 (UTC+2), Tom Paine escribió:
>>
>>
>> https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>>
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6c0b38a7-db27-4b41-9e62-2bbc36a9f60d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2015-07-07 Thread Esteban Freire
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:

- 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 -vvv --limit opennebula 
--tags=delete_VMs
Traceback (most recent call last):
 File "/usr/bin/ansible-playbook", line 324, in 
 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




 

El lunes, 6 de julio de 2015, 22:59:14 (UTC+2), Tom Paine escribió:
>
>
> https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/502864e3-a9e3-4b66-a09f-84c335f41124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to get item by item from the item.stdout so you can execute a command for each item on the item.stdout.

2015-07-06 Thread Tom Paine
https://docs.ansible.com/playbooks_loops.html#iterating-over-the-results-of-a-program-execution

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/089ec724-a6e9-40c5-ad0e-fd79a491bae5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.