Getting a Column value from row

31 views
Skip to first unread message

Vijay Misra

unread,
Jul 26, 2017, 3:29:11 AM7/26/17
to Ansible Project
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


    
      

Kai Stian Olstad

unread,
Jul 26, 2017, 10:13:18 AM7/26/17
to ansible...@googlegroups.com
On 26. juli 2017 09:29, Vijay Misra wrote:
> 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?


> 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

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.

- name: Get the VM id of the powered ON VM
command: vim-cmd vmsvc/getallvms | grep '{{guest_name}}' | cut -d'
' -f1
register: ouputvmid


--
Kai Stian Olstad

Vijay Misra

unread,
Jul 26, 2017, 1:50:23 PM7/26/17
to ansible...@googlegroups.com
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







--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/TAWwl3sA4tY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@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/26f1c6cf-a690-d449-d86e-1db528118b09%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

James Tanner

unread,
Jul 26, 2017, 1:58:51 PM7/26/17
to ansible...@googlegroups.com
Try using the shell module instead of command.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.

Jean-Yves LENHOF

unread,
Jul 26, 2017, 2:06:10 PM7/26/17
to ansible...@googlegroups.com
use :
ouputvmid.stdout_lines[2]
and filter it with the good regexp with regexp_replace

Regards,

JYL



Kai Stian Olstad

unread,
Jul 26, 2017, 2:24:15 PM7/26/17
to ansible...@googlegroups.com
On 26. juli 2017 19:50, Vijay Misra wrote:
> 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.

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
shell: vim-cmd vmsvc/getallvms | grep '{{guest_name}}' | cut -d' ' -f1
register: ouputvmid

or

- name: Get the VM id of the powered ON VM
raw: vim-cmd vmsvc/getallvms | grep '{{guest_name}}' | cut -d' ' -f1
register: ouputvmid

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

--
Kai Stian Olstad

Vijay Misra

unread,
Jul 26, 2017, 2:37:45 PM7/26/17
to ansible...@googlegroups.com
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: ]





--
Kai Stian Olstad

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/TAWwl3sA4tY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages