Reputation: 23
Can anyone please help me here, I have a playbook which has the following vars:
vars_files:
- "../vars/location.yml"
The location.yml
has the following declared:
loc: "{{ inventory_hostname[0:3] }}"
location_file: "/root/ansible/vars/{{ loc }}.yml"
So from hostname location_file:
is then prd.yml
, this file contains the line:
txm_QA_access: false
Now if I run the playbook which copies a sudoers template with the following conditional specified in it:
{% if txm_QA_access %}
%Sudo-admin ALL= /bin/su - admin
%Team-QA ALL= /bin/su - admin
%Team-QA\ OffShore\ \(UK\) ALL= /bin/su - admin
{% endif %} `
When I run the playbook I get an error:
"msg": "AnsibleUndefinedVariable: `txm_QA_access` is undefined"
Where am I going wrong? Isn't it defined as a Boolean which is false
so in theory it should skip adding those lines in the sudoers template?
Any help appreciated spent hours on this now.
Upvotes: 2
Views: 16164
Reputation: 7340
I guess this is because the vars file prd.yml
(most likely) is not getting loaded. And as @dechandler10 mentioned, you can use include_vars:
, instead of vars_files:
.
In your playbook you have already included location.yml
, next you should include vars from prd.yml
before the template
task.
vars_files:
- '../vars/location.yml'
tasks:
# Your 'location_file' variable set to: /root/ansible/vars/prd.yml
- name: include variables
include_vars:
file: '{{ location_file }}'
Now that your prd.yml
has been included, the variable txm_QA_access
will be available for template.
However, Ansible already has built-in mechanism called group_vars
and host_vars
.
Instead of having to define variable paths with inventory_hostname[0:3]
, Ansible can directly load variables for hosts or groups with this method. See organizing host and group variables.
Upvotes: 2
Reputation: 36
You didn't mention doing anything to include the variables in prd.yml
, so I'm going to assume you haven't. You'll need to use include_vars to load the variables from that file.
If you are doing that, I recommend adding debug tasks immediately before your template task to sort out exactly which variable is causing the problem.
Upvotes: 1