---
- block:
    - name: Install the keepalived repository
      apt_repository: repo={{ keepalived_repo }} update_cache=yes
      when: ansible_distribution_major_version <= '16'

    - name: Install the keepalived package
      apt: name={{ keepalived_pkgs }} state={{ keepalived_pkg_state }} cache_valid_time=1800

    - name: Install the user that the keepalived scripts will run under
      user: name={{ keepalived_script_username }} home={{ keepalived_script_user_home }} createhome=no shell=/usr/sbin/nologin system=yes

    - name: Create the keepalive script user directory
      file: dest={{ keepalived_script_user_home }} state=directory owner={{ keepalived_script_username }} group={{ keepalived_script_username }} mode=0750

    - name: Install the keepalived configuration
      template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf owner=root group=root mode=0600
      notify: restart keepalived

    - name: Install the keepalived default check script
      template: src=keepalived_check_service_status.sh.j2 dest=/usr/local/bin/keepalived_check_service_status owner=root group=root mode=0755
      notify: restart keepalived

    - name: Install the keepalived notify scripts
      template: src=keepalived_notify.sh.j2 dest={{ item.notify }} owner=root group={{ keepalived_script_username }} mode=0754
      with_items: '{{ keepalived_instances }}'

    - name: Set the kernel net.ipv4.ip_nonlocal_bind sysctl
      sysctl: 
        reload: yes
        state: present
        name: net.ipv4.ip_nonlocal_bind
        sysctl_set: yes
        sysctl_file: /etc/sysctl.d/90-keepalived
        value: 1
      when: keepalived_non_local_bind

    - name: Disable the kernel net.ipv4.ip_nonlocal_bind sysctl if not needed
      sysctl: 
        reload: yes
        state: absent
        name: net.ipv4.ip_nonlocal_bind
        sysctl_set: yes
        sysctl_file: /etc/sysctl.d/90-keepalived
        value: 1
      when: not keepalived_non_local_bind

    - name: Ensure that keepalived is started and enabled
      service: name=keepalived state=started enabled=yes
      when: keepalived_enabled

    - name: Ensure that keepalived is stopped and disabled
      service: name=keepalived state=stopped enabled=no
      when: not keepalived_enabled

  tags: keepalived
  when: keepalived_install

- block:
    - name: Install the keepalived NRPE nagios check
      copy: src=check_keepalived_state dest={{ nagios_plugins_dir }}/check_keepalived_state  owner=root group=root mode=0555
      with_items: '{{ keepalived_instances }}'

    - name: Install the keepalived NRPE command configuration
      template: src=keepalived-nrpe.cfg.j2 dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg  owner=root group=root mode=0444
      notify: Reload NRPE server

  tags: keepalived
  when:
    - keepalived_install
    - keepalived_nagios_check

- block:
    - name: Remove the keepalived package if we do not want it
      apt: name={{ keepalived_pkgs }} state=absent

    - name: Remove the keepalived notify scripts
      file: dest={{ item.notify }} state=absent
      with_items: '{{ keepalived_instances }}'

    - name: Remove the keepalived NRPE check
      file: dest={{ nagios_plugins_dir }}/check_keepalived_state state=absent
      with_items: '{{ keepalived_instances }}'

    - name: Remove the keepalived NRPE command configuration
      file: dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg state=absent

  tags: keepalived
  when: not keepalived_install