I would use group_by to create a dynamic group first in a prior play, like
so:

- hosts: webservers
  tasks:
     - group_by: name=webserver-{{ city | default('no_city') }}

And then you could use, in the template, replace the groupname 'webserver'
with 'webserver-amsterdam' like so:

groups['webserver-amsterdam'][0]

to select the first one.

Others may prefer deep Jinja2 magic, but I'd find that more readable.   You
can also use the dynamic group for other types of management in Ansible:

- hosts: webservers-amsterdam
  tasks:
     - yum: name=foo state=latest

etc









On Wed, Jul 30, 2014 at 8:06 AM, Piet83 <[email protected]> wrote:

> Thank you Michael for the right syntax :)
>
> Is it also possible to select one host in the goup array based on a city
> defined as variable for a particular host? In other words, I don't know on
> what position in the array the servername is I need. I only know it is in
> group databaseserver and that it has amterdam defined as variable city.
>
> By the way if I try this in a template:
>
> {{ hostvars[groups['databaseserver'][0]].ansible_eth0.ipv4.address }}
>
>
>
> I get the following error running a playbook with the above in a template:
> fatal: [localhost] => {'msg': "One or more undefined variables: 'dict'
> object has no attribute 'ansible_eth0'", 'failed': True}
> fatal: [localhost] => {'msg': "One or more undefined variables: 'dict'
> object has no attribute 'ansible_eth0'", 'failed': True}
>
> FATAL: all hosts have already failed -- aborting
>
>
>
>
>
>
>
> Op dinsdag 29 juli 2014 17:07:57 UTC+2 schreef Michael DeHaan:
>>
>> The above imaginary syntax will not work, but this will :)  The groups
>> array contains the names of each host in each group, and this picks out the
>> first one in inventory order.
>>
>> {{ hostvars[groups['databaseserver'][0]].ansible_eth0.ipv4.address }}
>>
>> If using a template file versus inline in the playbook, I'd do this:
>>
>> {# set db_head_node = groups['databaseserver'][0] #}
>> {{ hostvars[db_head_node].blarg }}
>>
>> for readability purposes
>>
>>
>>
>>
>> On Tue, Jul 29, 2014 at 11:02 AM, Piet83 <[email protected]> wrote:
>>
>>> Ok but what if I don't know the server name but only that it is an
>>> database server in amsterdam?
>>>
>>> Something like this, but then with the right syntax?
>>>
>>> {{ groups["databaseserver"] | {{ hostvars["amsterdam"] }} | {{
>>> ansible_eth0.ipv4.address }} }}
>>>
>>> Op dinsdag 29 juli 2014 14:23:04 UTC+2 schreef Brian Coca:
>>>
>>>> just do  {{ city }}​, if it is the 'current host', to access vars of
>>>> other hosts {{ hostvars['server3'].city }}
>>>>
>>>  --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/ansible-project/0ec5b636-9383-4a80-9c51-
>>> 28831b33406a%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/0ec5b636-9383-4a80-9c51-28831b33406a%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1cfae46f-eeea-4936-9cfe-1b9313c9a45f%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1cfae46f-eeea-4936-9cfe-1b9313c9a45f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgw1DKXcyLWrMGPbOUnfLq-iyeBrCxh%3DqLGvNzrj24Lasw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to