---
-
  name: "Managing progress for {{ ansible_role_name }} (Step 1 of 1)"
  when: count_progress | default(false)
  run_once: true
  block:
    -
      name: "Incrementing global progress step counter for {{ ansible_role_name }} (Step 1)"
      ansible.builtin.set_fact:
        global_current_progress_step: "{{ global_current_progress_step | default(0) | int + 1 }}"

    -
      name: "Including progress reporting task for {{ ansible_role_name }} (Step 1)"
      ansible.builtin.include_role:
        name: startcloud.startcloud_roles.progress
      vars:
        _progress_role_is_setup_run: false
        current_progress_step: "{{ global_current_progress_step | default(0) }}"
        progress_description: "{{ progress_role_description | default('Processing ' + ansible_role_name + ' - Step 1') }}"

-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Installing HAProxy"
      ansible.builtin.apt:
        pkg:
          - haproxy
    
    -
      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 }}', 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: "Copying haproxy supervisord configuration file"
      when: ansible_module_running_in_container
      ansible.builtin.template:
        src: supervisord.haproxy.conf.j2
        dest: /etc/supervisor/conf.d/supervisord.haproxy.conf
        owner: root
        group: root
        mode: '0644'
    
    -
      name: "Copying System Information Dump On Start Script"
      when: ansible_module_running_in_container
      ansible.builtin.template:
        src: dump.sh.j2
        dest: /etc/haproxy/dump.sh
        owner: root
        group: root
        mode: 'a+x'
    
    -
      name: "Starting service haproxy"
      when: not ansible_module_running_in_container
      ansible.builtin.service:
        enabled: true
        name: haproxy
        state: restarted
      tags: haproxy
    
    #-
    #  name: "Restart supervisord to apply configuration"
    #  when: ansible_module_running_in_container
    #  ansible.builtin.service:
    #    name: supervisor
    #    state: restarted
