Sorry for necro'ing an old thread, but I'm having a similar problem: I'm 
playing with an EC2 inventory, and I have several "environments", with 
machines in these environments having a specific "class".  Is there a way 
to template the ip of the machine that has both 'tag_environment_myenv' and 
'tag_class_database'?


On Friday, July 12, 2013 11:33:35 AM UTC-7, Michael DeHaan wrote:
>
> I would be inclined to do this in inventory:
>
> [core:children]
> core-cluster1
> core-cluster2
>
> [core-cluster1]
> asdf.example.com
> bar.example.com
>
> [core-cluster2]
> baz.example.com
> glorp.example.com
>
> Then you can just specify "core-cluster2" as  the group name.
>
>
>
> On Fri, Jul 12, 2013 at 11:11 AM, Tristan Zajonc 
> <tris...@gmail.com<javascript:>
> > wrote:
>
>> Michael,
>>
>> Right, but I need the core1 associated with a particular cluster.  This 
>> is why I was trying to use an intersection. But I can't easily use the 
>> intersection syntax, hosts: core1&:$cloud, to select from hostvars.  As a 
>> result, I've tried renaming my inventory so that they are named 
>> ${cloud}core1.  Then I select variables with with hostvars[groups[cloud + 
>> "core1"][0]]['variable_name'].  Is there a better way?
>>
>> Tristan
>>
>>
>>
>> On Fri, Jul 12, 2013 at 11:04 AM, Michael DeHaan 
>> <mic...@ansibleworks.com<javascript:>
>> > wrote:
>>
>>> "I am struggling with the best way to access variables of another host, 
>>> namely the ip address of core1,core2,core3"
>>>
>>> This should be all you need to get the first host in "core1"
>>>
>>> hostvars[groups["core1"][0]]["variable_name"]
>>>
>>>
>>>
>>>
>>>
>>> On Fri, Jul 12, 2013 at 10:48 AM, Tristan Zajonc 
>>> <tris...@gmail.com<javascript:>
>>> > wrote:
>>>
>>>> Michael,
>>>>
>>>> Yes, I figure I'm not doing this the "Ansible Way" and my eyes are 
>>>> bleeding as well.  I'm writing Ansible scripts to manage multiple Hadoop 
>>>> clusters.  I had 4 roles: core1, core2, core3, and slaves.  I organized my 
>>>> inventory into these groups and also had a "cloud" group, which is just a 
>>>> particular Hadoop cluster.  I would then select things like 
>>>> "hosts:core1:&$cloud" where I pass in $cloud as a command line extra-vars. 
>>>>  I've tried changing my inventory scheme and use "hosts: ${cloud}core1", 
>>>> but this also is awkward.
>>>>
>>>> I am struggling with the best way to access variables of another host, 
>>>> namely the ip address of core1,core2,core3.  I do not have fixed 
>>>> hostnames, 
>>>> so I was trying to use groups.  I'm not sure how to do it in the first 
>>>> inventory scheme, because can't do intersections, but in the second I 
>>>> could 
>>>> do:
>>>>
>>>> {{ 
>>>> hostvars[groups[cloud+'core1'][0]]['ansible_eth0']['ipv4']['address'] }}
>>>>
>>>> I feel like I'm missing some way to define dynamic global variables.  I 
>>>> looked at set_fact, but it doesn't seem to stick around for my entire 
>>>> playbook.  Is there a way to set a global variable that persists across 
>>>> the 
>>>> entire playbook.yml (multiple hosts: selections).  Or is there a better 
>>>> way 
>>>> to get some dynamic facts and use them across hosts?
>>>>
>>>> Feedback very much appreciated.
>>>>
>>>> Tristan
>>>>
>>>> On Thursday, July 11, 2013 7:34:01 PM UTC-4, Michael DeHaan wrote:
>>>>
>>>>>  This makes my eyes kind of bleed.
>>>>>
>>>>> In general when you are modelling something and it gets this ugly, 
>>>>> step back and think about the use case -- things in Ansible should never 
>>>>> look like this.    The project exists to avoid such things :)
>>>>>
>>>>> "I use intersections to select roles within the cloud" doesn't make 
>>>>> sense to me, unfortunately, can you explain more and show your command 
>>>>> line 
>>>>> use case and a bit more info?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Thu, Jul 11, 2013 at 3:30 PM, Tristan Zajonc <tris...@gmail.com>wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I have an inventory setup where instances have roles with a cloud. 
>>>>>>  Because there are multiple clouds I use intersections to select roles 
>>>>>> within a cloud, and pass in the cloud name as an extra variable at the 
>>>>>> command line. This seems to work well for selecting hosts, but I'm 
>>>>>> struggling to use intersections in templates or variables.  For 
>>>>>> instance, I 
>>>>>> have three core nodes and I'd like to put the IP addresses in 
>>>>>> /etc/hosts. 
>>>>>>  I have been trying variations of:
>>>>>>
>>>>>> {{hostvars[set(groups['Core1']).intersection(set(groups[
>>>>>> cloud])).pop()].ansible_eth0.ipv4.address}}
>>>>>>
>>>>>> but nothing works.
>>>>>>
>>>>>> Alternatively, is there a way for hosts to register global variables? 
>>>>>>  In that case I'd just connect and register these up front.
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> Tristan
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>  -- 
>>>>>> 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.
>>>>>>
>>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>>  
>>>>>>  
>>>>>>
>>>>>
>>>>>  -- 
>>>> 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 <javascript:>.
>>>>
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>  
>>>>  
>>>>
>>>
>>>
>>>
>>> -- 
>>> Michael DeHaan <mic...@ansibleworks.com <javascript:>>
>>> CTO, AnsibleWorks, Inc.
>>> http://www.ansibleworks.com/
>>>
>>>  -- 
>>> You received this message because you are subscribed to a topic in the 
>>> Google Groups "Ansible Project" group.
>>> To unsubscribe from this topic, visit 
>>> https://groups.google.com/d/topic/ansible-project/QuqXUV_BEMM/unsubscribe
>>> .
>>> To unsubscribe from this group and all its topics, send an email to 
>>> ansible-proje...@googlegroups.com <javascript:>.
>>>
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>  
>>>  
>>>
>>
>>  -- 
>> 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 <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>  
>>  
>>
>
>
>
> -- 
> Michael DeHaan <mic...@ansibleworks.com <javascript:>>
> CTO, AnsibleWorks, Inc.
> http://www.ansibleworks.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-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/6808b071-0576-4513-bedd-f4e6751770ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to