---
# - name: (git.yml) include variables
#  include_vars: "{{ item }}"
#  with_first_found:
#    - "git-{{ inventory_hostname }}.yml"
#    - "git-{{ ansible_distribution_release }}.yml"
#    - "git-{{ ansible_distribution | lower }}.yml"
#    - git-default.yml
#  tags:
#    - git-default-repositories
#    - git-lxc-guest-repositories
#    - git-apache2-repositories
#    - git-nginx-repositories
#    - git-mysql-server-repositories
#    - git-postgresql-server-repositories
#    - git-nextcloud-server-repositories
#    - git-dns-server-repositories
#    - git-backup-server-repositories
#    - git-samba-server-repositories
#    - git-mailservers-repositories
#    - git-sympa-repositories
#    - git-other-repositories
# ---
#  Firewall repository
# ---

- name: (git.yml) Install/Update firewall repository
  ansible.builtin.git:
    repo: "{{  git_firewall_repository.repo }}"
    dest: "{{  git_firewall_repository.dest }}"
    version: >-
      {{
        git_firewall_repository.version
        | default(git_firewall_repository.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ git_firewall_repository.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  when: git_firewall_repository is defined and git_firewall_repository | length > 0
  tags:
    - git-firewall-repository

# ---
#  Default reposotories
# ---

- name: (git.yml) Install/Update default repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_default_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  tags:
    - git-default-repositories

# ---
#  Group [oopen_server] reposotories
# ---

- name: (git.yml) Install/Update oopen_server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_oopen_server_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['oopen_server']|string is search(inventory_hostname)"
  tags:
    - git-oopen-server-repositories

# ---
#  Group [warenform_server] reposotories
# ---

- name: (git.yml) Install/Update warenform_server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_warenform_server_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['warenform_server']|string is search(inventory_hostname)"
  tags:
    - git-warenform-server-repositories

# ---
#  Group [lxc_guest] reposotories
# ---

- name: (git.yml) Install/Update lxc_guest repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_lxc_guest_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['lxc_guest']|string is search(inventory_hostname)"
  tags:
    - git-lxc-guest-repositories

# ---
#  Group [lxc_host] reposotories
# ---

- name: (git.yml) Install/Update lxc_host repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_lxc_host_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['lxc_host']|string is search(inventory_hostname)"
  tags:
    - git-lxc-host-repositories

# ---
#  Group [gateway_server] reposotories
# ---

- name: (git.yml) Install/Update gateway repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_gateway_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['gateway_server']|string is search(inventory_hostname)"
  tags:
    - git-gateway-server-repositories

# ---
#  Group [apache2_webserver] reposotories
# ---

- name: (git.yml) Install/Update apache2 repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_apache2_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['apache2_webserver']|string is search(inventory_hostname)"
  tags:
    - git-apache2-repositories

# ---
#  Group [nginx_webserver] reposotories
# ---

- name: (git.yml) Install/Update nginx repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_nginx_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['nginx_webserver']|string is search(inventory_hostname)"
  tags:
    - git-nginx-repositories

# ---
#  Group [mysql_server] reposotories
# ---

- name: (git.yml) Install/Update mysql server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_mysql_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['mysql_server']|string is search(inventory_hostname)"
  tags:
    - git-mysql-server-repositories

# ---
#  Group [postgresql_server] reposotories
# ---

- name: (git.yml) Install/Update postgresql-server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_postgresql_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['postgresql_server']|string is search(inventory_hostname)"
  tags:
    - git-postgresql-server-repositories

# ---
#  Group [nextcloud_server] reposotories
# ---

- name: (git.yml) Install/Update nextcloud server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_nextcloud_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['nextcloud_server']|string is search(inventory_hostname)"
  tags:
    - git-nextcloud-server-repositories

# ---
#  Group [dns_server] reposotories
# ---

- name: (git.yml) Install/Update dns server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_dns_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['dns_server']|string is search(inventory_hostname)"
  tags:
    - git-dns-server-repositories

# ---
#  Group [backup_server] reposotories
# ---

# Optional repositories: tolerate transient network errors, but fail on config/auth/permission issues.

- name: (git.yml) Install/Update backup server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_backup_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['backup_server']|string is search(inventory_hostname)"
  failed_when: >-
    failed and (
      msg is not defined or (
        ('Could not resolve host' not in msg)
        and ('Connection timed out' not in msg)
        and ('Failed to connect to' not in msg)
        and ('Connection reset by peer' not in msg)
        and ('Operation timed out' not in msg)
      )
    )
  tags:
    - git-backup-server-repositories

# ---
#  Group [samba_server] reposotories
# ---

# Same policy as backup repositories: only temporary connectivity problems are accepted.

- name: (git.yml) Install/Update samba server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_samba_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['samba_server']|string is search(inventory_hostname)"
  failed_when: >-
    failed and (
      msg is not defined or (
        ('Could not resolve host' not in msg)
        and ('Connection timed out' not in msg)
        and ('Failed to connect to' not in msg)
        and ('Connection reset by peer' not in msg)
        and ('Operation timed out' not in msg)
      )
    )
  tags:
    - git-samba-server-repositories

# ---
#  Group [mail_server] reposotories
# ---

- name: (git.yml) Install/Update mail server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_mailserver_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['mail_server']|string is search(inventory_hostname)"
  tags:
    - git-mailservers-repositories

# ---
#  Group [sympa_list_servers] reposotories
# ---

- name: (git.yml) Install/Update sympa server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_sympa_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['sympa_list_server']|string is search(inventory_hostname)"
  tags:
    - git-sympa-repositories

# ---
#  Group [jitsi_meet_server] reposotories
# ---

- name: (git.yml) Install/Update sympa server repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  with_items: "{{ git_jitsi_meet_repositories }}"
  loop_control:
    label: "{{ item.name }}"
  when: "groups['jitsi_meet_server']|string is search(inventory_hostname)"
  tags:
    - git-jitsi_meet-repositories

# ---
#  Group [so36_server_dehydrated] reposotories
# ---

# - name: (git.yml) Install/Update sympa server repositories
#   git:
#     repo: '{{ item.repo }}'
#     dest: '{{ item.dest }}'
#   with_items: '{{ git_so36_dehydrated_repositories }}'
#   loop_control:
#     label: "{{ item.name }}"
#   when: "groups['so36_server_dehydrated']|string is search(inventory_hostname)"
#   tags:
#     - git-so36-dehydrated-repositories

# ---
#  Other (host specific) repositories
# ---

# Read in host specific vars file if exists
- name: (git.yml) Check for host specific git vars file
  ansible.builtin.stat:
    path: "vars/git-{{ inventory_hostname }}.yml"
  register: common_git_host_vars_file
  tags:
    - git-other-repositories

- name: (git.yml) Include only files matching git-<hostname>.yml (2.2)
  ansible.builtin.include_vars:
    file: "vars/git-{{ inventory_hostname }}.yml"
  when: common_git_host_vars_file.stat.exists
  tags:
    - git-other-repositories

- name: (git.yml) Install/Update other repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: >-
      {{
        item.version
        | default(item.branch)
        | default(
          'master'
          if (lookup('pipe', 'git ls-remote --heads ' ~ item.repo ~ ' master') | length > 0)
          else 'main'
        )
      }}

  loop_control:
    label: "{{ item.name }}"
  with_items: "{{ git_other_repositories }}"
  tags:
    - git-other-repositories
