Ansible Get vSphere Virtual Machines

250 views
Skip to first unread message

David Foley

unread,
Apr 8, 2020, 7:12:06 AM4/8/20
to Ansible Project
Hi All,

Trying to Output only the Virtual Machine Name: but having issues with the Json output

---
   
- name: Running Create Virtual Machine Playbook
      hosts
: localhost
      gather_facts
: false
      connection
: local
      tasks
:
     
- name: Get VMs
        vmware_vm_info
:
          validate_certs
: no
          hostname
:
          username
:
          password
:
          vm_type
: vm
        delegate_to
: localhost
       
register: vminfo
     
- debug:
         
var: vminfo.virtual_machines.guest_name
     
- local_action: copy content={{ vminfo.virtual_machines.guest_name }} dest=/srv/ansible/vmname.json
     



I'm currently getting the following Error:



TASK
[debug] *****************************************************************************************************************************************
ok
: [localhost] => {
   
"vminfo.virtual_machines.guest_name": "VARIABLE IS NOT DEFINED!"
}

This is the JSON output if i just do a var vminfo

{
 
"failed": false,
 
"virtual_machines":
 
[
   
{
     
"guest_name": "",
     
"guest_fullname": "CentOS 7 (64-bit)",
     
"esxi_hostname": "e",
     
"tags": [],
       
"cluster": "",
       
"vm_network":
       
{
         
"":
         
{
           
"ipv4": [""],
           
"ipv6": []
           
},
           
"":
           
{"ipv4": [""],
             
"ipv6": []},
             
"": {"ipv4": [""], "ipv6": ["f"]}}, "mac_address": [""], "attributes": {}, "power_state": "poweredOn", "ip_address": "", "uuid": "4212ca75-8a0a-e9dc-95bc-3ee10ea6af11"
},


               
{
"guest_name": "", "guest_fullname": "Microsoft Windows 8 (64-bit)",

Gobi

unread,
Apr 8, 2020, 7:32:39 AM4/8/20
to ansible...@googlegroups.com
Hi David,

virtual_machines is a list.
This will work:
- debug:
   
var: vminfo.virtual_machines[0].guest_name

--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/218fea74-3de0-468f-a9d7-8ab73f826a6a%40googlegroups.com.

David Foley

unread,
Apr 8, 2020, 7:46:37 AM4/8/20
to Ansible Project
Hi Gobi

that outputs the first on the Json List, how would i do a loop ?

i Tried with_items

 - debug:
        msg: "{{ item.tags }}"
      with_items:
        - "{{ vm_info.virtual_machines.guest_name }}"

Gobi

unread,
Apr 8, 2020, 7:56:36 AM4/8/20
to ansible...@googlegroups.com
Hi David,
You can do a loop like this

- debug:
msg: "{{ item.tags }}"
with_items:
- "{{ vm_info.virtual_machines }}"


> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9d2f330c-e546-45f0-b8c4-a34b4d0efafd%40googlegroups.com.

David Foley

unread,
Apr 8, 2020, 8:13:16 AM4/8/20
to Ansible Project
Hi Gobi,

I was using that above, but it outputs all information,
I was trying to only output the guest_name with the with_item loop 

Mani

unread,
Apr 30, 2020, 6:56:18 AM4/30/20
to Ansible Project
Hi David,

You can try the below loop:

- debug:
    msg: "{{ item.guest_name }}"
  with_items:
    - "{{ vm_info.virtual_machines }}"
Reply all
Reply to author
Forward
0 new messages