Search code examples
ansiblevmware

How to retry until a IP address is assigned with Ansible and vmware_vm_info?


I want to retry until I GET an ip_address: with the vm_info: module for a host (guest_name:). The problem is that sometimes the provisioned vm has not booted and therefore I can't get an IP address

Question:

How should I define my role, to repeat until ip_address is defined?

---
 - name: Get virtual machine info
   vmware_vm_info:
     hostname: '{{ vsphere_host }}'
     username: '{{ vsphere_user }}'
     password: '{{ vsphere_password }}'
     validate_certs: false    
     folder: '/{{ vsphere_datacenter }}/vm'
   delegate_to: localhost
   register: vm_info

 - debug:
     var:  vm_info


 - name: "Update host_var with new IP Address"
   set_fact: 
     ansible_host: "{{ item.ip_address }}"
     ansible_hostname: "{{ item.guest_name }}"
     regexp: "^ansible_host:" 
   with_items:
     - "{{ vm_info.virtual_machines | json_query(query) }}"
   vars:
     query: "[?guest_name=='{{ hostname }}']"
   delegate_to: localhost

This is the debug of register vm_info:

ok: [1-Europe-Cisco-Site1-2-pci] => 
 vm_info:
   changed: false
   failed: false
   virtual_machines:
   - attributes: {}
     cluster: null
     esxi_hostname: 51.89.43.64
     guest_fullname: Other 3.x Linux (64-bit)
     guest_name: 1-Europe-Cisco-Site2-2-guest
     ip_address: ''
     mac_address:
     - 00:50:56:91:41:b7
     - 00:50:56:91:80:67
     - 00:50:56:91:f2:a3
     power_state: poweredOn
     tags: []
     uuid: 42110cfc-0b31-5de2-659d-6f8fe47e8136
     vm_network: {}
   - attributes: {}
     cluster: null
     esxi_hostname: 51.89.43.64
     guest_fullname: Other 3.x Linux (64-bit)
     guest_name: 1-Europe-Cisco-Site1-2-guest
     ip_address: ''
     mac_address:
     - 00:50:56:91:07:e1
     - 00:50:56:91:b0:79
     - 00:50:56:91:d4:5a
     power_state: poweredOn
     tags: []
     uuid: 4211db9d-5608-14fd-1c9b-6cfc470c29c7
     vm_network: {}
   - attributes: {}
     cluster: null
     esxi_hostname: 51.89.43.64
     guest_fullname: Other 3.x Linux (64-bit)
     guest_name: 1-Europe-Cisco-Site2-1-guest
     ip_address: ''
     mac_address:
     - 00:50:56:91:89:ac
     - 00:50:56:91:12:c7
     - 00:50:56:91:e3:0b
     - 00:50:56:91:de:b3
     power_state: poweredOn
     tags: []
     uuid: 421126d9-e74a-2db2-b58c-0a09571d9b29
     vm_network: {}

Until only works for one task, but I need to define here 2x tasks: 1.get vm_info 2.loop thru vm_info and define the host_ip


Solution

  • How about using the vmware_guest_info module the following like?
    In the following playbook, the vmware_guest_info will execute until getting an IP address from a virtual machine using the schema and properties options.

    ---
    - name: Example Playbook
      hosts: localhost
      gather_facts: false
      tasks:
        - name: Operate a virtual machine to powered on
          vmware_guest:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: false
            datacenter: "{{ datacenter_name }}"
            name: "{{ vm_name }}"
            state: poweredon
    
        - name: Gather a virtual machine info
          vmware_guest_info:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: false
            datacenter: "{{ datacenter_name }}"
            name: "{{ vm_name }}"
            schema: vsphere
            properties:
              - guest.ipAddress
          retries: 60
          delay: 10
          until: gather_vm_info.instance.guest.ipAddress is not none
          register: gather_vm_info
    
        - debug: var=gather_vm_info