---
#--------------------------------------------------------------#
# Install keepalived                               [vip_install]
#--------------------------------------------------------------#
#- name: install keepalived
#  tags: [node_vip, vip_install]
#  package: name=keepalived state=present

#--------------------------------------------------------------#
# Check VIP config                                   [vip_check]
#--------------------------------------------------------------#
- name: check vip identity
  tags: [ node_vip, vip_check ]
  assert:
    that:
      - vip_address is defined and vip_address != ''
      - vip_vrid is defined and vip_vrid|int > 0 and vip_vrid|int < 255
      - vip_role|upper == 'MASTER' or vip_role|upper == 'BACKUP'
    fail_msg: vip_address & vip_vrid is required for keepalived

#--------------------------------------------------------------#
# Config keepalived                                 [vip_config]
#--------------------------------------------------------------#
- name: render node vip conf keepalived.conf
  tags: [ node_vip, vip_config, vip_refresh ]
  template:
    src: keepalived.conf.j2
    dest: /etc/keepalived/keepalived.conf
    mode: 0644
  vars:
    # JMESPath query to find all hosts with same vip_address:
    # [@.*] - flatten hostvars dict to array, [0] - unwrap outer array, [?...] - filter by vip_address
    vip_cluster_members_query: "[@.*][0][?vip_address=='{{ vip_address }}']"

#--------------------------------------------------------------#
# Launch keepalived                                 [vip_launch]
#--------------------------------------------------------------#
- name: launch keepalived service unit
  tags: [ node_vip, vip_launch ]
  systemd: name=keepalived enabled=true state=restarted

#--------------------------------------------------------------#
# Reload keepalived                                 [vip_reload]
#--------------------------------------------------------------#
- name: reload keepalived service unit
  tags: [ node_vip, vip_reload, vip_refresh ]
  systemd: name=keepalived enabled=true state=reloaded

...