Although Ansible has a low learning curve, obviously there’s always a big difference between learning on your own versus working on a medium to large scale size (enterprise) projects. Today’s post will be related to a common error in Ansible known as AnsibleUndefinedVariable
.
Prerequisites
- Ansible
Solution(s)
The error is pretty much self-explanatory, so you might need to check if this specific variable or multiple vars are loaded by any of vars
, include_vars
, or vars_file
. It also depends on the approach on how you organize hosts and group variables. For best practices Organizing host and group variables.
A few common examples on how to include vars:
Example 1. Load variables using vars_file
from file (works on playbook level only).
---
- hosts: host_group_1
vars_files:
- some_vars_file.yml
roles:
- role: role_1
...
Example 2. Load variables using include_vars
on a task level.
---
- hosts: host_group_1
tasks:
- name: task 1
include_vars: some_vars_file.yml
...
Example 3. You could also do a combination.
---
- hosts: host_group_2
vars:
ansible_python_interpreter: /usr/bin/python3
vars_files:
- some_vars_file.yml
roles:
- role: role_2
Note(s): Obviously, there’s a difference between vars_file
and include_vars
which will be described more into details in one of the following posts.
Conclusion
And if you got any unresolved AnsibleUndefinedVariable
errors, please share them with me in the comment section below. On a side note, follow our official channel on Telegram.