---
- name: generate overcloud ansible static yaml inventory file
  shell: |
    source {{ undercloud_rc }}
    /usr/bin/tripleo-ansible-inventory \
        --plan {{ overcloud_stack_name }} \
        {% if overcloud_ssh_user != '' %} --ansible_ssh_user {{ overcloud_ssh_user }} {% endif %} \
        --static-yaml-inventory {{ working_dir }}/tripleo-ansible-inventory-static.yaml

- name: register deployed roles list
  shell: |
    awk -F':' '/role_name/ {gsub("[[:space:][:punct:]]", "", $2); print $2}' {{ working_dir }}/tripleo-ansible-inventory-static.yaml
  register: oc_roles

- name: store roles
  vars:
    controller_role_name: "{{ oc_roles.stdout|regex_search('[A-Za-z0-9]*[Cc]ontroller[A-Za-z0-9]*') }}"
  set_fact:
    oc_roles: "{{ [controller_role_name] +  oc_roles.stdout_lines|difference([controller_role_name]) }}"

- name: create FFU role ugrades scripts
  template:
    src: fast-forward-upgrade/overcloud_upgrade_role.sh.j2
    dest: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
    mode: 0775
  tags: ffu_overcloud_upgrade_controller
  with_items: "{{ oc_roles|default([]) }}"

- include: ../common/l3_agent_connectivity_check_start_script.yml
  tags: ffu_overcloud_upgrade_controller

- name: pre controller role upgrade steps workarounds
  command: "{{working_dir}}/pre_ffu_overcloud_upgrade_workarounds.sh"
  when: ffu_upgrade_workarounds|bool
  tags: ffu_overcloud_upgrade_controller

- name: run FFU controller ugrade scripts
  command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
  tags: ffu_overcloud_upgrade_controller
  with_items:
    - "{{ oc_roles|intersect(['Controller']) }}"

- include: "{{ item }}"
  with_items:
    - '../common/l3_agent_connectivity_check_stop_script.yml'
    - '../common/l3_agent_failover_check_post_script.yml'
  tags: ffu_overcloud_upgrade_controller

- include: ../common/l3_agent_connectivity_check_start_script.yml
  tags: ffu_overcloud_upgrade_compute

- name: run FFU compute ugrade scripts
  command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
  tags: ffu_overcloud_upgrade_compute
  with_items:
    - "{{ oc_roles|intersect(['Compute','ComputeAlt']) }}"
  when:
    - not ffu_computes_rolling|bool

- include: ../common/l3_agent_connectivity_check_stop_script.yml
  tags: ffu_overcloud_upgrade_compute

- block:
    - name: register compute nodes from tripleo ansible inventory
      shell: |
        awk -F':' '/^[^ \n]*compute-/ {print $1}' {{ working_dir }}/tripleo-ansible-inventory-static.yaml
      register: tripleo_compute_nodes
      tags: ffu_overcloud_upgrade_compute

    - name: Create compute nodes FFU ugrade playbooks scripts
      template:
        src: fast-forward-upgrade/overcloud_upgrade_nodes.sh.j2
        dest: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
        mode: 0775
      tags: ffu_overcloud_upgrade_compute
      with_items:
         - '{{ tripleo_compute_nodes.stdout_lines }}'

    - include: ../common/l3_agent_connectivity_check_start_script.yml
      tags: ffu_overcloud_upgrade_compute

    - name: Rolling upgrade compute nodes
      command: "{{ working_dir }}/overcloud_upgrade_{{ item }}.sh"
      tags: ffu_overcloud_upgrade_compute
      with_items:
         - '{{ tripleo_compute_nodes.stdout_lines }}'

    - include: ../common/l3_agent_connectivity_check_stop_script.yml
      tags: ffu_overcloud_upgrade_compute
  when: ffu_computes_rolling|bool
