# Variable not defined error

**URL:** <https://forum.ansible.com/t/variable-not-defined-error/23245>\
**Category:** Ansible Project\
**Created:** [December 13, 2016, 1:29am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245 "2016-12-13T01:29:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [December 13, 2016, 1:29am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/1 "2016-12-13T01:29:00Z")

</div>

Hi there —

I have a variable called pbis\_require\_membership\_of defined in group\_vars/all.yaml, and it is overridden for a particular host under role/vars/hostname, however neither of the values are being used at runtime and the module errors with pbis\_require\_membership\_of’ is undefined

`

(ansible)[ansible@ansible-server code]$ grep -R pbis\_require\_membership\_of group\_vars/

group\_vars/all/all.yaml:pbis\_require\_membership\_of: ‘“DOMAIN\teaminfra” “DOMAIN\LinuxAdmins”’

(ansible)[ansible@ansible-server code]$ grep -R pbis\_require\_membership\_of roles/linux-pbis/vars/hostname

pbis\_require\_membership\_of: ‘“DOMAIN\teaminfra” “DOMAIN\LinuxAdmins” “DOMAIN\StorageAdmins”’

`

However, the following occurs when I try to reference it at runtime:

`

TASK [linux-pbis : debug pbis\_require\_membership\_of] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

task path: roles/linux-pbis/tasks/main.yaml:62

ok: [hostname] =\> {

“pbis\_require\_membership\_of”: “VARIABLE IS NOT DEFINED!”

}

…

TASK [linux-pbis : copy temp config file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

task path: roles/linux-pbis/tasks/reconfig\_pbis.yaml:1

fatal: [hostname]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘require\_membership\_of.stdout != pbis\_require\_membership\_of’ failed. The error was: error while evaluating conditional (require\_membership\_of.stdout != pbis\_require\_membership\_of): ‘pbis\_require\_membership\_of’ is undefined\n\nThe error appears to have been in ‘roles/linux-pbis/tasks/reconfig\_pbis.yaml’: line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: copy temp config file\n ^ here\n”}

to retry, use: --limit @playbooks/linux/linux\_pbis.retry

`

I’ve recently updated from Ansible 2.1 to the latest (using git pull), which may or may not have been when this issue started.

Any thoughts on why this variable is not getting set correctly?

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [December 14, 2016, 7:50am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/2 "2016-12-14T07:50:32Z")

</div>

Hello Elliott,

I have written a bit of code that may be helpful to you. You may find it here: [https://github.com/gahan-corporation/ansible-development/tree/master/work/elliott-barrare](https://github.com/gahan-corporation/ansible-development/tree/master/work/elliott-barrare)

Hope it helps. Best of luck to you!

---

<div class="post-metadata">

**Author:** ![Greg\_Langford](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Greg\_Langford](https://forum.ansible.com/u/Greg_Langford)\
**Post date:** [December 15, 2016, 11:30am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/3 "2016-12-15T11:30:43Z")

</div>

I would also say that best practice would be to put host specific vars within host\_vars/hostname as opposed to within roles.

With roles I put my default vars within defaults and anything static within my role which will not change goes within the vars folder.

If there are host or group specific vars, put them in host\_vars or group\_vars this way if someone else has to read your code they do not need to dig in the depths of roles to figure out where the vars were defined.

Kind Regards

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [December 16, 2016, 3:20am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/4 "2016-12-16T03:20:22Z")

</div>

That is excellent advice, sir. I hope you don’t mind I’ve updated the readme to include it: [https://github.com/gahan-corporation/ansible-development/tree/master/work/elliott-barrare](https://github.com/gahan-corporation/ansible-development/tree/master/work/elliott-barrare)

Thanks!

---

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [January 20, 2017, 12:36am UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/5 "2017-01-20T00:36:27Z")

</div>

Thanks for all the advice, guys (I’m just getting back to this since last month). Unfortunately nothing seems to be working.

It appears that none of our variables seem to be getting set. I’ve tried various different locations and names for the group\_vars file, including group\_vars/all/all.yaml, group\_vars/all/all.yml, group\_vars/all.yaml and group\_vars/all.yml and none of them seem to work. The following variables are set in each file, but neither are getting loaded:

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 20, 2017, 2:47pm UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/6 "2017-01-20T14:47:13Z")

</div>

My hunch is that you directory structure is not correct so Ansible can't find the files.

In the same directory as you inventory file, output of these commands would help track it down.  
tree -L 3  
ansible --version

But also your original message you wrote this  
"I've recently updated from Ansible 2.1 to the latest (using git pull), which  
may or may not have been when this issue started."

It you didn't remove 2.1 completely before you install from source you may have a combination of the two, and that will give you some strange behaviors.

---

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [January 20, 2017, 8:32pm UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/7 "2017-01-20T20:32:44Z")

</div>

> My hunch is that you directory structure is not correct so Ansible can’t  
> find the files.

Yeah, that’s what I was thinking too, but unless that changed between versions I don’t know why it would suddenly stop working.

> In the same directory as you inventory file, output of these commands  
> would help track it down.  
> tree -L 3

`

$ tree -L 3

.

├── ansible.cfg

├── cache

…

│ ├── server1

│ └── server2

├── examples

│ └── group\_vars

│ ├── all

│ └── win

├── filter\_plugins

├── group\_vars

│ ├── all

│ │ └── all.yaml

│ ├── all.yaml

│ ├── all.yml

│ ├── linux

│ │ └── linux.yaml

│ ├── vagrant\_linux.yaml

│ ├── vagrant\_win.yaml

│ ├── win

│ │ ├── secure.yaml

│ │ └── win.yaml

│ └── workstations

│ ├── secure.yaml

│ └── workstations.yaml

├── host\_vars

│ ├── hostname.yaml

│ └── vagrant2.yaml

├── inventory

│ ├── centos\_templates

│ ├── production

│ │ ├── generated

│ │ └── production

│ ├── test

│ ├── vagrant

│ └── workstations

├── library

├── playbooks

│ ├── linux

│ │ ├── linux\_ansible\_control\_machine.yaml

│ │ ├── linux\_ftp.yaml

│ │ ├── linux\_jump.yaml

│ │ ├── linux\_nameservers.yaml

│ │ ├── linux\_os\_hardening.yaml

│ │ ├── linux\_pbis.retry

│ │ ├── linux\_pbis.yaml

│ │ ├── linux.retry

│ │ ├── linux\_set\_root\_password.yaml

│ │ ├── linux\_utility.yaml

│ │ ├── linux\_webservers.yaml

│ │ ├── linux.yaml

│ │ └── templates

│ ├── testing

│ │ ├── dumpall.j2

│ │ ├── dump\_vars.yaml

│ │ └── vagrant.yaml

│ ├── win

│ │ └── win\_servers.yaml

│ └── workstations

│ └── win\_workstations.yaml

├── README.md

├── roles

│ ├── linux-ansible-user

│ │ ├── defaults

│ │ ├── files

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ ├── templates

│ │ └── vars

│ ├── linux-common

│ │ ├── defaults

│ │ ├── files

│ │ ├── handlers

│ │ ├── tasks

│ │ ├── templates

│ │ └── vars

│ ├── linux-pbis

│ │ ├── defaults

│ │ ├── files

│ │ ├── meta

│ │ ├── tasks

│ │ ├── templates

│ │ └── vars

│ ├── linux-sendmail-client

│ │ ├── defaults

│ │ ├── files

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ ├── templates

│ │ └── vars

│ ├── linux-set-root-password

│ │ └── tasks

│ ├── linux-sudoers

│ │ ├── defaults

│ │ ├── files

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ ├── templates

│ │ └── vars

├── scripts

│ ├── ad\_kerberos

│ ├── Gemfile

│ ├── Gemfile.lock

│ └── windows-servers.rb

├── site.retry

├── site.yaml

└── Vagrantfile

`

I have removed a lot of unnecessary output from the above, but it should still show the required hierarchy. The group\_vars/all.y{,a}ml are an exact copy of group\_vars/all/all.yaml and were created only for testing.

> ansible --version

`

$ ansible --version

ansible 2.3.0 (devel 92a568c816) last updated 2017/01/20 13:14:32 (GMT -600)

config file =

configured module search path = Default w/o overrides

`

> But also your original message you wrote this  
> “I’ve recently updated from Ansible 2.1 to the latest (using git pull),  
> which  
> may or may not have been when this issue started.”
> 
> It you didn’t remove 2.1 completely before you install from source you  
> may have a combination of the two, and that will give you some strange  
> behaviors.

Unfortunately I don’t think that’s the case. I moved our ansible\_source directory aside and cloned the ansible repo again from scratch — there should not be any conflicts.

Thanks for the reply, and any additional help!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 20, 2017, 9:27pm UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/8 "2017-01-20T21:27:01Z")

</div>

> > My hunch is that you directory structure is not correct so Ansible can't  
> > find the files.
> 
> Yeah, that's what I was thinking too, but unless that changed between  
> versions I don't know why it would suddenly stop working.

So this did work at some point?  
If so did it stop after you upgraded, if thats the case does it work if you downgrade to the last working version?

> > In the same directory as you inventory file, output of these commands  
> > would help track it down.  
> > tree -L 3
> 
> $ tree -L 3  
> .
> 
> ├── ansible.cfg  
> ├── group\_vars  
> ├── host\_vars  
> ├── inventory  
> │ ├── centos\_templates  
> │ ├── production  
> │ │ ├── generated  
> │ │ └── production  
> │ ├── test  
> │ ├── vagrant  
> │ └── workstations  
> ├── playbooks  
> │ ├── linux  
> │ │ ├── linux\_ansible\_control\_machine.yaml  
> │ │ ├── linux\_ftp.yaml  
> │ │ ├── linux\_jump.yaml  
> │ │ ├── linux\_nameservers.yaml  
> │ │ ├── linux\_os\_hardening.yaml  
> │ │ ├── linux\_pbis.retry  
> │ │ ├── linux\_pbis.yaml  
> │ │ ├── linux.retry  
> │ │ ├── linux\_set\_root\_password.yaml  
> │ │ ├── linux\_utility.yaml  
> │ │ ├── linux\_webservers.yaml  
> │ │ ├── linux.yaml  
> │ │ └── templates  
> │ ├── testing  
> │ │ ├── dumpall.j2  
> │ │ ├── dump\_vars.yaml  
> │ │ └── vagrant.yaml  
> │ ├── win  
> │ │ └── win\_servers.yaml  
> │ └── workstations  
> │ └── win\_workstations.yaml  
> ├── site.yaml

I think I see what could be a problem.  
I guess you are running ansible-playbook from the directory ansible.cfg is in, and running someting like this?

ansible-playbook -i inventory/\<sonething\> playbooks/linux/linux.yaml

The reason I ask is that host\_vars and group\_vars must be in the same directory as the inventory or playbooks[1]

And here they are on the top level, so only site.yml is able to use host\_vars and group\_vars in your layout.

If you move the playbook you have problem with to the same level as site.yml, does it work?

> $ ansible --version
> 
> ansible 2.3.0 (devel 92a568c816) last updated 2017/01/20 13:14:32 (GMT -600)  
> config file =  
> configured module search path = Default w/o overrides

Did you run this the the ansible directory, it should have displayed the path to ansible.cfg in "config file", but yours is empty.

[1] [https://docs.ansible.com/ansible/intro\_inventory.html#splitting-out-host-and-group-specific-data](https://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data)

---

<div class="post-metadata">

**Author:** ![Elliott\_Barrere](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@Elliott\_Barrere](https://forum.ansible.com/u/Elliott_Barrere)\
**Post date:** [January 23, 2017, 5:38pm UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/9 "2017-01-23T17:38:16Z")

</div>

> > 
> 
> So this did work at some point?  
> If so did it stop after you upgraded, if thats the case does it work if  
> you downgrade to the last working version?

It did work at some point, and I believe it stopped working immediately after the upgrade.

Good point, I forgot I can simply check out an older version from Git to force an older version of Ansible. I will try that in a bit if other options don’t work.

> > > In the same directory as you inventory file, output of these commands  
> > > would help track it down.  
> > > tree -L 3
> > 
> > $ tree -L 3  
> > .
> > 
> > ├── ansible.cfg  
> > ├── group\_vars  
> > ├── host\_vars  
> > ├── inventory
> 
> > ├── site.yaml
> 
> I think I see what could be a problem.  
> I guess you are running ansible-playbook from the directory ansible.cfg  
> is in, and running someting like this?
> 
> ansible-playbook -i inventory/ playbooks/linux/linux.yaml

That is correct, but I am also specifying -l and --tags to limit scope for testing.

> The reason I ask is that host\_vars and group\_vars must be in the same  
> directory as the inventory or playbooks[1]
> 
> And here they are on the top level, so only site.yml is able to use  
> host\_vars and group\_vars in your layout.

> If you move the playbook you have problem with to the same level as  
> site.yml, does it work?

Aha! That indeed fixes it. I guess we were relying on some deprecated functionality that disappeared when we upgraded.

I have now moved playbooks/linux/linux.yaml to ./linux.yaml for testing, but this is obviously not a great long-term solution (we have a lot of playbook files, and we like to organize them with directories). I have tried moving the host\_vars and group\_vars directories underneath the ./playbooks and ./inventory directories respectively, but neither of those locations work either (probably because each contains a sub-directory that contains the .yaml files themselves)

What is the recommended directory structure for this type of setup, allowing us to keep an organized subdirectory structure?

> > $ ansible --version
> > 
> > ansible 2.3.0 (devel 92a568c816) last updated 2017/01/20 13:14:32 (GMT -600)  
> > config file =  
> > configured module search path = Default w/o overrides
> 
> Did you run this the the ansible directory, it should have displayed the  
> path to ansible.cfg in “config file”, but yours is empty.

It looks like I ran this from a subdirectory before. When I run from the ansible directory the “config file” value is displayed correctly.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 24, 2017, 3:25pm UTC](https://forum.ansible.com/t/variable-not-defined-error/23245/10 "2017-01-24T15:25:27Z")

</div>

> > If you move the playbook you have problem with to the same level as  
> > site.yml, does it work?
> 
> Aha! That indeed fixes it. I guess we were relying on some deprecated  
> functionality that disappeared when we upgraded.
> 
> I have now moved playbooks/linux/linux.yaml to ./linux.yaml for testing,  
> but this is obviously not a great long-term solution (we have a lot of  
> playbook files, and we like to organize them with directories). I have  
> tried moving the host\_vars and group\_vars directories underneath the  
> ./playbooks and ./inventory directories respectively, but neither of those  
> locations work either (probably because each contains a sub-directory that  
> contains the .yaml files themselves)

Yes, they need to be on the same level as the yaml file.

> What is the recommended directory structure for this type of setup,  
> allowing us to keep an organized subdirectory structure?

Ansible was not designed for you layout, there is an alternate layout in the best practice  
[https://docs.ansible.com/ansible/playbooks\_best\_practices.html#alternative-directory-layout](https://docs.ansible.com/ansible/playbooks_best_practices.html#alternative-directory-layout)

Since Ansible follow symlinks, you could just make some links to host\_vars and group\_vars and keep the layout as-is.
