# Variable scope issue: value from a different role being used

**URL:** <https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370>\
**Category:** Ansible Project\
**Created:** [September 29, 2014, 3:13pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370 "2014-09-29T15:13:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Bamford1](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Tom\_Bamford1](https://forum.ansible.com/u/Tom_Bamford1)\
**Post date:** [September 29, 2014, 3:13pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/1 "2014-09-29T15:13:46Z")

</div>

Hi all

I’m running from HEAD (recently ran ‘git pull --rebase && git submodule update --init --recursive’ following recent restructuring).

Unfortunately I can’t tell which commit I was previously running from, but I currently have an issue with the value of a variable being used from a different role.

I have the following structure. Real repository details are substituted for privacy but I have been careful to preserve the alphanumerical order of the project names.

**deploy.yml**

- name: Configure instance(s) and deploy application  
hosts: launch  
user: root  
gather\_facts: true

roles:

- role: deploy-projectB

tags: deploy  
when: project == ‘projectB’ or project == ‘subprojectB’

- role: deploy-projectA  
tags: deploy  
when: project == ‘projectA’

**roles/projectB/tasks/main.yml**

- name: what repo url and dir are we cloning?  
debug: msg=“repo\_url={{ repo\_url }} repo\_dir={{ repo\_dir }}”

- name: Git clone  
git: repo={{ repo\_url }}  
dest={{ repo\_dir }}  
version={{ repo\_branch }}  
accept\_hostkey=yes

**roles/projectB/vars/main.yml**

repo\_url: ssh://[git@github.com/myorg/projectB.git](http://git@github.com/myorg/projectB.git)  
repo\_dir: /usr/local/projectB/

**roles/projectA/vars.yml**

repo\_url: ssh://[git@github.com/myorg/projectA.git](http://git@github.com/myorg/projectA.git)  
repo\_dir: /usr/local/projectA/

Now, when running the following: ansible-playbook -vvvv deploy.yml -e ‘project=projectB repo\_branch=feature/blah’

I get the following output and error when the projectB role is being applied (the error is because the origin/feature/blah branch doesn’t exist on the projectA repo)

TASK: [deploy-system | what is going on?] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> ESTABLISH CONNECTION FOR USER: root  
ok: [[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)] =\> {  
“msg”: “repo\_branch=feature/blah repo\_url=ssh://[git@github.com/myorg/projectA.git](http://git@github.com/myorg/projectA.git) repo\_dir=/usr/local/projectA/”  
}

TASK: [deploy-system | Git clone] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> ESTABLISH CONNECTION FOR USER: root  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> REMOTE\_MODULE git repo=ssh:\*\*\*\*\*\*\*\*@[github.com/myorg/projectA.git](http://github.com/myorg/projectA.git) dest=/usr/local/projectA/ version=feature/blah accept\_hostkey=yes  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -o StrictHostKeyChecking=no -o Port=22 -o IdentityFile=“/root/.ssh/id\_rsa” -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 [ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com) /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1412002786.78-203481241742422 && echo $HOME/.ansible/tmp/ansible-tmp-1412002786.78-203481241742422’  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> PUT /tmp/tmpAAFWjp TO /root/.ansible/tmp/ansible-tmp-1412002786.78-203481241742422/git  
\<[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)\> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -o StrictHostKeyChecking=no -o Port=22 -o IdentityFile=“/root/.ssh/id\_rsa” -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 [ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com) /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_CTYPE=en\_US.UTF-8 /usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1412002786.78-203481241742422/git; rm -rf /root/.ansible/tmp/ansible-tmp-1412002786.78-203481241742422/ \>/dev/null 2\>&1’  
failed: [[ec2-54-183-209-28.us-west-1.compute.amazonaws.com](http://ec2-54-183-209-28.us-west-1.compute.amazonaws.com)] =\> {“failed”: true}  
msg: Failed to checkout feature/blah

FATAL: all hosts have already failed – aborting

As you can see, the {{ repo\_url }} and {{ repo\_dir }} variables from projectA are being incorrectly used when applying the projectB role. This was behaving as expected\* when running from an [unknown] earlier commit of ansible so I think it was introduced recently.

- Expected behaviour would be to use the variables from projectB/vars/main.yml

I have ensured that manually performing a git clone and git checkout does really work with the correct details.

Thanks in advance for any input or clarification on what I may have missed, or if it is indeed a bug.

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford1](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Tom\_Bamford1](https://forum.ansible.com/u/Tom_Bamford1)\
**Post date:** [September 29, 2014, 3:16pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/2 "2014-09-29T15:16:28Z")

</div>

Oh, the role name mismatch between what is in deploy.yml and the following filenames (deploy-projectA vs projectA) really is just a typo (doh).

Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford1](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Tom\_Bamford1](https://forum.ansible.com/u/Tom_Bamford1)\
**Post date:** [September 29, 2014, 3:17pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/3 "2014-09-29T15:17:42Z")

</div>

Sorry, also the output of `ansible --version`:

ansible 1.8 (devel 459722899d) last updated 2014/09/29 14:22:39 (GMT +000)  
lib/ansible/modules/core: (detached HEAD db5668b84c) last updated 2014/09/29 14:22:47 (GMT +000)  
lib/ansible/modules/extras: (detached HEAD 110250d344) last updated 2014/09/29 14:22:52 (GMT +000)  
configured module search path = /usr/share/ansible

---

<div class="post-metadata">

**Author:** ![Ben\_Symonds](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Ben\_Symonds](https://forum.ansible.com/u/Ben_Symonds)\
**Post date:** [September 30, 2014, 9:05am UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/4 "2014-09-30T09:05:25Z")

</div>

I have seen this too. I haven’t had time to investigate it yet (I just reverted back to 1.7 in the meantime). Happy to provide more details of my config (although it is similar - two roles with vars, role A picking up vars from role B). I just wanted to reply to the list confirming it’s not just Tom who’s seeing it!

I suppose it might be worth opening a Github issue for it. It does sound like a recently-introduced bug.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 30, 2014, 5:12pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/5 "2014-09-30T17:12:12Z")

</div>

Hi, I believe this issue is the same one as discussed above:

[https://github.com/ansible/ansible/issues/9219](https://github.com/ansible/ansible/issues/9219)

Please see my comment at the bottom regarding a new feature branch I’ve been working on, which resolves the example playbook in that issue. Additional testing on that new branch would be very welcome!

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 30, 2014, 7:22pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/6 "2014-09-30T19:22:12Z")

</div>

Role protections, such that variables defined in a role are assuredly used in that role, are a major part of the “great role standdown of 2013”.

We need to fix this ahead of the feature branch, as (A) that feature branch might not hit 1.8, and (B) we need to preserve that role compatibility now.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 30, 2014, 8:54pm UTC](https://forum.ansible.com/t/variable-scope-issue-value-from-a-different-role-being-used/16370/7 "2014-09-30T20:54:17Z")

</div>

Hi Tom/Ben, we went ahead and resolved this issue by back-porting some of the code from that feature branch into devel for the time-being, as we’re not ready to merge that feature branch in. If you refresh your development branches, you should get the new code.

Please let us know if you continue seeing any issues related to this.

Thanks!
