How to automatically handle slight difference in configuring Master-slave setup

14 views
Skip to first unread message

ishan jain

unread,
Mar 2, 2017, 4:19:38 AM3/2/17
to Ansible Project
I am preparing some Ansible scripts for a service that works in a master/slave setup. There is a single docker image for both but the master/slave behavior will be acquired after configuring some environment variables.

When i explicitly setup each container, this is roughly how my playbook would look:

 ---
- hosts: host1,host2
  gather_facts: yes
  tasks:
docker_container:
name: "{{prefix}}_container"
image: "{{image_name}}"
state: started
env:
M1: V1
M2: {{IP_address_of_the_other_one}}
when: master
docker_container:
name: "{{prefix}}_container"
image: "{{image_name}}"
state: started
env:
S1: SV1
S2: {{IP_address_of_the_other_one}}
when: slave







What i want to do is to automate this in my playbook where Ansible would automatically assign one value set to one host and second value set to the other one. This will help me reduce the number of variables a person has to configure. So how can i formulate a condition where i would apply M1 and M2 for one host and S1 and S2 for the other one ?

Dave Cottlehuber

unread,
Mar 2, 2017, 2:35:39 PM3/2/17
to ansible...@googlegroups.com
On Thu, 2 Mar 2017, at 10:19, ishan jain wrote:
> I am preparing some Ansible scripts for a service that works in a
> master/slave setup. There is a single docker image for both but the
> master/slave behavior will be acquired after configuring some environment
> variables.
>
> When i explicitly setup each container, this is roughly how my playbook
> would look:

I've dealt with this in 2 different ways in the past depending on scale.
In both cases I have a single group (call it lb for loadbalancer) with
the nodes in it, but use dynamic lookups for the latter more complex
scenario.

In one case, I used per-host variables - effectively a manual
declaration:

/p/i/ansible ╍{} cat host_vars/b*
---
config:
cluster:
primary: "{{ inventory_hostname }}"
backup: b1.local
---
config:
cluster:
primary: "{{ inventory_hostname }}"
backup: b2.local
---

And then these can be reference in the usual way.

The other approach, a little more sophisticated and ideal for larger
setups, I can describe but don't have the code handy.

- all hosts (in your case just 2) are listed in DNS or some other
service discovery tool.
- you retrieve via lookup[1] which returns a list
- you filter the list to remove this node's IP address or hostname
- iterate over the remaining list items to populate your config, in my
case multiple load balance entries

A+
Dave

[1]:
http://docs.ansible.com/ansible/playbooks_lookups.html#the-dns-lookup-dig
Reply all
Reply to author
Forward
0 new messages