---
- name: Check whether Ironic is enabled
  hosts: controllers
  gather_facts: False
  tags:
    - config
    - config-validation
    - kolla-ansible
    - kolla-openstack
  tasks:
    - name: Create controllers group with ironic enabled
      group_by:
        key: "controllers_with_ironic_enabled_{{ kolla_enable_ironic | bool }}"
      changed_when: false

- name: Ensure locally built Ironic Python Agent images are copied
  hosts: controllers_with_ironic_enabled_True[0]
  gather_facts: False
  tags:
    - kolla-ansible
    - kolla-openstack
  vars:
    # These are the filenames generated by overcloud-ipa-build.yml.
    ipa_image_name: "ipa"
    ipa_images:
      - "{{ ipa_image_name }}.kernel"
      - "{{ ipa_image_name }}.initramfs"
  tasks:
    - block:
        - block:
            - name: Check for the presence of locally built Ironic Python Agent (IPA) images
              stat:
                path: "{{ image_cache_path }}/{{ ipa_image_name }}/{{ item }}"
                get_checksum: False
                mime: False
              with_items: "{{ ipa_images }}"
              register: ipa_image_stat

            - name: Validate the presence of locally built Ironic Python Agent (IPA) images
              fail:
                msg: >
                  Expected locally built Ironic Python Agent (IPA) image
                  {{ item.item }} was not present in
                  {{ image_cache_path }}/{{ ipa_image_name }}.
              with_items: "{{ ipa_image_stat.results }}"
              when: not item.stat.exists
          tags:
            - config-validation
      when:
        - kolla_enable_ironic | bool
        - ipa_build_images | bool
      tags:
        - config

- name: Ensure Kolla OpenStack components are configured
  hosts: localhost
  tags:
    - kolla-ansible
    - kolla-openstack
  vars:
    switch_type_to_device_type:
      arista: netmiko_arista_eos
      dellos9: netmiko_dell_force10
      dellos10: netmiko_dell_os10
      dell-powerconnect: netmiko_dell_powerconnect
      junos: netmiko_juniper
      openvswitch: netmiko_ovs_linux
      nclu: netmiko_cumulus
      nvue: netmiko_cumulus_nvue
    ipa_image_name: "ipa"
  pre_tasks:
    - block:
        - name: Validate switch configuration for Neutron ML2 genericswitch driver
          fail:
            msg: >
              Switch configuration for {{ item }} is invalid. The following
              variables must be set for the host: switch_type, ansible_host,
              ansible_user, ansible_ssh_pass. Further, switch_type must be one of
              {{ switch_type_to_device_type.keys() | join(', ') }}.
          with_items: "{{ kolla_neutron_ml2_generic_switch_hosts }}"
          when: >
            item not in hostvars or
            'switch_type' not in hostvars[item] or
            hostvars[item].switch_type not in switch_type_to_device_type or
            'ansible_host' not in hostvars[item] or
            'ansible_user' not in hostvars[item] or
            'ansible_ssh_pass' not in hostvars[item]
          tags:
            - config-validation

        - name: Update a fact containing switches for use by Neutron ML2 genericswitch driver
          set_fact:
            kolla_neutron_ml2_generic_switches: >
              {{
                  kolla_neutron_ml2_generic_switches +
                  [{
                    'name': item,
                    'device_type': switch_type_to_device_type[hostvars[item].switch_type],
                    'ip': hostvars[item].ansible_host,
                    'username': hostvars[item].ansible_user,
                    'password': hostvars[item].ansible_ssh_pass,
                    'ngs_trunk_ports': (
                         hostvars[item].switch_interface_config |
                         switch_interface_config_select_description(kolla_neutron_ml2_generic_switch_trunk_port_hosts) |
                         switch_interface_config_select_trunk()).keys() | join(',')
                  } | combine(hostvars[item].kolla_neutron_ml2_generic_switch_extra) ]
              }}
          with_items: "{{ kolla_neutron_ml2_generic_switch_hosts }}"

        - block:
            - name: Set facts containing IPA kernel and ramdisk URLs
              set_fact:
                kolla_inspector_ipa_kernel_upstream_url: "{{ inspector_ipa_kernel_upstream_url }}"
                kolla_inspector_ipa_kernel_checksum_url: "{{ inspector_ipa_kernel_checksum_url }}"
                kolla_inspector_ipa_kernel_checksum_algorithm: "{{ inspector_ipa_kernel_checksum_algorithm }}"
                kolla_inspector_ipa_ramdisk_upstream_url: "{{ inspector_ipa_ramdisk_upstream_url }}"
                kolla_inspector_ipa_ramdisk_checksum_url: "{{ inspector_ipa_ramdisk_checksum_url }}"
                kolla_inspector_ipa_ramdisk_checksum_algorithm: "{{ inspector_ipa_ramdisk_checksum_algorithm }}"
              when: not ipa_build_images | bool

            - name: Set facts containing IPA kernel and ramdisk paths
              set_fact:
                kolla_inspector_ipa_kernel_path: "{{ hostvars[inspector_ipa_host].image_cache_path }}/{{ ipa_image_name }}/{{ ipa_images_kernel_name }}"
                kolla_inspector_ipa_ramdisk_path: "{{ hostvars[inspector_ipa_host].image_cache_path }}/{{ ipa_image_name }}/{{ ipa_images_ramdisk_name }}"
              when: ipa_build_images | bool
              vars:
                inspector_ipa_host: "{{ groups['controllers_with_ironic_enabled_True'][0] }}"
          when: kolla_enable_ironic | bool
      tags:
        - config
  roles:
    - role: kolla-openstack
      # Ironic inspector configuration.
      kolla_inspector_processing_hooks: "{{ inspector_processing_hooks }}"
      kolla_inspector_add_ports: "{{ inspector_add_ports }}"
      kolla_inspector_keep_ports: "{{ inspector_keep_ports }}"
      kolla_inspector_enable_discovery: "{{ inspector_enable_discovery }}"
      kolla_inspector_discovery_enroll_node_driver: "{{ inspector_discovery_enroll_node_driver }}"
      # Ironic inspector swift store configuration. Currently only supports the
      # 'fake' inspection store.
      kolla_inspector_enable_swift: "{{ inspector_store_enabled }}"
      kolla_inspector_swift_auth:
        auth_type: none
        endpoint_override: "http://{% raw %}{{ api_interface_address }}{% endraw %}:{{ inspector_store_port }}"
      kolla_inspector_ipa_host: "{{ groups['controllers_with_ironic_enabled_True'][0] }}"
      kolla_openstack_custom_config_paths_extra_multi_env_static:
        - "{{ kayobe_config_path }}"
      kolla_openstack_custom_config_paths_extra_multi_env: "{{ kolla_openstack_custom_config_paths_extra_multi_env_static + kayobe_env_search_paths }}"
      kolla_openstack_custom_config_paths_extra_legacy:
        - "{{ kayobe_env_config_path }}"
      kolla_openstack_custom_config_paths_extra: "{{ kolla_openstack_custom_config_paths_extra_multi_env if kolla_openstack_custom_config_environment_merging_enabled | bool else kolla_openstack_custom_config_paths_extra_legacy }}"
      kolla_libvirt_tls: "{{ compute_libvirt_enable_tls | bool }}"
      kolla_nova_libvirt_certificates_src: "{{ kayobe_env_config_path }}/certificates/libvirt"
