Set variable to some value if undefined

26 views
Skip to first unread message

Tuyen Nguyen

unread,
May 13, 2019, 9:25:36 AM5/13/19
to Ansible Project
Hi

I have a playbook whereby I pass variables in such as install_office == 'y', and I have tasks where it would only include_tasks for a task file to install Office (for example) when install_office == 'y'

eg.

  tasks:
  - name: Install Office
    include_tasks: installoffice.yml
    when: install_office == 'y'

And when I did not want Office installed, I needed to explicitly define a variable to install_office == 'n' (or anything else other than y would work as well), else Ansible tells me that install_office variable is not defined.

How can I set it so that if I do not define the variable, it will default to 'n'?  Right now I am using include_vars: to include a file defining each of the variables, and for each variable, I set it as 'y' or 'no' based on whether I want to include the task to my main playbook or not.  It would be more simpler if I can just define the ones I want to include as 'y' and let everything else that my playbook expects but is not defined to be set to 'n'.

Please let me know what I can do?  Thanks!!

 

Brian Coca

unread,
May 13, 2019, 9:49:29 AM5/13/19
to Ansible Project
That is what the `default` filter is for:

when: install_office|default('n') == 'y'



--
----------
Brian Coca

Tuyen Nguyen

unread,
May 14, 2019, 10:52:49 AM5/14/19
to Ansible Project
Hi

Thanks.  I will try this.  I think this should work.

Tuyen Nguyen

unread,
May 24, 2019, 1:21:44 PM5/24/19
to Ansible Project
Thank you!  This worked perfectly!!


On Monday, May 13, 2019 at 9:49:29 AM UTC-4, Brian Coca wrote:
Reply all
Reply to author
Forward
0 new messages