Ansible – boolean variable in extra vars

Passing variables to Playbook with extra vars flag is something normal during the daily work. Even though Ansible offers two ways to do that, you can face the problem when you need to set the true or false values. You should not do something like —extra-vars “foo=true” with the boolean values. I know, it’s quite annoying, but we need to deal with it. So, if you read this post, you wonder how you can pass the boolean variable in extra vars. And now I want to tell you how to do that in a proper way.

Variable Precedence

First, let’s see how variable precedence looks. Below, you have the list of all variable categories. On the top, you have a variable with the lowest priority, and on the bottom, there is the most valuable variable category. Hence you can deduce, that extra vars beat all variables.

  • role defaults
  • inventory file or script group vars
  • inventory group_vars/all
  • playbook group_vars/all
  • inventory group_vars/*
  • playbook group_vars/*
  • inventory file or script host vars
  • inventory host_vars/*
  • playbook host_vars/*
  • host facts
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • role (and include_role) params
  • include params
  • include_vars
  • set_facts / registered vars
  • extra vars (always win precedence)

If you need more detailed information, please see this Ansible Documentation section. Anyway, as you can see, passing variables via CLI is the best way to overwrite any other values of the same variable. I’m sure you know that if you work with the Ansible on a daily basis. For those guys, who find it as a new information, here you have the ways of passing variables in CLI.

Passing variables vi CLI

I do not want to explain this topic in details because Ansible Documentation is clear enough. Even though, I would try to bring you a step closer, especially those who do not know how to do that. So, generally, there are two basic ways of passing variables in CLI, as extra vars:

  1. with the -e flag
  2. with the –extra-vars flag

Both ways are exactly the same, but one of them is the shorter one. Therefore it doesn’t matter which one you will choose. Now let assume that we have two variables: foo and bar.

As you can see you should just type pairs key=value separated by the space. And that’s all. Of course, you can also load a file with the variables and so on, but it’s not the main topic. Please visit this page to get more information about playbook variables. Now, the most important information for us is:

  1. Boolean variable cannot be passed via the way described above
  2. Variables can be passed in JSON format

Yes, JSON. Can somebody call JSON? Noone? I can, hence I am able to show you how to pass the boolean variable in extra vars. Because it’s the way you can do that – by using JSON format.

JSON for the win!

So how to finally pass the boolean variable in extra vars?

The answer is very simple. You need to use a JSON format, which in our case should look like:

Above you see two variables:

  • foo – string
  • bar – list of three elements

Please remember, that every string in JSON should be in the quotation mark! So if you want to escape quotation mark in the variable values, you should use \ before the quotation mark. Anyway, as I draw, only the string should be in the quotation mark. It doesn’t concern the boolean values. It would be better if I show you an example. Let’s assume that we want to set values of two variables – foo and bar. Foo should be true and bar should be false. Therefore our –extra-vars should look like this one:

Standard way works for me, what are you talking about?!

Yes, in some cases the standard way should work properly. But… You know… In some cases. It depends on how you define your conditionals and so on. When you look at the Ansible documentation, you see this warning:

boolean variable in extra vars

Yes, as a string. What does it mean? No more or less than if you create proper conditional uses boolean values, passing a variable in the standard way (key-value pair) probably will not be work as you want. So, for this reason, it’s much safer when you use the JSON rather than the simpler format. And your life would be better!

More about Ansible on my blog you can find in the Ansible category!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.