---
- name: rename FluentdClient to Fluentd
  # https://bugzilla.redhat.com/show_bug.cgi?id=1540546
  replace:
    path: "{{ roles_data }}"
    regexp: '(\s+)- OS::TripleO::Services::FluentdClient$'
    replace: '\1- OS::TripleO::Services::Fluentd'
  when: overcloud_upgrade|bool

- name: remove ManilaBackendGeneric resource
  # https://bugzilla.redhat.com/show_bug.cgi?id=1541382
  replace:
    path: "{{ roles_data }}"
    regexp: '(\s+)- OS::TripleO::Services::ManilaBackendGeneric$'
    replace: ''

- name: check CephMgr service present
  shell: |
    grep -q 'OS::TripleO::Services::CephMgr' {{ roles_data }}
  register: ceph_mgr_found
  failed_when: false

- name: add CephMgr service
  lineinfile:
    path: "{{ roles_data }}"
    backrefs: true
    regexp: '(\s+)(- OS::TripleO::Services::CephMon$)'
    line: '\1- OS::TripleO::Services::CephMgr\n\1\2'
    insertbefore: '- OS::TripleO::Services::CephMon'
  when: ceph_mgr_found.rc == 1

# By default we have update_serial either unset at all for OSP13 and OSP14 or
# we have it set where Pacemaker enabled nodes, CephOSD nodes and Networkers
# have it set to 1. This is mostly defensive precaution and we do allow running
# in parallel for CephOSD and Networkers for production systems that did enough
# testing on preprod or can take small outage. We should also parallelize it in
# CI as we just waste time here.
#
# save roles_data
# remove all update_serial
# add update_serial: 25
# set update_serial to 1 on roles with OS::TripleO::Services::Pacemaker
# write new roles_data
- name: Ensure we set update_serial in roles data
  shell: |
    set -o pipefail
    if grep -q OS::TripleO::Services::Pacemaker "{{ roles_data }}" ; then
    cp -f "{{ roles_data }}" "{{ roles_data }}_update_serial"
    python -c 'import sys, yaml, simplejson as json;
    json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < "{{roles_data}}_update_serial" |\
    jq 'del(.[] | .update_serial)' |\
    jq 'map(. |= (. += {update_serial: 25}  ))' |\
    jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::Pacemaker"])
             then .update_serial = 1  else . end)' |\
    python -c 'import simplejson, sys, yaml;
    print yaml.dump(simplejson.loads(str(sys.stdin.read())),
    default_flow_style=False)' > "{{ roles_data }}"
    else
        echo "No service defined, not modifying the role file"
    fi
