Score:2

Ansible-pull does not find vars in group_vars directory

hm flag

I have an ansible structure that contains some roles.

/usr/local/ansible
    playbooks
        roles
            role1
            role2
                tasks
                handlers
                files
                templates
                    my_template.yml
                vars
                    main.yml
    group_vars
        role2.yml

Usually, when running this from the ansible server, the template: module finds group_vars/role2.yml and applies it to my_template just fine.

When I use ansible-pull the template module cannot find the group vars and the first template line that references a variable causes an error - can't find the variable. Only by putting my variables in vars/main.yml can we find them.

ansible-pull -C dockervm -d /usr/local/ansible -U [email protected]:MRI-Software/HappySoftware-config-usr_local_ansible.git -i localhost, -e 'small=yes vagrant=True ServerNamePrefix=docker' --accept-host-key /usr/local/ansible/playbooks/docker.yml

Since the ansible-pull operation leaves behind the entire /usr/local/ansible directory tree (as pulled from the git server) I tried running ansible-playbook manually and got the same failure as ansible-pull.

ansible-playbook -i localhost -e  'small=yes vagrant=True ServerNamePrefix=docker'  ./docker.yml

The main difference between this provisioning and the normal web server provisioning is that this is running against localhost.

So I made sure that /usr/local/ansible/group_vars and /usr/local/ansible/playbooks/group_vars both contained a localhost collection of variables, but the playbook still fails on the first variable reference in the template:

TASK [docker : template] *******************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'SSH_WEB_USER' is undefined"}

Yes, I checked that the variable definition was there.

So I transplanted the entire collection of variables to the roles/docker/vars directory inside main.yml and that works, but it's now an exception compared to the rest of the provisioning roles.

Any idea how to get the ansible-pull to reference the group_vars directory (in playbooks)?

Score:1
br flag

Your directories group_vars and playbooks are in the same directory /usr/local/ansible. The command ansible-pull runs the playbook from the directory playbooks /usr/local/ansible/playbooks/docker.yml.

Take a look at Variable precedence: Where should I put a variable?. You'll see that group_vars can be placed either in the playbook or inventory directory (precedence 4.-7.).

  • The simplest solution would be to move group_vars into the directory playbooks
/usr/local/ansible/playbooks/group_vars/main.yml

But, you'll see an error if you haven't cloned the repository playbooks yet

fatal: destination path '/usr/local/ansible/playbooks' already exists and is not an empty directory.

As a result, this option works if you move group_vars into the already cloned repository and the option --purge isn't used.

  • The next option is moving group_vars into the inventory. For example,
shell> pwd
/tmp/test

shell> tree .
.
└── inventory
    ├── group_vars
    │   └── all
    │       └── main.yml
    └── hosts

3 directories, 2 files

shell> cat inventory/hosts 
localhost

shell> cat inventory/group_vars/all/main.yml 
test_var_1: test variable 1

Configure inventory

shell> export ANSIBLE_INVENTORY=/tmp/test/inventory

and run the ansible-pull command

shell> ansible-pull -d /tmp/test/test_001 -U https://repos.example.org/test_001.git --full /tmp/test/test_001/pb_001.yml
Starting Ansible Pull at 2023-07-22 19:18:03
/home/admin/.local/bin/ansible-pull -d /tmp/test/test_001 -U https://repos.example.org/test_001.git --full /tmp/test/test_001/pb_001.yml
localhost | CHANGED => {
    "after": "1f637428cafb37156e4027dc459e90609a2c2192",
    "before": null,
    "changed": true
}

PLAY [all] *********************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => 
  msg: Playbook pb_001.yml is runnning ...

TASK [debug] *******************************************************************
ok: [localhost] => 
  test_var_1: test variable 1

PLAY RECAP *********************************************************************
localhost: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

, where the below playbook was used

shell> cat test_001/pb_001.yml 
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        msg: Playbook pb_001.yml is runnning ...
    - debug:
        var: test_var_1
Ed Greenberg avatar
hm flag
Actually, I have the group_vars in playbooks and had copied it to ../playbooks as part of debugging this. It doesn't find it in either place. I'm going to try putting it in a subdir called inventory as you suggested, and specify that on the ansible-pull line. I'll report back.
br flag
See [Special Variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html). As part of debugging, you might want to take a look at *inventory_dir* and *playbook_dir*.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.