---
# This playbook will ensure that all baremetal compute nodes are named after
# their inventory host names. It matches the ipmi address in the inventory to
# the one gathered from inspection

- name: Rename baremetal compute nodes
  hosts: controllers[0]
  gather_facts: False
  vars:
    venv: "{{ virtualenv_path }}/openstack-cli"
  pre_tasks:
    - name: Set up openstack cli virtualenv
      pip:
        virtualenv: "{{ venv }}"
        name:
          - python-openstackclient
          - python-ironicclient
        state: latest
        extra_args: "{% if pip_upper_constraints_file %}-c {{ pip_upper_constraints_file }}{% endif %}"

- name: Rename baremetal compute nodes
  hosts: baremetal-compute
  gather_facts: False
  max_fail_percentage: >-
    {{ baremetal_compute_rename_max_fail_percentage |
       default(baremetal_compute_max_fail_percentage) |
       default(kayobe_max_fail_percentage) |
       default(100) }}
  vars:
    venv: "{{ virtualenv_path }}/openstack-cli"
    controller_host: "{{ groups['controllers'][0] }}"
  tasks:
    - name: Fail if ipmi host variable not set
      vars:
        ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}"
      fail:
        msg: >
          The host variable, ipmi_address is not defined for {{ inventory_hostname }}. This variable is required
          to run this playbook.
      when: ipmi_address is not defined or not ipmi_address
    - name: Get list of nodes
      command: >
        {{ venv }}/bin/openstack baremetal node list -f json --fields uuid name driver_info
      register: nodes
      delegate_to: "{{ controller_host }}"
      environment: "{{ openstack_auth_env }}"
      run_once: true
      changed_when: false
      vars:
        # NOTE: Without this, the controller's ansible_host variable will not
        # be respected when using delegate_to.
        ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"

    - name: Rename baremetal compute nodes
      command: >
        {{ venv }}/bin/openstack baremetal node set --name "{{ inventory_hostname }}" "{{ node['UUID'] }}"
      delegate_to: "{{ controller_host }}"
      environment: "{{ openstack_auth_env }}"
      vars:
        # NOTE: Without this, the controller's ansible_host variable will not
        # be respected when using delegate_to.
        ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
        ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}"
        matching_nodes: "{{ (nodes.stdout | from_json) | selectattr('Driver Info.ipmi_address', 'defined') | selectattr('Driver Info.ipmi_address', 'equalto', ipmi_address) | list }}"
        node: "{{ matching_nodes | first }}"
      when:
        - matching_nodes | length > 0
        - node['Name'] != inventory_hostname
