---
-
  name: "Installing HAProxy and KeepAlived"
  ansible.builtin.apt:
    pkg:
      - haproxy
      - keepalived

-
  name: "Creating template and certifcate directories"
  ansible.builtin.file:
    mode: '0755'
    path: "{{ item }}"
    state: directory
  with_items:
    - "/etc/haproxy/errors/tpl"
    - "/etc/haproxy/errors/html"

-
  name: "Configuring haproxy"
  become: true
  tags: haproxy
  ansible.builtin.template:
    owner: "{{ item.owner }}"
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "a+x"
  loop:
    - { src: 'haproxy.cfg.j2', dest: '/etc/haproxy/haproxy.cfg', owner: 'haproxy' }
    - { src: 'tpl/400.http.j2', dest: '/etc/haproxy/errors/tpl/400.http', owner: 'haproxy' }
    - { src: 'tpl/403.http.j2', dest: '/etc/haproxy/errors/tpl/403.http', owner: 'haproxy' }
    - { src: 'tpl/404.http.j2', dest: '/etc/haproxy/errors/tpl/404.http', owner: 'haproxy' }
    - { src: 'tpl/408.http.j2', dest: '/etc/haproxy/errors/tpl/408.http', owner: 'haproxy' }
    - { src: 'tpl/500.http.j2', dest: '/etc/haproxy/errors/tpl/500.http', owner: 'haproxy' }
    - { src: 'tpl/502.http.j2', dest: '/etc/haproxy/errors/tpl/502.http', owner: 'haproxy' }
    - { src: 'tpl/503.http.j2', dest: '/etc/haproxy/errors/tpl/503.http', owner: 'haproxy' }
    - { src: 'tpl/504.http.j2', dest: '/etc/haproxy/errors/tpl/504.http', owner: 'haproxy' }
    - { src: 'html/400.html.j2', dest: '/etc/haproxy/errors/html/400.html', owner: 'haproxy' }
    - { src: 'html/403.html.j2', dest: '/etc/haproxy/errors/html/403.html', owner: 'haproxy' }
    - { src: 'html/404.html.j2', dest: '/etc/haproxy/errors/html/404.html', owner: 'haproxy' }
    - { src: 'html/408.html.j2', dest: '/etc/haproxy/errors/html/408.html', owner: 'haproxy' }
    - { src: 'html/500.html.j2', dest: '/etc/haproxy/errors/html/500.html', owner: 'haproxy' }
    - { src: 'html/502.html.j2', dest: '/etc/haproxy/errors/html/502.html', owner: 'haproxy' }
    - { src: 'html/503.html.j2', dest: '/etc/haproxy/errors/html/503.html', owner: 'haproxy' }
    - { src: 'html/504.html.j2', dest: '/etc/haproxy/errors/html/504.html', owner: 'haproxy' }

-
  name: "Starting service haproxy"
  ansible.builtin.service:
    enabled: true
    name: haproxy
    state: restarted
  tags: haproxy
