---

- name: Add apache vhosts configurations
  template:
    src="{{item.template | default('vhost.conf.j2')}}"
    dest="/etc/apache2/sites-available/{{ item.server_file_name }}.conf"
    owner=root group=root mode=644
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false
  tags:
    - apache_hosts

- name: Ensure apache SSL path exists
  file: path="{{apache_ssl_path}}" state=directory
  tags:
    - apache_dir_check

- name: Ensure document base exists
  file: path="{{item.document_root | dirname | realpath }}" state=directory
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false
  tags:
    - apache_dir_check

- name: Ensure document root exists (dir)
  file: path="{{item.document_root | realpath}}" state=directory
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false and item.link_to is not defined
  tags:
    - apache_dir_check

- name: Ensure document root exists (link)
  file: src={{item.link_to}} dest={{item.document_root}} state=link force=true
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false and item.link_to is defined
  tags:
    - apache_dir_check

---

- name: Add apache vhosts configurations
  template:
    src="{{item.template | default('vhost.conf.j2')}}"
    dest="/etc/apache2/sites-available/{{ item.server_file_name }}.conf"
    owner=root group=root mode=644
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false
  tags:
    - apache_hosts

- name: Ensure apache SSL path exists
  file: path="{{apache_ssl_path}}" state=directory
  tags:
    - apache_dir_check

- name: Ensure document base exists
  file: path="{{item.document_root | dirname | realpath }}" state=directory
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false and item.documentroot is defined
  tags:
    - apache_dir_check

- name: Ensure document root exists (dir)
  file: path="{{item.document_root | realpath}}" state=directory
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false and item.documentroot is defined and item.link_to is not defined
  tags:
    - apache_dir_check

- name: Ensure document root exists (link)
  file: src={{item.link_to}} dest={{item.document_root}} state=link force=true
  notify: Restart apache
  with_items: "{{apache_vhosts}}"
  when: apache_vhosts != false and item.documentroot is defined and item.link_to is defined
  tags:
    - apache_dir_check

Check for item.documentroot defined in apache_dir_check
Check for item.documentroot defined in apache_dir_check
