---
- name: Restart neutron-server container
  vars:
    service_name: "neutron-server"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-openvswitch-agent container
  vars:
    service_name: "neutron-openvswitch-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    environment: "{{ service.environment }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart fake neutron-openvswitch-agent container
  vars:
    service_name: "neutron-openvswitch-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"
  with_sequence: "start=1 end={{ num_nova_fake_per_node }}"

- name: Restart neutron-dhcp-agent container
  vars:
    service_name: "neutron-dhcp-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"
    pid_mode: "{{ service.pid_mode | default(omit) }}"
    cgroupns_mode: "{{ service.cgroupns_mode | default(omit) }}"
    environment: "{{ service.environment | default(omit) }}"

- name: Get container facts
  become: true
  kolla_container_facts:
    action: get_containers
    container_engine: "{{ kolla_container_engine }}"
    name:
      - neutron_l3_agent
  register: container_facts
  listen: Restart neutron-l3-agent container

- name: Group hosts
  group_by:
    key: neutron_l3_agent_running_{{ container_facts.containers['neutron_l3_agent'] is defined }}
  listen: Restart neutron-l3-agent container

- name: Start stopped neutron-l3-agent container
  vars:
    service_name: "neutron-l3-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    environment: "{{ service.environment }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"
    pid_mode: "{{ service.pid_mode | default(omit) }}"
    cgroupns_mode: "{{ service.cgroupns_mode | default(omit) }}"
  when:
    - groups['neutron_l3_agent_running_False'] is defined
    - inventory_hostname in groups['neutron_l3_agent_running_False']
  listen: Restart neutron-l3-agent container

- name: Wait if container starting
  wait_for:
    timeout: "{{ neutron_l3_agent_failover_delay }}"
  when:
    - groups['neutron_l3_agent_running_False'] is defined
    - groups['neutron_l3_agent_running_True'] is defined
  listen: Restart neutron-l3-agent container

- name: Restart running neutron-l3-agent container
  vars:
    service_name: "neutron-l3-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    environment: "{{ service.environment }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"
    pid_mode: "{{ service.pid_mode | default(omit) }}"
    cgroupns_mode: "{{ service.cgroupns_mode | default(omit) }}"
  when:
    - inventory_hostname == item
  loop: "{{ groups['neutron_l3_agent_running_True'] | default([]) }}"
  loop_control:
    pause: "{{ neutron_l3_agent_failover_delay }}"
  listen: Restart neutron-l3-agent container

- name: Restart neutron-sriov-agent container
  vars:
    service_name: "neutron-sriov-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-mlnx-agent container
  vars:
    service_name: "neutron-mlnx-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"

- name: Restart neutron-eswitchd container
  vars:
    service_name: "neutron-eswitchd"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"

- name: Restart neutron-tls-proxy container
  vars:
    service_name: "neutron-tls-proxy"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-metadata-agent container
  vars:
    service_name: "neutron-metadata-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-ovn-metadata-agent container
  vars:
    service_name: "neutron-ovn-metadata-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-bgp-dragent container
  vars:
    service_name: "neutron-bgp-dragent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-infoblox-ipam-agent container
  vars:
    service_name: "neutron-infoblox-ipam-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-metering-agent container
  vars:
    service_name: "neutron-metering-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes | reject('equalto', '') | list }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart ironic-neutron-agent container
  vars:
    service_name: "ironic-neutron-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"

- name: Restart neutron-ovn-agent container
  vars:
    service_name: "neutron-ovn-agent"
    service: "{{ neutron_services[service_name] }}"
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    volumes: "{{ service.volumes }}"
    dimensions: "{{ service.dimensions }}"
    privileged: "{{ service.privileged | default(False) }}"
    healthcheck: "{{ service.healthcheck | default(omit) }}"
