# extra\_vars don't override inventory variables in templates (continuing #9242)

**URL:** <https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451>\
**Category:** Ansible Project\
**Created:** [October 7, 2014, 7:50am UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451 "2014-10-07T07:50:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 7, 2014, 7:50am UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/1 "2014-10-07T07:50:12Z")

</div>

Hi

I hope this is the right place to post this. I reported [https://github.com/ansible/ansible/issues/9242](https://github.com/ansible/ansible/issues/9242) a few days ago and I still have issues after it was closed. I’ll give a similar example to the one I gave in the issue, but using vars\_files and adding one more usecase:

inventory:

localhost ansible\_connection=local  
[all:vars]  
var=one

vars\_file:

var2: “{{var}}”

mapping:

one: 1  
two: 2

playbook:

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 7, 2014, 3:47pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/2 "2014-10-07T15:47:02Z")

</div>

Another case, which isn’t related to extra\_vars but to the root cause (according to my best guess) of evaluating the variables expressions too early:

inventory:

localhost ansible\_connection=local  
[all:vars]  
var=value

Playbook:

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 22, 2014, 12:48pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/3 "2014-10-22T12:48:18Z")

</div>

Since my [issue](https://github.com/ansible/ansible/issues/9393) was closed without a response and redirected me to the list, is it possible to get a response here?

---

<div class="post-metadata">

**Author:** ![Stephen\_Gargan](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Stephen\_Gargan](https://forum.ansible.com/u/Stephen_Gargan)\
**Post date:** [October 22, 2014, 7:10pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/4 "2014-10-22T19:10:35Z")

</div>

Hagai,

I had a similar issue and posted a pull request for it a while back. Its not been merged yet but you can grab it here

[https://github.com/ansible/ansible/pull/9106](https://github.com/ansible/ansible/pull/9106)

Try applying it and see if it fixes your issue too.

regards,

Steve.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 23, 2014, 8:22am UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/5 "2014-10-23T08:22:40Z")

</div>

Hi Stephen

First of all, thanks a lot. Your changes however did not solve my second case, the one using ‘default’ on inventory variables. So I guess some more tweaking is required. I’d love to help btw.

Something is also weird about your pull request - it’s not in its own branch, which makes it more difficult to pull to my repo (I had to copy the individual files manually!). I put them in a branch in my fork if you want: [https://github.com/hkariti/ansible/tree/premature\_template\_vars](https://github.com/hkariti/ansible/tree/premature_template_vars)

---

<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:** [October 24, 2014, 1:19pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/6 "2014-10-24T13:19:14Z")

</div>

Trying to sort through the email storm here 🙂

Yes, vars\_files and such will evaluate all the variables it can _So that they may be used_ if possible early, as that is often the case.

While it doesn’t help your question, I would try to not complicate your infrastructure so much, it’s hard to say from your theoretical example, but if you could post a more real world example of showing what you are trying to model, I think you might be doing something a little non-idiomatic.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 25, 2014, 11:13am UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/7 "2014-10-25T11:13:19Z")

</div>

Hi Michael, thanks for your response.

Before I describe my usecase I’d like to point out that the current behavior is a bug IMO, because it evaluates variables before all variable sources are looked at. I understand evaluating variables early so they can be used as soon as possible, but no task/play is run before all var sources are loaded anyway, so I don’t see a case when a variable is unavailable if evaluating is postponed in this case.

Now for my usecase. I have a play that creates ec2 instances. An ec2 instance needs a region to be created in, and a base image id (AMI id in ec2 terms).

I defined the default region to create instances as an inventory variable (as it’s different for prod and stage environments):

prod/group\_vars/all:

region: us-west-1

and I have a region\<-\>AMI id mapping in an external vars file:

amis:  
us-west-1: AMI1  
us-east-1: AMI2

And the play uses it like so:

vars:  
ami\_id: “{{amis[region]}}”

Problem is, if I want to run the playbook and override the region with -e I can’t, because ami\_id is evaluated before ‘-e’ takes effect.

I have a different usecase for my second example (the once with the |default line) if you want. If you think I didn’t model this case well, I’ll be glad to hear of a better way that solves my problem. Nevertheless, I still think one should expect the above way to work.

Thanks again.

---

<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:** [October 30, 2014, 5:53pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/8 "2014-10-30T17:53:55Z")

</div>

-e does bind pretty early, but it seems like you are using variables in paths to “vars\_files” that already resolve, so it selects what file to use earlier.

This is technically required to set globals in those files.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [October 30, 2014, 11:56pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/9 "2014-10-30T23:56:49Z")

</div>

> -e does bind pretty early, but it seems like you are using variables in paths to “vars\_files” that already resolve, so it selects what file to use earlier.

I don’t, actually. I don’t template the vars\_files path at all. All I do is use a variable that’s defined in a vars\_file in a regular vars section.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [November 6, 2014, 5:44pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/10 "2014-11-06T17:44:06Z")

</div>

Ok, I poked around a bit and indeed found one precedence issue and one premature templating issue:

In lib/ansible/runner/ **init**.py lines 612-613:

module\_vars\_inject = utils.combine\_vars(host\_variables, combined\_cache.get(host, {}))  
module\_vars\_inject = utils.combine\_vars(self.module\_vars, module\_vars\_inject)

module\_vars are overrided by host\_variables, although they are supposed to be of high precedence. They are being used in the correct order (module\_vars \> host\_variables) when building the inject var. Changing the order of precedence in the above lines solves my first use-case (the mapping override one).

In lib/ansible/playbook/play.py lines 89-90:

all\_vars = utils.combine\_vars(self.vars, self.playbook.extra\_vars)  
self.vars = template(basedir, self.vars, all\_vars)

play vars are templated without taking inventory variables into account. Removing these lines solves my second use-case (defining a variable in vars section based on inventory var with a default value).

make tests seem to pass. Is there a chance that someone with more knowledge of how vars behave can verify if my fixes are correct? Should I open a PR for this?

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:** [November 7, 2014, 2:22pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/11 "2014-11-07T14:22:54Z")

</div>

Would need to see your complete playbook.

Definitely don’t modify the code just yet 🙂

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [November 7, 2014, 2:37pm UTC](https://forum.ansible.com/t/extra-vars-dont-override-inventory-variables-in-templates-continuing-9242/16451/12 "2014-11-07T14:37:00Z")

</div>

I gave two example playbooks in this thread and in the [github issue](https://github.com/ansible/ansible/issues/9242) (same ones). Do you want a more ‘real world’ example?
