Search code examples
linuxansibleredhatvmwareansible-tower

Ansible vmware_guest optional disk with Ansible Tower survey


I have a playbook for the creation of a VM from a template in VMware ESXi 6.7. My playbook is below. I want to only configure the second (and possible subsequent) disks if the DISK1_SIZE_GB variable is > 0. This is not working. I've also tried using 'when: DISK1_SIZE_GB is defined' with no luck. I'm using a survey in Ansible Tower, with the 2nd disk configuration being an optional answer. In this case I get an error about 0 being an invalid disk size, or when I check for variable definition I get an error about DISK1_SIZE_GB being undefined. Either way, the 'when' conditional doesn't seem to be working. If I hardcode the size, as in the first 'disk' entry, it works fine .. same if I enter a valid size from Ansible Tower. I need to NOT configure additional disks unless the size is defined in the Tower survey. Thanks!


---
- name: Create a VM from a template
  hosts: localhost
  gather_facts: no
  tasks:
  - name: Clone a template to a VM
    vmware_guest:
      hostname: "{{ lookup('env', 'VMWARE_HOST') }}"
      username: "{{ lookup('env', 'VMWARE_USER') }}"
      password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
      validate_certs: 'false'
      name: "{{ HOSTNAME }}"
      template: RHEL-Server-7.7
      datacenter: Production
      folder: Templates
      state: poweredon
      hardware:
        num_cpus: "{{ CPU_NUM }}"
        memory_mb: "{{ MEM_MB }}"
      disk:
        - size_gb: 20
          autoselect_datastore: true
        - size_gb: "{{ DISK1_SIZE_GB }}"
          autoselect_datastore: true
          when: DISK1_SIZE_GB > 0
      networks:
      - name: "{{ NETWORK }}"
        type: static
        ip: "{{ IP_ADDR }}"
        netmask: "{{ NETMASK }}"
        gateway: "{{ GATEWAY }}"
        dns_servers: "{{ DNS_SERVERS }}"
        start_connected: true
      wait_for_ip_address: yes

Solution

  • AFAIK this can't be accomplished in a single task. You were on the right track with when: DISK1_SIZE_GB is defined if disk: was a task and not a parameter though. Below is how I would approach this.

    Create two survey questions:

    • DISK1_SIZE_GB - integer - required answer - enforce a non-zero minimum value such as 20 (since you're deploying RHEL)

    • DISK2_SIZE_GB - integer - optional answer - no minimum or maximum value

    Create disk 1 in your existing vmware_guest task:

    disk:
      - size_gb: "{{ DISK1_SIZE_GB }}"
        autoselect_datastore: true
    

    Create a new vmware_guest_disk task which runs immediately afterwards and conditionally adds the second disk:

    - name: Add second hard disk if necessary
      vmware_guest_disk:
        hostname: "{{ lookup('env', 'VMWARE_HOST') }}"
        username: "{{ lookup('env', 'VMWARE_USER') }}"
        password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
        validate_certs: 'false'
        name: "{{ HOSTNAME }}"
        datacenter: Production
        folder: Templates
        state: poweredon
        disk:
          - size_gb: "{{ DISK2_SIZE_GB }}"
            autoselect_datastore: true
      when: DISK2_SIZE_GB is defined