# Inconsistent variable dereferences depending on number of forks

**URL:** <https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547>\
**Category:** Ansible Project\
**Created:** [September 25, 2013, 10:59am UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547 "2013-09-25T10:59:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 10:59am UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/1 "2013-09-25T10:59:28Z")

</div>

Dear all,

I have recently upgraded to ansible 1.3.2 from the release/1.3.2 branch. This upgrade broke my playbooks.

git bisect identified commit 576962d as the first bad commit. This commit changes the number of forks in relation to the number of hosts. As it turns out, this commit did not introduce the bug I encountered, but merely unmasked it.

Unfortunately, the most reduced example I managed to create is not really minimal. At the end of the post there’s a script to create the files I mention here, so that you can easily reproduce the problem. Note that you will need to set hash\_behaviour = merge In your ansible.cfg:

# hosts:

[vm\_servers]  
127.0.0.2  
[vm\_guests]  
127.0.0.3  
127.0.0.4

# host\_vars/127.0.0.3:

name: srv  
vm:  
host\_system: 127.0.0.2

# host\_vars/127.0.0.4:

name: srx  
vm:  
host\_system: 127.0.0.2

# site.yml:

- include: role.vm.yml

# role.vm.yml:

- hosts: vm\_guests  
vars\_files:
- “roles/vm/vars/{{ name }}.yml”  
roles:
- vm

# roles/vm/vars/srv.yml

vm:  
foo: bar

# roles/vm/vars/srx.yml

vm:  
foo: baz

# roles/vm/tasks/main.yml

- name: delegation pass  
action: shell hostname -f  
delegate\_to: “{{ vm.host\_system }}”

- name: template upload  
action: template src=test.j2 dest=/tmp/test.txt

- name: delegation fail  
action: shell hostname -f  
delegate\_to: “{{ vm.host\_system }}”

# roles/vm/templates/test.j2

{%- for host in groups[‘vm\_guests’] -%}  
{{ hostvars[host][‘name’] }}  
{%- endfor %}

With ansible 1.3.2, the playbook works like expected when called with:

$ ansible-playbook site.yml -i hosts

If I limit the number of forks to exactly 1, execution aborts with an error:

$ ansible-playbook site.yml -i hosts -vvvv --forks 1  
…  
TASK: [delegation pass]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 12:27pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/2 "2013-09-25T12:27:31Z")

</div>

See [https://github.com/ansible/ansible/blob/devel/CONTRIBUTING.md](https://github.com/ansible/ansible/blob/devel/CONTRIBUTING.md) for information about how to report bugs.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 12:29pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/3 "2013-09-25T12:29:48Z")

</div>

In the above, you have

- “roles/vm/vars/{{ name }}.yml”

This is a vars\_file that is loaded by the hostname.

This is a bad idea because all variables in the vars/ directory are going to get loaded.

What you should do instead is put all variables in the ‘host\_vars/host’ file that are specific, or store them outside of the roles path.

---

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 12:48pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/4 "2013-09-25T12:48:26Z")

</div>

In my original playbook I have something like

vars\_files:

- [ “roles/vm/vars/{{ name }}.yml”, “roles/vm/vars/default.yml”

as mentioned in [the docs](http://www.ansibleworks.com/docs/playbooks2.html#conditional-imports). Is this still bad practice? Is the referenced example not applicable to roles?

---

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 12:57pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/5 "2013-09-25T12:57:49Z")

</div>

Thanks. I created a new issue [#4239](https://github.com/ansible/ansible/issues/4239).

---

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 1:10pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/6 "2013-09-25T13:10:51Z")

</div>

Michale, your last sentence just registered 🙂

So this is bad:

vars\_files:

- [“roles/vm/vars/{{ name }}.yml”, “roles/vm/vars/default.yml”]

while the following is ok:

vars\_files:

- [“vars/{{ name }}.yml”, “vars/default.yml”]

Correct?

---

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 1:12pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/7 "2013-09-25T13:12:16Z")

</div>

Oops, typo. Sorry, Michael.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 1:16pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/8 "2013-09-25T13:16:46Z")

</div>

I looked and the feature I think we had about loading everything in vars/ under a role isn’t actually implemented yet 🙂

So, yeah, we need to look into this.

I imagine something is not doing a copy operation so the main variables are getting tweaked.

Sorry about the noise – hard to remember what gets implemented sometimes!

---

<div class="post-metadata">

**Author:** ![Florian\_Tham](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Florian\_Tham](https://forum.ansible.com/u/Florian_Tham)\
**Post date:** [September 25, 2013, 1:20pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/9 "2013-09-25T13:20:31Z")

</div>

Thanks. Should we move the discussion to github?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 11:44pm UTC](https://forum.ansible.com/t/inconsistent-variable-dereferences-depending-on-number-of-forks/13547/10 "2013-09-25T23:44:49Z")

</div>

Yeah we have the ticket open now, so we can track it on the ticket.

Thanks!
