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

261 views
Skip to first unread message

anatoly techtonik

unread,
Feb 21, 2014, 2:56:05 PM2/21/14
to ansible...@googlegroups.com
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] **************************************************************
...

??? 

Scott Sturdivant

unread,
Feb 21, 2014, 2:59:36 PM2/21/14
to ansible...@googlegroups.com
Change to default('localhost').


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

anatoly techtonik

unread,
Feb 21, 2014, 3:08:30 PM2/21/14
to ansible...@googlegroups.com
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] **** ?

anatoly techtonik

unread,
Feb 21, 2014, 3:09:56 PM2/21/14
to ansible...@googlegroups.com
@Scott, thanks. Trial and error told me that way too.

Michael DeHaan

unread,
Feb 21, 2014, 3:59:28 PM2/21/14
to ansible...@googlegroups.com
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.


anatoly techtonik

unread,
Feb 23, 2014, 1:08:16 AM2/23/14
to ansible...@googlegroups.com
On Friday, February 21, 2014 11:59:28 PM UTC+3, Michael DeHaan wrote:
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.

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

Michael DeHaan

unread,
Feb 23, 2014, 8:54:39 AM2/23/14
to ansible...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages