---

# Check Ansible can read system info (some versions can't determine OS for newer OS)

- name: Check if Ansible can read System Info
  fail:
    msg: "Ansible version on the Nagios XI system is too low to run migration"
  when: ansible_os_family is not defined

# Verify we have argparse on EL6 systems

- name: Verify Python Modules (CentOS & RHEL 6)
  yum:
    name: python-argparse
    state: latest
  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int == 6

# Check if Python 3 is installed

- name: Check Python 3
  command: which python3
  register: python3_check
  ignore_errors: yes

# Check if Python 2 is installed (only if Python 3 is not found)

- name: Check Python 2
  command: which python
  register: python2_check
  when: python3_check.rc != 0
  ignore_errors: yes

# Run the bundler with either Python 2 or Python 3

- name: Run Configuration Bundler with Python 2
  script:
    chdir: /tmp
    cmd: /usr/local/nagiosxi/scripts/migrate/nagios_bundler.py {{ bundler_args }}
  when: python3_check.rc != 0 
  args:
    executable: "{{ python2_check.stdout }}"

- name: Run Configuration Bundler with Python 3
  script:
    chdir: /tmp
    cmd: /usr/local/nagiosxi/scripts/migrate/nagios_bundler.py {{ bundler_args }}
  when: python3_check.rc == 0
  args:
    executable: "{{ python3_check.stdout }}"

# Grab the bundle and move it into the Nagios XI temp

- name: Find bundle
  find:
    paths: /tmp
    patterns: "nagiosbundle-*.tar.gz"
  register: glob

- name: Fetch Bundle
  fetch:
    src: "{{ item.path }}"
    dest: /usr/local/nagiosxi/tmp/migrate/
    flat: yes
  with_items:
    - "{{ glob.files }}"

# Clean up Nagios Core server bundle

- name: Clean up bundle files
  file:
    path: "{{ item.path }}"
    state: absent
  with_items:
    - "{{ glob.files }}"
