How to fix Ansible error about "undefined variable"

by Christian Fei @ 2019-04-04

214 words, 1 minute reading time

#post #tutorial 


Got the following error when running my playbook, with a newly installed external role:

fatal: []: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible_distribution' is undefined\n\nThe error appears to have been in '.../tasks/main.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n  ^ here\n"}

After some "research" / googling, I finally understood why this was happening.

get your facts straight

In this case the variable missing can be read from the stacktrace above: ansible_distribution.

It never happened to me that a variable wasn't available, and ansible_distribution seemed pretty much like a standard ansible variable..

So I quickly debugged the variables with the following command, that essentially runs the setup module (with the -m option) and spits out a whole bunch of collected variables (read gathered).

ansible -u root -i hosts.production -m setup

After a short break it hit me: it seems the variables are not gathered.

And in fact, in the playbook there was the option gather_facts set to false..

By default, Ansible gathers all the facts about the host. For more info check out the official documentation.

To fix it, simply remove the line from the playbook or set it to True (which is the default value):

  gather_facts: False


Leave a comment

Better web scraping with node.js My blogging stack in 2020 Run cypress integration tests with github actions workflow Simple telegram message with github actions Deploy eleventy site with github actions on aws s3 Simple ad and trackers blocking with dns Build for a slow connection Upgrade mongodb 3 to 4 on ubuntu Publishing org scoped npm packages with travis A story about npm publish / unpublish