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"


-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/610b85f9-36ba-44c1-8fb6-4853ce5dfe54%40googlegroups.com.

Reply via email to