# Default value for \`hosts: {{ host }}\` substitution doesn't work

**URL:** <https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625>\
**Category:** Ansible Project\
**Created:** [February 21, 2014, 7:56pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625 "2014-02-21T19:56:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [February 21, 2014, 7:56pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/1 "2014-02-21T19:56:05Z")

</div>

I try to set default value for hosts field in playbook with Jinja2 filter, but it doesn’t work:

> * * *
> 
> - hosts: “{{ host | default(localhost) }}”  
> tasks:
> - name: show OS version info  
> debug: msg=“{{ansible\_distribution}} {{ansible\_distribution\_version}}”

> # ansible-playbook -i hosts dist.yml
> 
> PLAY [{{host | default(localhost)}}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> skipping: no hosts matched

But when I execute it with command line parameter, it works ok:

> # ansible-playbook -i hosts dist.yml --extra-vars ‘host=localhost’
> 
> PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> …

???

---

<div class="post-metadata">

**Author:** ![Scott\_Sturdivant](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Scott\_Sturdivant](https://forum.ansible.com/u/Scott_Sturdivant)\
**Post date:** [February 21, 2014, 7:59pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/2 "2014-02-21T19:59:36Z")

</div>

Change to default(‘localhost’).

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [February 21, 2014, 8:08pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/3 "2014-02-21T20:08:30Z")

</div>

Found the reason myself:

> > * * *
> > 
> > - hosts: “{{ host | default(localhost) }}”

This should be:

> * * *

> - hosts: “{{ host | default(‘localhost’) }}”

Still would be nice to get more valuable error message. I guess Jinja2 translates bare localhost to variable name, which in turn is translated to empty string, but why the Ansible shows PLAY [{{host | default(localhost)}}] \*\*\*\* and not PLAY \*\*\*\* or PLAY [#error] \*\*\*\* ?

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [February 21, 2014, 8:09pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/4 "2014-02-21T20:09:56Z")

</div>

@Scott, thanks. Trial and error told me that way too.

---

<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:** [February 21, 2014, 8:59pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/5 "2014-02-21T20:59:28Z")

</div>

If you are attempting to drive the host spec based on inventory variables, this won’t work.

If you drive it with input from -e, it will.

You can’t use inventory variables to drive the host spec.

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [February 23, 2014, 6:08am UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/6 "2014-02-23T06:08:16Z")

</div>

Why? Isn’t the initialization order sets inventory variables first?

---

<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:** [February 23, 2014, 1:54pm UTC](https://forum.ansible.com/t/default-value-for-hosts-host-substitution-doesnt-work/14625/7 "2014-02-23T13:54:39Z")

</div>

This is documented here:

[http://docs.ansible.com/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](http://docs.ansible.com/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)
