Re: [ansible-project] Help with Conditions

2020-05-05 Thread Brian Coca
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

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CACVha7d0sg9RH7Wgm-50uc-VYW5u2eTgYoq3UkDbXt-Deb-_1Q%40mail.gmail.com.


[ansible-project] Help with Conditions

2020-05-05 Thread David Foley
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/610b85f9-36ba-44c1-8fb6-4853ce5dfe54%40googlegroups.com.