# Getting a Column value from row

**URL:** <https://forum.ansible.com/t/getting-a-column-value-from-row/25088>\
**Category:** Ansible Project\
**Created:** [July 26, 2017, 7:29am UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088 "2017-07-26T07:29:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 26, 2017, 7:29am UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/1 "2017-07-26T07:29:11Z")

</div>

I need to get the VMID field from the column data as below.

[[root@prmh-mag-31:~] vim-cmd vmsvc/getallvms  
Vmid Name File Guest OS Version Annotation  
51 myvgpu\_clone2 [EVC\_Store1] myvgpu\_clone2/myvgpu\_clone2.vmx windows8\_64Guest vmx-11

so as above is the command result , i need to get the VMID corresponding to vm name myvgpu\_clone2. For that i have used ansible command module and grepping the first output of vm list for vmname so i get the one row . now i have to get the VMID field from the single row.

I have used following code but it does not give the desired output. any help is aprreciated.

Thanks,  
VM

name: Get the VM id of the powered ON VM  
command: vim-cmd vmsvc/getallvms | grep ‘{{guest\_name}}’ | “‘cut -d ’ ’ -f 1’”  
register: ouputvmid  
ignore\_errors: true

- debug:  
var: ouputvmid

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 26, 2017, 2:13pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/2 "2017-07-26T14:13:03Z")

</div>

> I need to get the VMID field from the column data as below.
> 
> [[root@prmh-mag-31:~] vim-cmd vmsvc/getallvms  
> Vmid Name File  
> Guest OS Version Annotation  
> 51 myvgpu\_clone2 [EVC\_Store1] myvgpu\_clone2/myvgpu\_clone2.vmx  
> windows8\_64Guest vmx-11
> 
> so as above is the command result , i need to get the VMID corresponding  
> to vm name myvgpu\_clone2. For that i have used ansible command module and  
> grepping the first output of vm list for vmname so i get the one row . now  
> i have to get the VMID field from the single row.
> 
> I have used following code but it does not give the desired output. any  
> help is aprreciated.

What output do you get and what's wrong with it?

> &nbsp;&nbsp;name: Get the VM id of the powered ON VM  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: vim-cmd vmsvc/getallvms | grep '{{guest\_name}}' | "'cut -d  
> ' ' -f 1'"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ouputvmid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true

Not all esx have python so you might need to use the raw module.

You need to remove the single and double quotes around cut like so.

&nbsp;&nbsp;&nbsp;- name: Get the VM id of the powered ON VM  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: vim-cmd vmsvc/getallvms | grep '{{guest\_name}}' | cut -d' ' -f1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ouputvmid

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 26, 2017, 5:50pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/3 "2017-07-26T17:50:09Z")

</div>

Thanks Kai for your quick response !

Here is my output , problem is it does not extract the VMID but outputs the whole string. I intend to just get the VMID.

TASK [ESX\_VM\_SnapRestore : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [10.114.22.61] =\> {  
“changed”: false,  
“ouputvmid”: {  
“changed”: true,  
“cmd”: [  
“vim-cmd”,  
“vmsvc/getallvms”,  
“|”,  
“grep”,  
“myvgpu\_clone2”,  
“|”,  
“‘cut -d ’ ’ -f 1’”  
],  
“delta”: “0:00:00.455803”,  
“end”: “2017-07-26 07:10:17.235366”,  
“rc”: 0,  
“start”: “2017-07-26 07:10:16.779563”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: "Vmid Name File Guest OS Version Annotation\n51 myvgpu\_clone2 [EVC\_Store1] myvgpu\_clone2/myvgpu\_clone2.vmx windows8\_64Guest vmx-11 ",  
“stdout\_lines”: [  
“Vmid Name File Guest OS Version Annotation”,  
**"51** myvgpu\_clone2 [EVC\_Store1] myvgpu\_clone2/myvgpu\_clone2.vmx windows8\_64Guest vmx-11 "  
]  
}  
}

Though when i run it on ESX command line . it outputs the VMID as below.

## vim-cmd vmsvc/getallvms | grep ‘myvgpu\_clone2’| cut -d ’ ’ -f 1## 51

---

<div class="post-metadata">

**Author:** ![James\_Tanner](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@James\_Tanner](https://forum.ansible.com/u/James_Tanner)\
**Post date:** [July 26, 2017, 5:58pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/4 "2017-07-26T17:58:38Z")

</div>

Try using the shell module instead of command.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [July 26, 2017, 6:05pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/5 "2017-07-26T18:05:44Z")

</div>

use : ouputvmid.stdout\_lines[2] and filter it with the good regexp with regexp\_replace Regards, JYL

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 26, 2017, 6:23pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/6 "2017-07-26T18:23:57Z")

</div>

I forgot that command doesn't allow pipe, so as James suggested, use the shell module. You can also use the raw module.

- name: Get the VM id of the powered ON VM  
&nbsp;&nbsp;&nbsp;shell: vim-cmd vmsvc/getallvms | grep '{{guest\_name}}' | cut -d' ' -f1  
&nbsp;&nbsp;&nbsp;register: ouputvmid

or

- name: Get the VM id of the powered ON VM  
&nbsp;&nbsp;&nbsp;raw: vim-cmd vmsvc/getallvms | grep '{{guest\_name}}' | cut -d' ' -f1  
&nbsp;&nbsp;&nbsp;register: ouputvmid

The value will be in {{ ouputvmid.stdout }}

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 26, 2017, 6:37pm UTC](https://forum.ansible.com/t/getting-a-column-value-from-row/25088/7 "2017-07-26T18:37:32Z")

</div>

Awesome Kai & Jean ! it worked when i used the shell command. 🙂

Lot of thanks to you guys!

ASK [ESX\_VM\_SnapRestore : Get the VM id of the powered ON VM] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed:

TASK [ESX\_VM\_SnapRestore : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: =\> {  
“changed”: false,  
“ouputvmid”: {  
“changed”: true,  
“cmd”: “vim-cmd vmsvc/getallvms | grep ‘myvgpu\_clone2’ | cut -d ’ ’ -f 1”,  
“delta”: “0:00:00.464921”,  
“end”: “2017-07-26 18:34:12.646298”,  
“rc”: 0,  
“start”: “2017-07-26 18:34:12.181377”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “51”,  
“stdout\_lines”: [  
“51”  
]  
}  
}

TASK [ESX\_VM\_SnapRestore : Create snapshot of a vGPU VM] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: ]
