This isn't working for me -hosts: {{ hostvars['localhost']['mylist'] }} : hostvars is undefined

829 views
Skip to first unread message

Larry Fast

unread,
Apr 12, 2016, 4:12:23 PM4/12/16
to Ansible Project

I'm trying to dynamically build the group of hosts that will be used in a play. I'm doing this within a   hosts: localhost   play.  However hostvars seems to be undefined when evaluating the - hosts: line.  Suggestions?

Stephen Granger

unread,
Apr 12, 2016, 4:51:35 PM4/12/16
to ansible...@googlegroups.com
Not being helpful, but re-enforcing the (mis)behaviour using Ansible 2.0.2

For my case I was calling sts_assume_role on localhost (hosts:localhost) for get a different aws id, key and token seeing it's only an api call, then setting then using set_facts to set variables. In another play in the same playbook I called it against another host (host:foobar) and tried to reference hostvars['localhost']['aws_cred_id'] .The variable is not defined was my result.

I managed to work around it just by calling the sts_assume_role task on the remote host.

Is this the expected behaviour? I thought magic variables could be used for this? is it only for retrieving group file variables?

http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

On 12 April 2016 at 13:12, Larry Fast <lfas...@gmail.com> wrote:

I'm trying to dynamically build the group of hosts that will be used in a play. I'm doing this within a   hosts: localhost   play.  However hostvars seems to be undefined when evaluating the - hosts: line.  Suggestions?

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cf79d4c8-480f-4120-8229-464666702a92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve

Larry Fast

unread,
Apr 12, 2016, 5:39:42 PM4/12/16
to Ansible Project
Thanks Steve.  This sounds very much like what I'm trying to do.  In my case I want to enforce some rules, defaults and aliases on a host group selected via --extra-vars. Thought I would do that with a localhost Play and set_facts, as you described. If this is the wrong approach I'm happy to get steered in the right direction. I started looking at dynamic inventory but this seems to be overkill for my current need. It also feels like the wrong direction. Another option I'm considering is a jinja2 function.  Eg.   - hosts: "{{ myj2func() }}


Stephen Granger

unread,
Apr 12, 2016, 5:47:19 PM4/12/16
to ansible...@googlegroups.com
Just want to make sure you've considered add_host


On 12 April 2016 at 14:39, Larry Fast <lfas...@gmail.com> wrote:
Thanks Steve.  This sounds very much like what I'm trying to do.  In my case I want to enforce some rules, defaults and aliases on a host group selected via --extra-vars. Thought I would do that with a localhost Play and set_facts, as you described. If this is the wrong approach I'm happy to get steered in the right direction. I started looking at dynamic inventory but this seems to be overkill for my current need. It also feels like the wrong direction. Another option I'm considering is a jinja2 function.  Eg.   - hosts: "{{ myj2func() }}


--
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/d/optout.



--
Steve

Larry Fast

unread,
Apr 12, 2016, 6:08:17 PM4/12/16
to Ansible Project
Hmm, looks promising but seems to be the opposite of what I want.  I would want to create a new group ID that references an existing group. Another solution would be to dynamically create an extra-var or similar root/global scoped variable. Is there anything like set_extra_var with behaviour similar to set_fact?  This is pushing back toward the functionality that works:

- hosts: "{{ extra_var }}"   I tested this and it works
- hosts: "{{ dynamically_created_extra_var }}"    Does this exist?
Reply all
Reply to author
Forward
0 new messages