0

I'm trying to write a role to configure a keepalived cluster. I was hoping to pass unique info into the a template based on the IP of the target box.

In this scenario: Server A is 192.168.1.140 and Server B is 192.182.1.141 and the VIP would be 192.168.1.142

the dictionary would look something like this:

---
  192.168.1.140:
    peer: 192.168.1.141
    priority: 110
    vip: 192.168.1.142

  192.1.168.1.141
    peer:192.168.1.140
    priority: 100
    vip: 192.168.1.142

I was hoping the task would look like this:

---
- name: keepalived template
  template:
    src: keepalived.j2
    dest: /etc/keepalived/keepalived.conf
    owner: root
    group: root
    mode: 0644
  with_dict: '{{ ansible_default_ipv4.address }}'

and the template would look like this:

}
vrrp_instance VI_1 {
interface    eth0
priority    {{ item.value.priority }}
...
unicast_scr {{ ansible_default_ipv4.address }}
unicast_peer {
     {{ item.value.peer }}
}
virtual_ipaddresses {
     {{ item.value.vip }} }
}

Any insight would be greatly appreciated John

1 Answers1

0

Group your peers details under some common dictionary:

---
peer_configs:
  192.168.1.140:
    peer: 192.168.1.141
    priority: 110
    vip: 192.168.1.142
  192.1.168.1.141
    peer:192.168.1.140
    priority: 100
    vip: 192.168.1.142

with_... is generally for looping, you don't need any loop, as I see, so use:

- name: keepalived template
  template:
    src: keepalived.j2
    dest: /etc/keepalived/keepalived.conf
    owner: root
    group: root
    mode: 0644
  vars:
    peer_config: '{{ peer_configs[ansible_default_ipv4.address] }}'

and template:

vrrp_instance VI_1 {
interface    eth0
priority    {{ peer_config.priority }}
...
unicast_scr {{ ansible_default_ipv4.address }}
unicast_peer {
     {{ peer_config.peer }}
}
virtual_ipaddresses {
     {{ peer_config.vip }} }
}
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193