Help with Conditions

39 views
Skip to first unread message

David Foley

unread,
May 5, 2020, 7:37:55 AM5/5/20
to Ansible Project
Hi All,

Just wondering is their a better way of doing the Following?


---
- hosts: localhost
  gather_facts
: false
  tasks
:
 
- name: Getting vCenter Details
    uri
:
      url
: http:///mdb/{{ fqdn }}
      return_content
: yes
      body_format
: json
   
register: mdb_console
 
- copy:
      content
: "{{ mdb_console }}"
      dest
: "/usr2/dfoley/jenkins/workspace/vSphere/Virtual_Machine_Decommissioning/mdb_console.json"


 
- name: Getting VM Info
    block
:
     
- name: Get virtual machine info
        vmware_vm_info
:
          validate_certs
: no
          hostname
: "{{ mdb_console.json.0['console'] }}"
          username
: "{{ user }}"
          password
: "{{ pass }}"
        delegate_to
: localhost
       
register: vm_info
       
when: mdb_console.json.0['console'] is defined
     
- copy:
          content
: "{{ item }}"
          dest
: "/usr2/dfoley/jenkins/workspace/vSphere/Virtual_Machine_Decommissioning/MachineInfo.json"
        with_items
:
         
- "{{ vm_info.virtual_machines | json_query(query) }}"
        vars
:
          query
: "[?guest_name=='{{ VM_Name }}']"


 
- name: Getting VM Info
    block
:
     
- name: Get virtual machine info
        vmware_vm_info
:
          validate_certs
: no
          hostname
: "{{ mdb_console.json.0['vconsole'] }}"
          username
: "{{ user }}"
          password
: "{{ pass }}"
        delegate_to
: localhost
       
register: vm_info
       
when: mdb_console.json.0['console'] is not defined
     
- copy:
          content
: "{{ item }}"
          dest
: "/usr2/dfoley/jenkins/workspace/vSphere/Virtual_Machine_Decommissioning/MachineInfo.json"
        with_items
:
         
- "{{ vm_info.virtual_machines | json_query(query) }}"
        vars
:
          query
: "[?guest_name=='{{ VM_Name }}']"


         
 
- name: Read Json File
    shell
: cat /usr2/dfoley/jenkins/workspace/vSphere/Virtual_Machine_Decommissioning/MachineInfo.json
   
register: json_file


 
- name: Power Down Virutal Machine
    vmware_guest_powerstate
:
      validate_certs
: no
      hostname
: "{{ mdb_console.json.0['console'] }}"
      username
: "{{ user }}"
      password
: "{{ pass }}"
      name
: "{{ VM_Name }}"
      state
: powered-off
    delegate_to
: localhost
   
when: (json_file.stdout | from_json).power_state != "poweredOff" and mdb_console.json.0['console'] is defined


 
- name: Power Down Virutal Machine
    vmware_guest_powerstate
:
      validate_certs
: no
      hostname
: "{{ mdb_console.json.0['vconsole'] }}"
      username
: "{{ user }}"
      password
: "{{ pass }}"
      name
: "{{ VM_Name }}"
      state
: powered-off
    delegate_to
: localhost
   
when: (json_file.stdout | from_json).power_state != "poweredOff" and mdb_console.json.0['console'] is not defined



Looking at maybe something I'm used to with Salt.

- name: Power Down Virutal Machine
    vmware_guest_powerstate
:
      validate_certs
: no
     {% if mdb_console.json.0['console is defind %}
      hostname
: "{{ mdb_console.json.0['console'] }}"
     {% else %}
      hostname: "{{ mdb_console.json.0['vconsole'] }}"
     {% endif %} 
      username
: "{{ user }}"
      password
: "{{ pass }}"
      name
: "{{ VM_Name }}"
      state
: powered-off
    delegate_to
: localhost
    
when: (json_file.stdout | from_json).power_state != "poweredOff"


Brian Coca

unread,
May 5, 2020, 9:40:17 AM5/5/20
to Ansible Project
I would use vars to simplify the tasks themselves, alternatively you
could use a block to establish the common condition

- name: run when not powered off
vmware_guest_powerstate:
hostname: '{{ mdb_console.json.0[resolved_name]}}'
...
vars:
powerstate: '{{ (json_file.stdout | from_json).power_state}}'
console_defined : '{{ mdb_console.json.0['console'] is defined}}'
resolved_name: '{{ console_defined|ternary("console", "vconsole")}}'
when: 'powerstate != "poweredOff"'



--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages