---
- name: Install software
  package:
    name: "{{ item }}"
    state: latest
  with_items: "{{ worker_packages }}"

- name: Apache2 enable modules
  apache2_module:
    name: "{{ item }}"
    state: present
  with_items: "{{ worker_a2mods }}"
  notify: Restart Apache2

- name: Apache2 disable sites
  file:
    path: "/etc/apache2/sites-enabled/{{ item }}"
    state: absent
  with_items: "{{ worker_a2dissites }}"
  notify: Restart Apache2

- name: Apache2 create vhosts
  template:
    dest: "/etc/apache2/sites-available/{{ item }}"
    src: "etc/apache2/sites-available/{{ item }}.j2"
    mode: 0644
    backup: yes
  with_items: "{{ worker_a2ensites }}"
  notify: Restart Apache2

- name: Apache2 disable sites
  file:
    path: "/etc/apache2/sites-enabled/{{ item }}"
    src: "/etc/apache2/sites-available/{{ item }}"
    state: link
    force: yes
  with_items: "{{ worker_a2ensites }}"
  notify: Restart Apache2

- name: Apache2 create DocRoots
  file:
    path: "/{{ item }}"
    state: directory
    mode: 0755
  with_items: "{{ worker_a2docroots }}"
  notify: Restart Apache2

- name: PHP Install pools
  template:
    dest: "/{{ item }}"
    src: "{{ item }}.j2"
    mode: 0644
    backup: yes
  with_items: "{{ worker_phpfpmpools }}"
  notify: Restart PHP-FPM

- name: Disable worker in load balancers
  shell: "echo disable server nodes/{{ inventory_hostname }} | socat stdio /run/haproxy/admin.sock"
  delegate_to: "{{ item }}"
  with_items: "{{ groups.lb }}"

- name: Apache2 copy websites
  copy:
    dest: "/{{ item }}/"
    src: "{{ item }}/"
    backup: yes
  with_items: "{{ worker_a2docroots }}"

- name: Apache2 template dummy index.html
  template:
    dest: "/{{ item }}/index.html"
    src: "{{ item }}/index.html.j2"
    mode: 0644
    backup: yes
  with_items: "{{ worker_a2docroots }}"

- name: Sleep 30 seconds...
  pause: seconds=30

- name: Enable worker in load balancers
  shell: 'echo "enable server nodes/{{ inventory_hostname }}" | socat stdio /run/haproxy/admin.sock'
  delegate_to: "{{ item }}"
  with_items: "{{ groups.lb }}"