Skip to content

Instantly share code, notes, and snippets.

@markgraf
Last active September 30, 2021 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markgraf/c9bfc8acf6e86924c2309a709c08acc3 to your computer and use it in GitHub Desktop.
Save markgraf/c9bfc8acf6e86924c2309a709c08acc3 to your computer and use it in GitHub Desktop.
The tale of Ansible and the variable that should be there but isn't

The tale of Ansible and the variable that should be there but isn't

So I had a role where I needed to define lots of variables. I decided to split them over multiple files like this:

my_app/defaults/main/
  main.yml
  another.yml 
  yet_another.yml

Tinkering with my shiny new role I moved all of the variables from yet_another.yml to group_vars. Being cautious, I just commented them out.

I ran my role and got:

TASK [include_role : my_app]
***********************************************************************
ERROR! failed to combine variables, expected dicts but
got a 'NoneType' and a 'AnsibleMapping':
null

Trying to find out which of the variables caused the issue, I commented them out one by one. When I arrived at the last variable in the file, the role failed. Aha! Found the culprit. Or so I thought.

Then the search for the misspelled variable began. To no avail.

Okay, maybe the var does not properly get propagated from group_vars?

$ ansible-inventory --graph my_app --vars -i environments/mine/ | less
[...]
|  |--{my_app_default_permission = read-write}
|  |--{my_app_default_user_role = Super admin role}

Nope, it's right there, wether I comment out the variable or not, with the expected values. Hm.

  1. Comment it out, and another variable back in. Role runs fine. What is worng here?

  2. Add a variable that is used nowhere in the role instead, role runs fine. What?

     stupid_unused_var: nothing
    
  3. move yet_another.yml to yet_another.yml.bak and the role runs. Waiiiit...

  4. add a new variables file with nothing but --- and a comment: role fails.

Head -> Desk.

Lesson learned:

Variable files in default/main/whatever.yml need to contain more than just the three dashes declaring that this is a yaml file and a couple of comments.

---
# just a comment
# and nothing more

This will be interpreted as a null-value by Ansible (2.10 in my case), which will entail all sorts of weird errors, that are really not what they look like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment