If you wanted to get the ip address of the first node in the 
production-cluster, you could do something like this.  Think of the group 
as a list and 0 being the first element in the list.

"{{ 
hostvars[groups['production-cluster'][0]]['ansible_eth0']['ipv4']['address'] 
}}"

as far as your second example goes, you have some syntax errors in defining 
your variable.  You don't need to encapsulate "partner" in curly brackets 
or quotes.  The following should work:


{{ hostvars[partner]['ansible_eth0']['ipv4']['address'] }} 


- James




On Tuesday, November 4, 2014 6:59:16 AM UTC-5, Barry Morrison wrote:
>
> I have a pair of servers (server1.<environment>.domain, 
> server2.<environment>.domain) in two environments (staging and production). 
> Server1 is primary, server2 is secondary. 
>
> When I originally built the playbook, I had aliases in the inventory file
>
> [staging-cluster]
> primary server1.staging.domain
> secondary server2.staging.domain
>
> And I'd target the IP of the other host with {{ 
> hostvars['primary']['ansible_eth0']['ipv4']['address'] }} to get the of the 
> "other" host. This worked fine, until I needed the playbook to target 
> another set of hosts in production:
>
> [production-cluster]
> primary server1.prod.domain
> secondary server2.prod.domain
>
> The problem with the above, aliases are global and you can't have more 
> than one in an inventory. So...this breaks the above lookup {{ 
> hostvars['secondary']['ansible_eth0']['ipv4']['address'] }}
>
> As much as I disliked the idea, I then thought to leverage 
> hostvars/serverX.environment.domain with a var like:
>
> primary: true
> partner: server2.environment.domain
>
> The above var would exist on server1 since it is the primary and it's 
> partner is server2
>
> The problem with the above is, I can't expand {{ partner }} in the above 
> fact (may not have explained that properly, I can't do this:
>
> {{ hostvars['{{ partner }}']['ansible_eth0']['ipv4']['address'] }} or this 
> {{ hostavars[{{partner}}]['ansible_eth0']['ipv4']['address'] }}
>
> Anyone have any ideas on how I can manage a primary/secondary 
> role/membership in a cluster? Actually defining if the host is 
> primary/secondary is the easy part...but defining it's partner and getting 
> facts from its partner, I'm not certain of. 
>
> Thanks!!
>

-- 
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/90de66f9-dfea-4611-8b01-a43bea3e940d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to