# Inventory vars in a playbook

**URL:** <https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056>\
**Category:** Ansible Project\
**Created:** [August 27, 2014, 7:46pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056 "2014-08-27T19:46:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Gailor](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeremy\_Gailor](https://forum.ansible.com/u/Jeremy_Gailor)\
**Post date:** [August 27, 2014, 7:46pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/1 "2014-08-27T19:46:58Z")

</div>

Can anyone explain to me if this just doesn’t work or if I’m doing something wrong;

/production  
[webapps:vars]  
default\_branch=master

/staging  
[webapps:vars]  
default\_branch=staging

/my\_playbook.yml

- hosts: webapps  
vars\_prompt:
- name: “release\_branch”  
prompt: “Deploy Branch:”  
default: “{{default\_branch}}”

Basically is just takes the text in ‘default’ verbatim (i.e. - no substitution). Given how Ansible puts itself into the deployment process, and how well that actually works, this is a really common use case that it feels like should be supported.

---

<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:** [August 27, 2014, 8:14pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/2 "2014-08-27T20:14:02Z")

</div>

vars\_prompt variables aren’t being expanded with inventory scoped variables, because the question is only going to be asked once. Therefore it’s storing the literal string value.

I really don’t recommend relying on vars\_prompt because playbooks really should be non-interative, “-e” extra variables also work a bit more nicely. You can still define defaults if the variables are not there using the “| default” Jinja2 function.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Gailor](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeremy\_Gailor](https://forum.ansible.com/u/Jeremy_Gailor)\
**Post date:** [August 27, 2014, 8:23pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/3 "2014-08-27T20:23:39Z")

</div>

So I’m actually using a when: condition to see it’s passed on the command line first, but —extra-args feels very clunky for specifying a branch when you want to release, and it is often the case that you have a specific branch you need to run in a particular environment, usually for testing.

- Jeremy

---

<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:** [August 27, 2014, 8:31pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/4 "2014-08-27T20:31:26Z")

</div>

You could also set it in a group variables file, for the staging or production group.

-e overrides inventory scope, so could be used to replace the default.

Alternatively, keep the vars prompt and do this in the task section after prompting for the variable I have named “prompted\_variable”, while setting the default prompt in the vars\_prompt to just ‘’.

- set\_fact: actual\_branch={{ prompted\_variable | default(variable\_from\_inventory) }}

---

<div class="post-metadata">

**Author:** ![Jeremy\_Gailor](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeremy\_Gailor](https://forum.ansible.com/u/Jeremy_Gailor)\
**Post date:** [August 27, 2014, 8:32pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/5 "2014-08-27T20:32:47Z")

</div>

To clarify, the full block is:

/my\_playbook.yml

- hosts: webapps  
vars\_prompt:
- name: “release\_branch”  
prompt: “Deploy Branch:”  
default: master  
when: not release\_branch

Ansible will skip this block if release\_branch is passed in --extra-args. Thinking it through, maybe it’s worth just having it passed in on the command line if it needs to be set and falling back to the inventory as a default. Thanks for the help.

---

<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:** [August 27, 2014, 8:37pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/6 "2014-08-27T20:37:06Z")

</div>

Yep, sounds good.

IIRC, “when” cannot be applied to a vars\_prompt.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Gailor](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeremy\_Gailor](https://forum.ansible.com/u/Jeremy_Gailor)\
**Post date:** [August 27, 2014, 8:38pm UTC](https://forum.ansible.com/t/inventory-vars-in-a-playbook/16056/7 "2014-08-27T20:38:20Z")

</div>

That is pretty weird, because “when” I tested it this morning I swear it skipped the prompt when I passed in —extra-vars “release\_branch=staging”.

- Jeremy
