I've been trying to look up more on this but it seems like a dead end. In
fact, I see a few more question where people are using dynamic inventory
and multiple groups facing similar issues. I realized this isn't the only
place I'll have an issue, these kind of issues will also show up when
working with group_vars.

Is this not a recommended way of doing things? I've found atleast 3 other
questions on similar lines.
 - http://www.mattzone.com/ansible/ansible_20150821.html (by tupty)
 - https://groups.google.com/forum/#!topic/Ansible-project/QuqXUV_BEMM
 -
https://www.mail-archive.com/ansible-project@googlegroups.com/msg08024.html

Is there a better way to handle grouping of servers? Is there anything in
the documentation or best practices that I've missed?


On Mon, Oct 26, 2015 at 12:13 PM, Mehul Ved <mehul.n....@gmail.com> wrote:

> Hi Bryan,
>  Here's what I get when I run the command
>
> $ RAX_CREDS_FILE=.raxpub ansible web -i inventory/rax.py -m debug -a
> "msg={{ hostvars[groups['beta']|intersect(groups['db'])[0]]['db_port'] }}"
> --limit beta -v
> beta_web1 | FAILED => Failed to template msg={{
> hostvars[groups['beta']|intersect(groups['db'])[0]]['db_port'] }}: template
> error while templating string: expected token ',', got '['
>
> $ RAX_CREDS_FILE=.raxpub ansible web -i inventory/rax.py -m debug -a
> "msg={{ hostvars[groups['beta']|intersect(groups['db'])[1]]['db_port'] }}"
> --limit beta -v
> beta_web1 | FAILED => Failed to template msg={{
> hostvars[groups['beta']|intersect(groups['db'])[1]]['db_port'] }}: template
> error while templating string: expected token ',', got '['
>
> $ RAX_CREDS_FILE=.raxpub ansible web -i inventory/rax.py -m debug -a
> "msg={{ hostvars[groups['beta']|intersect(groups['db'])[2]]['db_port'] }}"
> --limit beta -v
> beta_web1 | FAILED => Failed to template msg={{
> hostvars[groups['beta']|intersect(groups['db'])[2]]['db_port'] }}: template
> error while templating string: expected token ',', got '['
>
> notice the increment in the index but it still gets the same error.
>
> If I omit the index to see what is there,
>
> $ RAX_CREDS_FILE=.raxpub ansible web -i inventory/rax.py -m debug -a
> "msg={{ hostvars[groups['beta']|intersect(groups['db'])]['db_port'] }}"
> --limit beta -v
> beta_web1 | FAILED => One or more undefined variables:
> ansible.runner.HostVars object has no element [u'beta_db1']
>
> I'm not sure what I'm missing out here.
>
> On Sat, Oct 24, 2015 at 12:48 AM, Brian Coca <bc...@ansible.com> wrote:
>
>> actually [u'beta_db1'] is a list with 1 string item inside, the debug
>> construct you are using is mixing templated and non templated vars,
>> that won't work also you seem to have mismatched list indecies.
>>
>> debug: msg="{{hostvars[group['beta'] |
>> intersection(groups['db'])[0]]['db_port']}}"
>> or
>> debug: var=hostvars[group['beta'] |
>> intersection[groups['db'])[0]]['db_port']
>>
>> On Fri, Oct 23, 2015 at 6:09 AM, Mehul Ved <mehul.n....@gmail.com> wrote:
>> > I have 4 machines on rackspace - two sets of machines, for production
>> and
>> > beta. Each set has a web server and db server.
>> > Here's what the metadata for each machine would look like
>> >
>> > metadata: {
>> >   group: web,
>> >   set: production
>> > }
>> >
>> > metadata: {
>> >   group: db,
>> >   set: production
>> > }
>> >
>> > metadata: {
>> >   group: web,
>> >   set: beta
>> > }
>> >
>> > metadata: {
>> >   group: db,
>> >   set: beta
>> > }
>> >
>> > While deploying to machine set beta and group web, I need to get certain
>> > variables from machine set beta and group db. I have been trying various
>> > ways to ensure I get the right machine. I got a pointer from somebody
>> on IRC
>> > to try intersection filter to get the correct machine from the group.
>> >
>> > So, I did this
>> >   debug: var='groups['beta'] | intersection(groups['db'])'
>> >
>> > The above gives me a string with machine name
>> > "[u'beta_db1']"
>> >
>> > I am unable to use it as shown in variables example
>> >   debug: var='hostvars[{{ group['beta'] | intersection['groups['db'])
>> > }}][0]]['db_port']
>> >
>> > It returns the result as
>> > "hostvars[[u'beta_db'][0]]['db_port']"
>> >
>> > I intend to use this in my vars/main.yml as
>> >
>> > app_db_port: <the variable defined for beta db machine>
>> >
>> > Am I in the right direction? If so how to handle the issue where
>> hostvars
>> > needs a list but intersection returns a string? Or is there a better
>> way to
>> > achieve what I want?
>> >
>> >
>> > --
>> > With Regards,
>> > Mehul Ved
>> >
>> > --
>> > 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-project+unsubscr...@googlegroups.com.
>> > To post to this group, send email to ansible-project@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> https://groups.google.com/d/msgid/ansible-project/CADTL0oYm9Uziv4em%3DYgSxY7SR3MG72yUme%2BJmfy0Q8%3DsXCG2Zg%40mail.gmail.com
>> .
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Brian Coca
>>
>> --
>> 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-project+unsubscr...@googlegroups.com.
>> To post to this group, send email to ansible-project@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/CAJ5XC8m_gsuYNH2hE74v5AVnpO2XquUf6aODzvRpToDWf6mfxg%40mail.gmail.com
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> With Regards,
> Mehul Ved
>



-- 
With Regards,
Mehul Ved

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CADTL0oYqsD1bWzCxHH8EPKfwxAF4oV7UEXhQ_LcoBbduzy8acQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to