Hi,

Immediately after sending this mail I got an answer to this question from 
another source - a user called sivel on the #ansible IRC channel. I thought 
that I would put the answer here in case others hit a similar problem.

I had one of the [0] in the wrong place, which meant that I was looking for 
a key of [u'prod-LON-mon-01'] instead of u'prod-LON-mon-01'. The solution 
that Sivel gave me was to rewrite what I had as:

{{ 
hostvars[groups['prod']|intersect(groups['monitoring_servers'])|first].rax_addresses['internal-network'][0].addr
 
}}

He also included the use of |first which makes the selection clearer to 
read which is a bonus too.

Hope this helps someone else.

On Friday, 28 October 2016 19:09:12 UTC+1, Dan Rough wrote:
>
> Mehul / Karen,
>
> Did you ever get anywhere with this? I have just arrived at the same 
> problem and am looking for a solution too.
>
> When I run:
>
> ansible prod -i ./inventories/rax.py -c local -m debug -a var="{{ 
> hostvars[groups['prod']|intersect(groups['monitoring_servers'])][0].rax_addresses['internal-network'][0].addr
>  
> }}"
>
> I receive the error:
>
> "msg": "the field 'args' has an invalid value, which appears to include a 
> variable that is undefined. The error was: ansible.vars.hostvars.HostVars 
> object has no element [u'prod-LON-mon-01']"
>
> The intersect has correctly identified the server I wish to single out, 
> prod-LON-mon-01 so I think I'm pretty close to solving this myself. If I 
> hardcode that value in as hostvars['prod-LON-mon-01'] etc, etc, I get the 
> IP address I am after.
>
> I'm sure there's an obvious mistake that I've made, if you, or anyone else 
> for that matter is able to spot it and point it out, I'd be grateful.
>
> Thanks!
>
> Dan.
>
> On Friday, 6 November 2015 07:34:02 UTC, Mehul Ved wrote:
>>
>> Hi Karen,
>>   I already have these machines up and running and correctly tagged. I 
>> want to access hostvars of a machine from another machine. e.g.
>>  - I am on production web machine and need to get the database port, 
>> which is defined on db machine.
>>  - There are 2 db machines, production and beta, both of which have these 
>> variables.
>> In this scenario, how do I ensure that I'm accessing the database port 
>> variable from the correct machine?
>> If I define a variable in a role in my web playbook as
>>   app_db_port = hostvars[group['database']][0]['db_port']
>> then I'm not sure if I'm getting the db port of production machine or 
>> beta machine.
>>
>>
>> On Thu, Oct 29, 2015 at 7:18 AM, K Cheng <[email protected]> wrote:
>>
>>> Hey Mehul,
>>>
>>> I'm using dynamic inventory with AWS ec2 module. I had a quick glance at 
>>> your question so forgive me if I have the wrong idea in what you are asking.
>>>
>>> I had the scenario where I needed to get the number of instances that 
>>> were in the same environment and of the same role type and perform a count 
>>> of those instances.
>>>
>>> I managed to achieve it using this syntax (hopefully this will be of 
>>> some help to you for your scenario):
>>>
>>>
>>> - name: Set a count variable of existing instances in the same 
>>> environment AND role type
>>>   set_fact:
>>>     start_count: "{{ groups['tag_environment_' + env] | default('') | 
>>> intersect(groups['tag_role_' + instance_role] | default('')) | length}}"
>>>
>>>
>>> This allows me then tag instances in the same environment and the same 
>>> role type with an incrementing number: 
>>>
>>> - name: Tag instances using the correct sequence number for that 
>>> environment and role
>>>   ec2_tag:
>>>     profile: "{{ aws_account_name }}"
>>>     region: "{{ region }}"
>>>     resource: "{{ item.id }}"
>>>     tags:
>>>       Name: "{{ instance_role }}{{ 
>>> '%02d'|format(item.ami_launch_index|int + start_count|int + 1) }}.{{ env 
>>> }}.aws-{{ region_shortname[region] }}.{{ tld_name_internal }}"
>>>   with_items: "{{ ec2.instances | default({}) }}"
>>>
>>>
>>> For example, when I launch 2 more instance with environment of stage and 
>>> role of web, and there were already 2 existing instances, then I would get 
>>> web03.stage,domain and web04.stage.domain as the tag.
>>>
>>> Hope this helps you with your scenario.
>>>
>>> Karen
>>>
>>> On Friday, October 23, 2015 at 8:09:29 PM UTC+10, Mehul Ved 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 [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/6f009b56-7f9e-4ba5-9d3b-4468053ee85d%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/ansible-project/6f009b56-7f9e-4ba5-9d3b-4468053ee85d%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> -- 
>> 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 [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/f955ebe6-ff29-4a52-8753-87f65690e5f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to