ansible-keepalived icon indicating copy to clipboard operation
ansible-keepalived copied to clipboard

how to set unicast mode

Open ragpm opened this issue 3 years ago • 5 comments

Hi,

Is it possible to set a list of unicast peers using this role ? I'm reading the documentation and can not find anything about that.

Thanks.

ragpm avatar May 16 '22 12:05 ragpm

found a way using like that:

unicast_src_ip: "{{ (groups['all'].index(inventory_hostname) == 0) | ternary(hostvars[groups['all'][0]].access_ip,hostvars[groups['all'][1]].access_ip) }}"
unicast_peers:
  - "{{ (groups['all'].index(inventory_hostname) == 0) | ternary(hostvars[groups['all'][1]].access_ip,hostvars[groups['all'][0]].access_ip) }}"

Is there anothe way to do that ?

ragpm avatar May 16 '22 13:05 ragpm

Hello,

It is a bit hard to know without seeing your group vars ;)

You can see more details about the implementation in https://github.com/evrardjp/ansible-keepalived/blob/0c9625bcc90104c72581af07bcb2e654fef14215/templates/keepalived.conf.j2#L117-L124

It looks like the if block would be templated with that. But there might be other ways, depends on your variables :)

evrardjp avatar May 16 '22 13:05 evrardjp

exact. My group is simple:

all:
  hosts:
    hap1:
      ansible_host: 10.0.2.31
      ip: 10.0.2.31
      access_ip: 10.0.2.31
      ansible_user: ansible
    hap2:
      ansible_host: 10.0.2.32
      ip: 10.0.2.32
      access_ip: 10.0.2.32
      ansible_user: ansible

My vars:

vrrp_nic: enp0s3

keepalived_sync_groups:
  haproxy:
    instances:
      - internal

keepalived_instances:
  internal:
    interface: "{{ vrrp_nic }}"
    nopreempt: true
    state: "{{ (groups['all'].index(inventory_hostname) == 0) | ternary('MASTER','BACKUP') }}"
    virtual_router_id: 42
    priority: "{{ (groups['all']|length - groups['all'].index(inventory_hostname)) * 250 // (groups['all']|length) }}"
    vips:
      - "10.0.2.30/24 dev {{ vrrp_nic }}"
    unicast_src_ip: "{{ (groups['all'].index(inventory_hostname) == 0) | ternary(hostvars[groups['all'][0]].access_ip,hostvars[groups['all'][1]].access_ip) }}"
    unicast_peers:
      - "{{ (groups['all'].index(inventory_hostname) == 0) | ternary(hostvars[groups['all'][1]].access_ip,hostvars[groups['all'][0]].access_ip) }}"

The code I found is working for 2 servers (and i don't need more servers).

ragpm avatar May 16 '22 13:05 ragpm

That should work indeed. I am not sure you need to define unicast_src_ip in your case (it depends on your nics). This could simplify a bit.

For the unicast_peer, there is probably another way to define the list based on all hostvars access_ip, but this should work, so don't worry :)

evrardjp avatar Jul 12 '22 08:07 evrardjp

Can we close this?

evrardjp avatar Jul 12 '22 08:07 evrardjp