0

I am a newbie to Ansible and trying to figure out how to synchronize between 3 hosts based on condition. Below are details:

  • There are 3 nodes and based on which node is primary (I was able to get a custom fact set for it) need to delegate to that host (which runs synchronize from that host to other two hosts).
  • Custom fact basically is value 1 or 0 (1 means Primary/Master and 0 means not).
  • I am unable to get the hostname (primary hostname) value to include into the synchronize steps.

Below is the playbook I have. How do I make synchronize read the "primary" hostname value to run the next steps ?

---

- name: Playbook for rsync
  become: yes
  hosts: clients
  gather_facts: True

  tasks:
  - name: Task to rsync from Primary server
    synchronize:
      src: "{{ item.source }}"
      dest: "{{ item.dest }}"
    with_items:
      - { source: '/rsynctest/', dest: '/rsynctest/' }
      - { source: '/rsynctest1/', dest: '/rsynctest1/' }
    delegate_to: "{{ ansible_hostname }}"
    when:
      - ansible_local.determine.isprimary  == "1"

Thanks
-Raj

SmartTom
  • 691
  • 7
  • 14
RajV
  • 1

1 Answers1

1

It's not beginner level problem. The problem should be split into two parts: separation of the primary from secondaries, and delegation/execution.

In my experience the best way is to use dynamic groups [1]. They can be formed based on the specific value.

Here an example for grouping:

- hosts: cluster
  name: Grouping hosts
  gather_facts: false
  tasks:
    - name: group master hosts
      group_by:
        key: master
      when: master|d(False)

    - name: group slave hosts
      group_by:
        key: slave
      when: slave|d(False)

(You need to define slave/master variables, or adjust example as needed).

The next play in the playbook may use those groups.

 - name: Syncing
   hosts: slave
   tasks:
     - name: Sync to master
       command: echo syncing from slave
       delegate_to: '{{ groups.master[0] }}'

[1] https://docs.ansible.com/ansible/latest/modules/group_by_module.html

George Shuklin
  • 6,952
  • 10
  • 39
  • 80