Reputation: 10470
I am having a hard time understand how to group_vars. I am working with a large and confusing ansible code base. I want to test what I am doing on a centos docker container that I run locally on my laptop. I can access this container like so:
$ ansible all -i,localhost -m ping -o --key-file=~/.ssh/id_rsa -u root
...
localhost | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
I want to test a role that will place a file on the container.
- name: enable_systemd | create RALM service scripts
template:
src: ralm.service.j2
dest: /etc/systemd/system/ralm-{{ item }}.service
mode: '0644'
with_items: "{{ ralm_instances }}"
when: servertype != 'zc'
The template references a variable, BBS_HOME.
I run my playbook like so:
$ ansible-playbook -i staging_hosts -l localhost -b \
> --key-file=~/.ssh/id_rsa -u root playbooks/real-123.yml \
> -e"stype=rrm" -e"ralm_instances=1"
localhost is in this inventory file:
$ grep -sr -B1 localhost staging_hosts
staging_hosts/staging_hosts-[real_123]
staging_hosts/staging_hosts:localhost
And I have a this file ...
$ grep BBS_HOME group_vars/real_123
BBS_HOME: /opt/bbs
But when I run my playbook I get this output:
PLAY [all] *****************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************
ok: [localhost]
TASK [../roles/ralm : enable_systemd | remove existing RALM service files] *************************************************************************************************************************
skipping: [localhost]
TASK [../roles/ralm : enable_systemd | create RALM service scripts] ********************************************************************************************************************************
ok: [localhost] => (item=1)
TASK [../roles/ralm : enable_systemd | create RALM service environment conf file] ******************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AnsibleUndefinedVariable: 'BBS_HOME' is undefined
failed: [localhost] (item=1) => {"ansible_loop_var": "item", "changed": false, "item": "1", "msg": "AnsibleUndefinedVariable: 'BBS_HOME' is undefined"}
PLAY RECAP *****************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
Yet when I do this ...
$ ansible all -i staging_hosts -l localhost -m debug -a "var=hostvars[inventory_hostname]" | head
... I can see my group_vars and values:
localhost | SUCCESS => {
"hostvars[inventory_hostname]": {
"MULTI_RZ": 1,
"BBS_HOME": "/opt/bbs",
"STATUS": 1,
...
Here's my ansible version info:
$ ansible --version
ansible [core 2.11.6]
config file = None
configured module search path = [u'/Users/redcrik/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /Users/redcrik/DOCKER/ANSIBLE/venv/lib/python2.7/site-packages/ansible
ansible collection location = /Users/redcrik/.ansible/collections:/usr/share/ansible/collections
executable location = /Users/redcrik/DOCKER/ANSIBLE/venv/bin/ansible
python version = 2.7.16 (default, Aug 30 2021, 14:43:11) [GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy
jinja version = 2.11.3
libyaml = True
Upvotes: 1
Views: 6526
Reputation: 2291
Does your file structure from where you run the ansible-playbook
command look like this?
staging_hosts/
staging_hosts
group_vars/
real_123
playbooks/
real-123.yml
When running with ansible-playbook
, it's looking for the group_vars
folder in the same directory as the playbook and/or the inventory. When you run the ansible
command, it looks for the group_vars
folder in the current working directory and the inventory.
If you move your group_vars
into your inventory folder, it should work in both cases.
staging_hosts/
staging_hosts
group_vars/
real_123
playbooks/
real-123.yml
Upvotes: 2