---
- name: Create ports for Octavia health-manager nodes
  become: true
  kolla_toolbox:
    container_engine: "{{ kolla_container_engine }}"
    module_name: os_port
    module_args:
      auth: "{{ octavia_user_auth }}"
      cacert: "{{ openstack_cacert }}"
      endpoint_type: "{{ openstack_interface }}"
      region_name: "{{ openstack_region_name }}"
      state: present
      network: "{{ octavia_amp_network['name'] }}"
      security_groups: "{{ octavia_amp_security_groups['health-mgr-sec-grp']['name'] }}"
      device_owner: 'Octavia:health-mgr'
      name: "octavia-listen-port-{{ ansible_facts.nodename }}"
  register: port_info

# ansible os_port module does not support 'host' parameter, but we need set the port's host
# value to {{ ansible_facts.nodename }}, once os_port support this parameter, remove the task below
# https://docs.ansible.com/ansible/latest/modules/os_port_module.html#parameters
- name: Update Octavia health manager port host_id
  become: True
  vars:
    port_id: "{{ port_info.port.id }}"
  command: >
    {{ kolla_container_engine }} exec kolla_toolbox openstack
    --os-interface {{ openstack_interface }}
    --os-auth-url {{ octavia_user_auth.auth_url }}
    --os-identity-api-version 3
    --os-project-domain-name {{ octavia_user_auth.domain_name }}
    --os-project-name {{ octavia_user_auth.project_name }}
    --os-region-name {{ openstack_region_name }}
    --os-username {{ octavia_user_auth.username }}
    --os-password {{ octavia_user_auth.password }}
    {% if openstack_cacert != '' %}--os-cacert {{ openstack_cacert }} {% endif %}
    port set --host {{ ansible_facts.nodename }} {{ port_id }}
  when:
    - port_info.changed

- name: Add Octavia port to openvswitch br-int
  vars:
    port_mac: "{{ port_info.port.mac_address }}"
    port_id: "{{ port_info.port.id }}"
  become: True
  command: >
    {{ kolla_container_engine }} exec openvswitch_vswitchd ovs-vsctl --may-exist \
    add-port br-int {{ octavia_network_interface }} \
    -- set Interface {{ octavia_network_interface }} type=internal \
    -- set Interface {{ octavia_network_interface }} external-ids:iface-status=active \
    -- set Interface {{ octavia_network_interface }} external-ids:attached-mac={{ port_mac }} \
    -- set Interface {{ octavia_network_interface }} external-ids:iface-id={{ port_id }} \
    -- set Interface {{ octavia_network_interface }} external-ids:skip_cleanup=true

- name: Create octavia dhclient conf
  become: true
  copy:
    content: |
      request subnet-mask,broadcast-address,interface-mtu;
      do-forward-updates false;
    dest: /etc/dhcp/octavia-dhclient.conf
    mode: 0664

- name: Create octavia-interface service
  become: True
  template:
    src: octavia-interface.service.j2
    dest: /etc/systemd/system/octavia-interface.service
  register: octavia_interface

- name: Restart octavia-interface.service if required
  become: True
  systemd:
    name: octavia-interface
    daemon_reload: yes
    state: restarted
  when: octavia_interface.changed

- name: Enable and start octavia-interface.service
  become: True
  service:
    name: octavia-interface
    enabled: yes
    state: started

- name: Wait for interface {{ octavia_network_interface }} ip appear
  vars:
    port_ip: "{{ port_info.port.fixed_ips[0].ip_address }}"
  command: ip address show dev {{ octavia_network_interface }}
  changed_when: false
  register: ip_info
  until: ip_info.stdout.find(port_ip) != -1
  retries: 5
  delay: 2

# NOTE(wuchunyang): we have gathered facts at first, but here we add a new
# network device(o-hm0) which is not collected, so we need gather facts again
- name: Gather facts
  setup:
  when: octavia_interface.changed
