---
- set_fact:
    etcd_vms_count: "{{ openshift_ovirt_vm_manifest | selectattr('name', 'match', 'etcd') | map(attribute='count') | list | first | d(0) }}"

- name: Add master VMs to the master, nodes, and optionally etcd inventory groups
  include_tasks: add_host.yaml
  loop: "{{ openshift_ovirt_vm_manifest | flatten(levels=1) | selectattr('name', 'match', 'master') | list }}"
  vars:
    openshift_node_group_name: node-config-master
    openshift_schedulable: True
    hostgroups: "{{ ['masters', 'nodes'] + (etcd_vms_count | int == 0) | ternary(['etcd'], []) }}"
  loop_control:
    loop_var: outer_item

- name: Add compute VMs to the nodes inventory group
  include_tasks: add_host.yaml
  loop: "{{ openshift_ovirt_vm_manifest | flatten(levels=1) | selectattr('name', 'match', 'compute') | list }}"
  vars:
    openshift_node_group_name: node-config-compute
    openshift_schedulable: True
    hostgroups: nodes
  loop_control:
    loop_var: outer_item

- name: Add infra VMs to the nodes inventory group
  include_tasks: add_host.yaml
  loop: "{{ openshift_ovirt_vm_manifest | flatten(levels=1) | selectattr('name', 'match', 'infra') | list }}"
  vars:
    openshift_node_group_name: node-config-infra
    openshift_schedulable: True
    hostgroups: nodes
  loop_control:
    loop_var: outer_item

- name: Add etcd VMs to the etcd inventory group
  include_tasks: add_host.yaml
  loop: "{{ openshift_ovirt_vm_manifest | flatten(levels=1) | selectattr('name', 'match', 'etcd') | list }}"
  vars:
    hostgroups: etcd
  loop_control:
    loop_var: outer_item

- include_tasks: add_host.yaml
  loop: "{{ openshift_ovirt_vm_manifest | flatten(levels=1) | selectattr('name', 'match', 'lb') | list }}"
  vars:
    hostgroups: lb
  loop_control:
    loop_var: outer_item
