# How to use an IF Else Statement with Ansible Playbook

**URL:** <https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292>\
**Category:** Ansible Project\
**Created:** [February 21, 2020, 11:21am UTC](https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292 "2020-02-21T11:21:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [February 21, 2020, 11:21am UTC](https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292/1 "2020-02-21T11:21:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Davide\_Scrimieri](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@Davide\_Scrimieri](https://forum.ansible.com/u/Davide_Scrimieri)\
**Post date:** [February 21, 2020, 11:30am UTC](https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292/2 "2020-02-21T11:30:07Z")

</div>

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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement)

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [February 21, 2020, 11:53am UTC](https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292/3 "2020-02-21T11:53:25Z")

</div>

> HI Davide

When i use

when: vminfo[“runtime.powerState”] == “poweredOn”

Im Getting

fatal: [localhost]: FAILED! =\> {“msg”: “The conditional check ‘vminfo["runtime.powerState"] == "poweredOn"’ failed. The error was: error while evaluating conditional (vminfo["runtime.powerState"] == "poweredOn"): ‘dict object’ has no attribute ‘runtime.powerState’\n\nThe error appears to be in ‘/srv/ansible/t.yaml’: line 20, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - vmware\_guest\_powerstate:\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [February 21, 2020, 11:56am UTC](https://forum.ansible.com/t/how-to-use-an-if-else-statement-with-ansible-playbook/32292/4 "2020-02-21T11:56:16Z")

</div>

So i have updated the Script as follows:
