With AWX/Tower, group_vars are not loaded when the group_vars folder is inside the inventory.
Sample project structure :
my_project/
inventory_folder/
dev/
hosts.ini
group_vars/
my_group.yml
all.yml
playbooks/
playbook.yml
roles/
In tower, the inventory "dev" is defined with the source type 'Sourced from a Project'. When job is executed inside tower, vars defined in my_group.yml or in all.yml are not loaded.
But this is working when playbook is executed with ansible-playbook command :
'ansible-playbook -i my_project/inventory_folder/dev/hosts.ini my_project/playbooks/playbook.yml '
3.2.0 is not an AWX version and the minimum version of Ansible we install in the deployment is 2.4
Can you re-verify with a latest AWX image?
@matburt Is there a specific file layout we should be using for AWX to import group_vars and host_vars?
I'm using version (tag) 1.0.2 and can confirm I'm having this problem although my layout is different:
my_project/
group_vars/
dev.yml
inventories/
dev.ini
host_vars/
DEV_HOSTNAME.yml
@matburt
I experience this problem also, my layout is
inventory/
hosts.ini
group_vars/
groupname1.yml
groupname2.yml
host_vars/
host1.yml
host2.yml
host_vars are loaded just fine.
I'm using latest AWX image.
@matburt
Could it be reopened, this bug makes "Source from the Project" feature of the inventory unusable.
Ansible 2.4.3.0
AWX 1.0.3.52
Is it that the group_vars aren't getting loaded, or that they are applied inside of hostvars for all the hosts in the group? The latter is a known issue, and was just resolved with a change in AWX that merged yesterday, and is staged for the Ansible 2.5 release. You should be able to resolve this by getting up-to-date versions of both, but this will require installing Ansible from source inside of your environment.
@AlanCoding
Yes, you're right, group_vars applied inside of host_vars. And I see ansible-inventory's --export in 2.5 which should provide group variables values.
Thanks a lot.
@matburt
I am having exactly the same problem. I have tried different directory structures but group_vars are not getting loaded. Since Ansible looks for group_vars in the same directory as the inventory, I am not sure where to put it as I added my inventories using the Web UI and I don't know where and how AWX stores them. This is my current directory structure:
my_project/
ansible.cfg
roles/
sites/
hosts(static inventory file - not used in AWX)
group_vars/
You are probably running into this issue: https://github.com/ansible/ansible/issues/30877
Which will be fixed in AWX once ansible releases a new version and we pick it up. Or you install the version that includes that fix yourself.
@matburt
My issue is actually different. At least in the issue you linked, the group_vars are loaded and applied to hostvars. In my case, it doesn't get loaded at all and I run into variable not defined errors for vars defined in the group_vars each time I run the playbooks.
@Ken4scholars are the groups you're looking at defined in hosts in the directory structure you gave above? In that same directory structure, does group_vars/ have a file that's name is the same as your group? If yes to both of these, what version of Ansible are you using? If you're still trying to troubleshoot, could you try running ansible-inventory -i hosts --list --export, with and without the --export option?
@AlanCoding
I tried a couple of groups with the same name as host groups and group_vars/all.yml which I think should work for all hosts - and it works with vanilla Ansible. I'm using Ansible 2.4. I'm not sure of the exact revision number and cannot give the debug information as it is all on my workstation and I'm back from work. I will send the results of the command tomorrow when I get to work. I appreciate the quick responses.
@AlanCoding
The --export option seems not to exist. And yes, I can see that thehostvars are populated with the group_vars/all.yml variables. But I still get "undefined variable" errors for those variables when I run jobs. Is this the expected behaviour? If so, how can I get the group_vars working?
Also note that /api/v2/inventories/<inventory_id>/script/ does not show those hostvars as cli does.
You are saying that simple hostvars in your inventory aren't defined in your playbook, of course that's not expected. You can triple-check that your variable is showing up at /api/v2/inventories/<inventory_id>/script/?hostvars=1, that's what gets passed into the ansible-playbook CLI.
@AlanCoding
I tried that but the hostvars in the api only shows the variables I defined for each host through the Web UI. Is it possible that they are shadowing the ones defined in group_vars/* files? I hardly think this is the case since some of the hosts don't have any variables defined through UI but still don't have the ones in group_vars/*.
UPD: Just to let you know - I defined my inventory for AWX using the Web UI as I didn't see a way to easily import from my hosts file. I suspect this might be the problem but I am not sure.
I don't know. I have plenty of examples using group_vars/all.yml, etc. and for 2.4 they should show up in host variables (in the UI as well) after a sync.
Is it possible that they are shadowing the ones defined
Shouldn't be, especially assuming that you did the sync last. The sync should be overwriting the existing variables for the hosts that the sync touches. It shouldn't matter that it was first created (or had vars added) in the UI.
@AlanCoding
Does any of the examples have public access? I want to take a look at the structure
https://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/scripts/basic_vars
Here, I'm naming "all" variables along the lines of "inventory_var", and some come from the file (a plugin var) and some come directly from the inventory.
@AlanCoding
Thanks, I will take a look. As a side note, it seems you added your inventories and vars with scripts. Mine were static yaml files for the vars and a static INI file for the inventory (which I am not using in AWX). Could this be why AWX can't find them?
Any kind of combination of file types like that are fair game for testing, and I'll go raise an Ansible bug if I find something. I'm pretty sure I've tested INI inventory with yaml vars.
@AlanCoding
So how can I import the INI inventory defined in my project? I currently see no way of doing that. I'm only seeing the option of either using inventory scripts or creating the inventory manually in the UI
Source the inventory from a project, and if that particular file doesn't show up in the list of inventory files, you can type in the relative path manually. This feature will accept all types that work out of the box with Ansible.
@AlanCoding
That did it! You're a lifesaver! Immediately after I added the source, it started picking up my group_vars too. So my suspicion is that the group_vars are tightly coupled to their inventories. Since I wasn't using the project inventory but was creating the inventory in the UI(though the group names are practically the same), it did not pick up the group_vars too. Now it does. I've been on this for 2 straight days now. Thanks a million!
@AlanCoding
we are using tower 3.0.3 and ansible 2.3.2.0 , we face same issue of group_vars not being loaded and we resolved it by as written above by including group_vars dir at playbook level in all playbooks directory
Redhat must fix this issue in tower
If you are using Ansible Tower, please talk to your Red Hat service rep (who will, among other things, note you're using an EOL version.)
AWX 2.1.0 / Ansible 2.7.1
Issue is still present.
Project tree is like this:
.
โโโ inventories
โ โโโ test
โ โ โโโ group_vars
โ โ โ โโโ all.yml
...
โโโ test.yml
Content of the all.yml file is show up in vars inside awx/inventory.
Test playbook is very simple:
- hosts: consul_instances
gather_facts: no
tasks:
- name: test vault vars
debug: var=lookup('env','VAULT_TOKEN')
- name: test vault vars
debug: var=lookup('env','VAULT_ADDR')
- name: test vault vars
debug: var=pgsql_password
VAULT_TOKEN/VAULT_ADDR is picked up and showed correctly on test run, but pgsql_password = Undefined Variable.
In inventories/test/group_vars/all.yml pgsql_password is defined.
You could be hitting https://github.com/ansible/awx/issues/2574, which was recently fixed in AWX.
@ripun .. I am having same issue where playbook doesn't see group_vars/files ..can you tell ..
you specified you fixed issue by "including group_vars dir at playbook level in all playbooks directory"
how do you do it ? Do you provide path to group_vars ?
thanks
Julia