vmsiul
Enthusiast
Enthusiast

vRA 8.6 | IF / ELSE statement in YAML with Booleans

Jump to solution

Hello, 

I am trying to get some assistance with the following. I am using a Boolean 

inputs:
app1:
type: boolean
title: SQL
default: false
app2:
type: boolean
title: IIS
default: false

I would like to then be able to pass this into an input which will call ansible playbook that will match then the app1 or app2 tittle (SQL or IIS etc) which will be the name of the Ansible playbook in the controller. 

------------------------

Cloud_Ansible_1:
type: Cloud.Ansible
properties:

----------------------

playbooks:
provision:
- /home/ansible/playbook.yml
- '${input.app1 == "true" ? "/home/ansible/sql.yaml" : ””}'          ----->  Run the following if the App1 is true . 

but for some reason the value is not working and instead I am getting an error with:

The only playbook run options supported are playbook level variables (Specified by -e OR --extra-vars). Please correct the following playbooks specified with unsupported options: ${input.app1

Can someone provide some assistance? maybe I am doing something wrong or something needs to be different? 

Thank you

0 Kudos
3 Solutions

Accepted Solutions
johnbowdre
Enthusiast
Enthusiast

I wonder if the error might be due to having a null value in there when app1 isn't true?

Defaulting to a valid-but-empty playbook path might be an option:

resources:
  Cloud_Ansible_1:
    type: Cloud.Ansible
    properties:
      [...]
      playbooks:
        provision:
          - /home/ansible/playbook.yml
          - '${input.app1 ? "/home/ansible/app1.yml" : "/home/ansible/empty.yml"}'
          - '${input.app2 ? "/home/ansible/app2.yml" : "/home/ansible/empty.yml"}'

Just a guess though. I don't have access to Ansible in my lab at the moment so I can't test this myself. It works in my head though!

View solution in original post

0 Kudos
xian_
Expert
Expert

Can you try literal null instead of empty string "" after the colon? (I can't try either as not using Ansible here.)

View solution in original post

0 Kudos
Ankush11s
VMware Employee
VMware Employee
inputs:
  App1:
    type: boolean
    Default: false
    title: App1
resources:
  Ansible:
    type: Cloud.Ansible
    metadata:
      layoutPosition:
        - 0
        - 1
    properties:
      account: eso-ansible
      username: administrator
      password: Dynam1c0ps
      maxConnectionRetries: 20
      playbooks:
        provision:
          - '${input.App1 == true ?  "/root/ansible-playbooks/windows_enableiis.yml" :  ""}'
      osType: windows
      inventoryFile: /root/ansible-playbooks/windowshosts
      groups:
        - win2012
      host: ${resource.AnsibleWindowsVM.*}


This should work error was occurring since we were passing true in quotes which was making it string ,instead of predefined type of boolean 

View solution in original post

7 Replies
sajal1
VMware Employee
VMware Employee

Hi,

Not sure what is creating the exact problem but it seems to me that the variable is represented as an ansible variable. On the other hand, check the following blog on achieving it in another way.

https://www.mobius.co.uk/deeper-look-at-the-vrealize-automation-and-ansible-open-source-integration/

I know it is not a direct answer to your question, but will let you achieve what you want to do. For the exact problem, I need to test it myself and unfortunately I do not have this setup in my environment.

0 Kudos
johnbowdre
Enthusiast
Enthusiast

I wonder if the error might be due to having a null value in there when app1 isn't true?

Defaulting to a valid-but-empty playbook path might be an option:

resources:
  Cloud_Ansible_1:
    type: Cloud.Ansible
    properties:
      [...]
      playbooks:
        provision:
          - /home/ansible/playbook.yml
          - '${input.app1 ? "/home/ansible/app1.yml" : "/home/ansible/empty.yml"}'
          - '${input.app2 ? "/home/ansible/app2.yml" : "/home/ansible/empty.yml"}'

Just a guess though. I don't have access to Ansible in my lab at the moment so I can't test this myself. It works in my head though!

0 Kudos
Ankush11s
VMware Employee
VMware Employee

This is currently being reviewed , whether if else is supported under ansible code block or not 

xian_
Expert
Expert

Can you try literal null instead of empty string "" after the colon? (I can't try either as not using Ansible here.)

0 Kudos
Ankush11s
VMware Employee
VMware Employee
inputs:
  App1:
    type: boolean
    Default: false
    title: App1
resources:
  Ansible:
    type: Cloud.Ansible
    metadata:
      layoutPosition:
        - 0
        - 1
    properties:
      account: eso-ansible
      username: administrator
      password: Dynam1c0ps
      maxConnectionRetries: 20
      playbooks:
        provision:
          - '${input.App1 == true ?  "/root/ansible-playbooks/windows_enableiis.yml" :  ""}'
      osType: windows
      inventoryFile: /root/ansible-playbooks/windowshosts
      groups:
        - win2012
      host: ${resource.AnsibleWindowsVM.*}


This should work error was occurring since we were passing true in quotes which was making it string ,instead of predefined type of boolean 

sajal1
VMware Employee
VMware Employee

Thanks Ankush. This should definitely solve it. Missed that point. After al it is a syntax problem.

0 Kudos
vmsiul
Enthusiast
Enthusiast

Thank you all for your assistance! 🙂 

0 Kudos