Set variable to some value if undefined

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!!

That is what the `default` filter is for:

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

Hi

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

Thank you! This worked perfectly!!