# Managing Primary/Secondary relationship in cluster

**URL:** <https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684>\
**Category:** Ansible Project\
**Created:** [November 4, 2014, 11:59am UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684 "2014-11-04T11:59:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Morrison](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@Barry\_Morrison](https://forum.ansible.com/u/Barry_Morrison)\
**Post date:** [November 4, 2014, 11:59am UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/1 "2014-11-04T11:59:15Z")

</div>

I have a pair of servers (server1..domain, server2..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!!

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [November 4, 2014, 2:08pm UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/2 "2014-11-04T14:08:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Barry\_Morrison](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@Barry\_Morrison](https://forum.ansible.com/u/Barry_Morrison)\
**Post date:** [November 4, 2014, 10:06pm UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/3 "2014-11-04T22:06:45Z")

</div>

{{ hostvars[partner][‘ansible\_eth0’][‘ipv4’][‘address’] }} \<\< doesn’t work

“{‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘partner’ is undefined”, ‘failed’: True}”

in inventory/host\_vars/server1.domain there is ‘partner: server2.domain’

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [November 4, 2014, 10:15pm UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/4 "2014-11-04T22:15:51Z")

</div>

That indicates you haven’t defined “partner” properly. Make sure it’s listed in the right pace in your inventory host\_vars.

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [November 4, 2014, 10:17pm UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/5 "2014-11-04T22:17:34Z")

</div>

Ah, I didn’t see that last line. Do a:

- debug: var=partner

Just to make sure you have your variables loading properly.

---

<div class="post-metadata">

**Author:** ![Barry\_Morrison](https://avatars.discourse-cdn.com/v4/letter/b/3bc359/32.png) [@Barry\_Morrison](https://forum.ansible.com/u/Barry_Morrison)\
**Post date:** [November 4, 2014, 10:29pm UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/6 "2014-11-04T22:29:36Z")

</div>

I had the remnants of the aliases so host\_vars/server1 didn’t match ‘primary’

I’m not getting the var.

Thanks!

---

<div class="post-metadata">

**Author:** ![shirou](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@shirou](https://forum.ansible.com/u/shirou)\
**Post date:** [November 5, 2014, 1:34am UTC](https://forum.ansible.com/t/managing-primary-secondary-relationship-in-cluster/16684/7 "2014-11-05T01:34:32Z")

</div>

Another way is 'differenct'.  
(see [http://docs.ansible.com/playbooks\_variables.html#set-theory-filters](http://docs.ansible.com/playbooks_variables.html#set-theory-filters))

&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ (groups['pair'] | difference(inventory\_hostname))[0] }}"

The result is list, not a value. So it requires [0] to get actual hostname.  
It works if there are more hosts.
