- name: Register roles data file location if exists
  shell: "grep '\\-r\\ \\|\\-\\-roles' {{ overcloud_deploy_script }} | awk {'print $2'}"
  register: custom_roles_file
  ignore_errors: true

- name: Check if roles data has already been adjusted
  stat:
    path: "{{ custom_roles_file.stdout }}.pre_pike_upgrade"
  register: custom_roles_adjusted

- block:
    - name: Make a copy of the custom roles data file
      copy:
        src: "{{ custom_roles_file.stdout }}"
        dest: "{{ custom_roles_file.stdout }}.pre_pike_upgrade"
        remote_src: true

    - name: Assigns deprecated params to Controller role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: "^(- name: Controller( # the 'primary' role goes first)?$)"
        replace: "{{ item }}"
      with_items:
        - '\1\n  deprecated_param_image: "controllerImage"'
        - '\1\n  deprecated_param_flavor: "OvercloudControlFlavor"'
        - '\1\n  deprecated_param_extraconfig: "controllerExtraConfig"'
        - '\1\n  uses_deprecated_params: True'

    - name: Assigns network attributes to Controller role or custom controller roles
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Controller.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - External\n    - InternalApi\n    - Storage\n    - StorageMgmt\n    - Tenant'
        - '\1\n  tags:\n    - primary\n    - controller'
        - '\1\n  description: |\n    Controller role that has all the controler services loaded and handles\n    Database, Messaging and Network functions.'

    - name: Assigns deprecated params to Compute role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Compute$)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  deprecated_server_resource_name: "NovaCompute"'
        - '\1\n  deprecated_param_ips: "NovaComputeIPs"'
        - '\1\n  deprecated_param_scheduler_hints: "NovaComputeSchedulerHints"'
        - '\1\n  deprecated_param_metadata: "NovaComputeServerMetadata"'
        - '\1\n  deprecated_param_extraconfig: "NovaComputeExtraConfig"'
        - '\1\n  deprecated_param_image: "NovaImage"'
        - '\1\n  uses_deprecated_params: True'

    - name: Assigns network attributes to Compute role or custom compute roles
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Compute.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi\n    - Storage\n    - Tenant'
        - '\1\n  description: |\n    Basic Compute Node role'

    - name: Assigns new attributes to AltCompute role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: AltCompute.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi\n    - Storage\n    - Tenant'
        - '\1\n  description: |\n    Basic Compute Node role'

    - name: Assigns new attributes to BlockStorage role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: BlockStorage.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi\n    - Storage\n    - StorageMgmt'
        - '\1\n  description: |\n    Cinder Block Storage node role'

    - name: Assigns deprecated params to ObjectStorage role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: ObjectStorage$)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  deprecated_param_flavor: "OvercloudSwiftStorageFlavor"'
        - '\1\n  deprecated_param_image: "SwiftStorageImage"'
        - '\1\n  deprecated_param_ips: "SwiftStorageIPs"'
        - '\1\n  deprecated_param_metadata: "SwiftStorageServerMetadata"'
        - '\1\n  uses_deprecated_params: True'

    - name: Assigns network attributes to ObjectStorage role or custom object storage roles
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: ObjectStorage.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi\n    - Storage\n    - StorageMgmt'
        - '\1\n  description: |\n    Swift Object Storage node role'

    - name: Assigns new attributes to CephStorage role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: CephStorage.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - Storage\n    - StorageMgmt'
        - '\1\n  description: |\n    Ceph OSD Storage node role'

    - name: Assigns new attributes to Database role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Database.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi'
        - '\1\n  description: |\n    Standalone database role with the database being managed via Pacemaker'

    - name: Assigns new attributes to Galera role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Galera.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi'
        - '\1\n  description: |\n    Standalone database role with the database being managed via Pacemaker'

    - name: Assigns new attributes to Networker role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Networker.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi\n    - Tenant'
        - '\1\n  description: |\n    Standalone networking role to run Neutron agents'

    - name: Assigns new attributes to Messaging role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Messaging.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - InternalApi'
        - '\1\n  description: |\n    Standalone messaging role with RabbitMQ being managed via Pacemaker'

    - name: Assigns new attributes to Monitor role
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '^(- name: Monitor.*)'
        replace: "{{ item }}"
      with_items:
        - '\1\n  networks:\n    - Storage'
        - '\1\n  description: |\n    Ceph Monitor role'

    - name: Add services common to all roles introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::Timezone)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::CertmongerUser'
        - '\1\n    - OS::TripleO::Services::Docker'
        - '\1\n    - OS::TripleO::Services::Securetty'
        - '\1\n    - OS::TripleO::Services::Tuned'
        - '\1\n    - OS::TripleO::Services::ContainersLogrotateCrond'

    - name: Add CinderBackend services introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- .*CinderVolume)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::CinderBackendVRTSHyperScale'
        - '\1\n    - OS::TripleO::Services::CinderBackendDellEMCUnity'
        - '\1\n    - OS::TripleO::Services::CinderBackendDellEMCVMAXISCSI'

    - name: Add Clustercheck service introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::MySQL$)'
        replace: '\1\n    - OS::TripleO::Services::Clustercheck'

    - name: Add ExternalSwiftProxy service introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::SwiftProxy)'
        replace: '\1\n    - OS::TripleO::Services::ExternalSwiftProxy'

    - name: Add Iscsid service introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: "{{ item }}"
        replace: '\1\n    - OS::TripleO::Services::Iscsid'
      with_items:
        - '(- .*CinderVolume)'
        - '(- OS::TripleO::Services::NovaCompute)'

    - name: Add Neutron API services introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::NeutronApi)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::NeutronBgpVpnApi'
        - '\1\n    - OS::TripleO::Services::NeutronL2gwApi'

    - name: Add Neutron agents introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::NeutronL3Agent)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::NeutronL2gwAgent'
        - '\1\n    - OS::TripleO::Services::NeutronLbaasv2Agent'

    - name: Add Neutron agents introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- .*NeutronOvsAgent)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::NeutronVppAgent'
        - '\1\n    - OS::TripleO::Services::NeutronLinuxbridgeAgent'
        - '\1\n    - OS::TripleO::Services::Vpp'

    - name: Add NovaMigrationTarget service introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::NovaCompute)'
        replace: '\1\n    - OS::TripleO::Services::NovaMigrationTarget'

    - name: Add OVNController service introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::OVNDBs)'
        replace: '\1\n    - OS::TripleO::Services::OVNController'

    - name: Add Manila backend services introduced in Pike
      replace:
        dest: '{{ custom_roles_file.stdout }}'
        regexp: '(- OS::TripleO::Services::ManilaShare)'
        replace: "{{ item }}"
      with_items:
        - '\1\n    - OS::TripleO::Services::ManilaBackendIsilon'
        - '\1\n    - OS::TripleO::Services::ManilaBackendUnity'
        - '\1\n    - OS::TripleO::Services::ManilaBackendVMAX'
        - '\1\n    - OS::TripleO::Services::ManilaBackendVNX'
  when: custom_roles_file.stdout|length > 0 and not custom_roles_adjusted.stat.exists
