---
- name: Check SSH connectivity and create group of reachable hosts
  hosts: rear_server
  gather_facts: false
  become: true

  tasks:
    - name: Check SSH connectivity
      ansible.builtin.ping:
      register: result
      failed_when: false

    - name: Clear unreachable host errors
      ansible.builtin.meta: clear_host_errors

    - name: Add reachable hosts to group
      ansible.builtin.group_by:
        key: reachable_hosts
      when:
        - result['ping'] is defined
        - "'pong' in result['ping']"


- name: Update NFS exports on reachable servers
  hosts: reachable_hosts
  become: true
  gather_facts: true

  vars_files:
    - bigboot_vars.yml

  tasks:
    - name: Import rear_vars role
      ansible.builtin.import_role:
        name: rhc.rear.rear_vars

    - name: Grab the template name
      ansible.builtin.set_stats:
        aggregate: false
        per_host: false
        data:
          nfs_exports_template_name: "{{ tower_job_template_name }}"

    - name: Create IP list and add to NFS exports
      ansible.builtin.include_tasks: tasks/rear_nfs_exports.yml
      loop: "{{ bigboot_data | dict2items }}"
      loop_control:
        label: "{{ item['key'] }}"
      when:
        - item['value']['bigboot_execute_bigboot'] | default(false) | bool
        - not rear_backup_skip | default(false) | bool
