---
- name: register is_upstream variable
  shell: |
    rpm -q openstack-tripleo-heat-templates | grep '\.el[[:digit:]]\.'
  failed_when: false
  register: is_upstream

- name: Check if overcloud deployed with pacemaker
  shell: |
    source {{ undercloud_rc }}
    openstack stack output show {{ overcloud_stack_name }} EnabledServices | grep 'pacemaker'
  failed_when: false
  register: pacemaker_present

- name: set deployment_with_ha fact
  set_fact:
    deployment_with_ha_upstream: "{{ is_upstream.rc == 0 and pacemaker_present.rc == 0 }}"

- name: assert CephOSD is enabled
  shell: |
    source {{ undercloud_rc }}
    openstack stack output show -f yaml {{ overcloud_stack_name }} EnabledServices | awk '/\s+- ceph_osd/'
  register: ceph_osd_service

- name: set ceph_osd_enabled fact
  set_fact:
    ceph_osd_enabled: "{{ (ceph_osd_service.stdout|length > 0) | ternary(true, false) }}"

- name: change puppet-ceph-external to ceph-ansible-external
  replace:
    dest: "{{ overcloud_deploy_script }}"
    regexp: "(.*){{ tht_directory }}/environments/{{ item.old }}(.*)"
    replace: '\1{{ tht_directory }}/environments/ceph-ansible/{{ item.new }}\2'
  with_items:
    - {'old': 'puppet-ceph-external.yaml', 'new': 'ceph-ansible-external.yaml'}
  when: not ceph_osd_enabled|bool

- name: get network-data-file
  shell: |
    awk '/-n\s/ || /--networks-file\s/ {gsub(/[[:space:]]/, "", $2); print $2}' {{ overcloud_deploy_script }}
  register: network_data_file

- name: set network_data_file fact
  set_fact:
    network_data_file: "{{ network_data_file.stdout }}"

- name: get env files used during deploy
  shell: |
    awk '/-e\s|--environment-file\s/ {gsub(/[[:space:]]/, "", $2); print $2}' {{ overcloud_deploy_script }}
  register: initial_env_file

- name: set initial_env_file fact
  set_fact:
    initial_env_file: "{{ initial_env_file }}"

- name: fetch roles-data file referenced in initial deploy script
  shell: |
    awk '/-r\s|--roles-file\s/ {gsub(/[[:space:]]/, "", $2); print $2}' {{ overcloud_deploy_script }}
  register: roles_data_file

- name: set roles-data fact
  vars:
    roles_data: "{{ roles_data_file.stdout }}"
  set_fact:
    roles_data: "{{ (roles_data_file.stdout) | ternary(roles_data, default_roles_data) }}"
    custom_roles_used: "{{ (roles_data_file.stdout) | ternary(true, false) }}"

- name: check if bm nodes are present
  shell: |
    source {{ undercloud_rc }} ;
    openstack baremetal node list -f value -c UUID
  register: bm_node_present

- name: set fact splitstack_deployment
  set_fact:
    splitstack_deployment: "{{ (bm_node_present.stdout_lines) | ternary(false, true) }}"
