How to use an IF Else Statement with Ansible Playbook

197 views
Skip to first unread message

David Foley

unread,
Feb 21, 2020, 6:21:44 AM2/21/20
to Ansible Project
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 % }

Davide Scrimieri

unread,
Feb 21, 2020, 6:30:45 AM2/21/20
to ansible...@googlegroups.com
Please, don't do this. Register a variable and use the when condition. 

--
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-proje...@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.

David Foley

unread,
Feb 21, 2020, 6:53:25 AM2/21/20
to Ansible Project
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"}

David Foley

unread,
Feb 21, 2020, 6:56:16 AM2/21/20
to Ansible Project
So i have updated the Script as follows: 
 
---
- hosts: localhost
  gather_facts: false
  tasks:
  - vmware_guest_info:
      hostname: 
      username: 
      password: 
      validate_certs: no
      datacenter: Test
      name: Win10
      schema: "vsphere"
      properties: ["runtime.powerState"]
    delegate_to: localhost
    register: vminfo
  - debug:
      var: vminfo


  - vmware_guest_powerstate:
      validate_certs: no
      hostname: 
      username: 
      password: 
      name: Win10
      state: powered-off
    delegate_to: localhost
    register: deploy
    when: vminfo['powerState'] == "poweredOn"

Reply all
Reply to author
Forward
0 new messages