Re: [ansible-project] How to use an IF Else Statement with Ansible Playbook

2020-02-21 Thread Davide Scrimieri
Please, don't do this. Register a variable and use the when condition.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement


On Fri, Feb 21, 2020 at 12:22 PM David Foley  wrote:

> Hi,
>
> Can Someone help with a IF Else Statment within Ansible.
>
> If i Run a a GetStatus Playbook the Register: vminfo outputs the Following:
>
>
> ok: [localhost] => {
> "vminfo": {
> "changed": false,
> "failed": false,
> "instance": {
> "runtime": {
> "powerState": "poweredOn"
> }
> }
> }
>
>
> What I'm trying to-do is place this into an if Else Statment
> My play book is below
>
>
> ---
> - name: Running Delete Virtual Machine playbook
>   hosts: localhost
>   gather_facts: false
>   connection: local
>   tasks:
> - vmware_guest_info:
> hostname: vctst01
> username:
> password:
> validate_certs: no
> datacenter: Test
> name: Win10
> schema: "vsphere"
> properties: ["runtime.powerState"]
>   delegate_to: localhost
>   register: vminfo
> - debug:
> var: vminfo
>
> {% if vminfo == 'poweredOff' %}
>
> - name: Deleting
>   vmware_guest:
> hostname: vctst01
> username:
> password:
> validate_certs: no
> cluster: Test
> name: Win10
> state: absent
>   delegate_to: localhost
>   register: facts
>
> {% else %}
> - name: Powering Down
>   vmware_guest_powerstate::
> validate_certs: no
> hostname: vctst01
> username:
> password:
> name: Win10
> state: powered-off
>   delegate_to: localhost
>   register: deploy
>
> - name: Deleting Machine
>   vmware_guest:
> hostname: vctst01
> username:
> password:
> validate_certs: no
> cluster: Test
> name: Win10
> state: absent
>   delegate_to: localhost
>   register: facts
> {% endif % }
>
> --
> 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/de617a22-2b8d-489e-a510-0042196dcf68%40googlegroups.com
> 
> .
>

-- 
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/CAEOhr7-14b5WOVr3pc6xTvJkMxa592Oy2SDJ%2BHDXpiNZWzyh0w%40mail.gmail.com.


[ansible-project] How to use an IF Else Statement with Ansible Playbook

2020-02-21 Thread David Foley
Hi,

Can Someone help with a IF Else Statment within Ansible.

If i Run a a GetStatus Playbook the Register: vminfo outputs the Following:


ok: [localhost] => {
"vminfo": {
"changed": false,
"failed": false,
"instance": {
"runtime": {
"powerState": "poweredOn"
}
}
}


What I'm trying to-do is place this into an if Else Statment
My play book is below


---
- name: Running Delete Virtual Machine playbook
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
- vmware_guest_info:
hostname: vctst01
username: 
password: 
validate_certs: no
datacenter: Test
name: Win10
schema: "vsphere"
properties: ["runtime.powerState"]
  delegate_to: localhost
  register: vminfo
- debug:
var: vminfo

{% if vminfo == 'poweredOff' %}

- name: Deleting
  vmware_guest:
hostname: vctst01
username:
password: 
validate_certs: no
cluster: Test
name: Win10
state: absent
  delegate_to: localhost
  register: facts

{% else %}
- name: Powering Down
  vmware_guest_powerstate::
validate_certs: no
hostname: vctst01
username: 
password: 
name: Win10
state: powered-off
  delegate_to: localhost
  register: deploy

- name: Deleting Machine
  vmware_guest:
hostname: vctst01
username: 
password: 
validate_certs: no
cluster: Test
name: Win10
state: absent
  delegate_to: localhost
  register: facts
{% endif % }

-- 
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/de617a22-2b8d-489e-a510-0042196dcf68%40googlegroups.com.