/etc/ansible/roles/ceph-ansible/roles/ceph-facts/tasks/set_monitor_address.yml
---
- name: set_fact _monitor_address to monitor_address_block ipv4
  set_fact:
    _monitor_addresses: "{{ _monitor_addresses | default([]) + [{ 'name': item, 'addr': hostvars[item]['ansible_all_ipv4_addresses'] | ips_in_ranges(hostvars[item]['monitor_address_block'].split(',')) | first }] }}"
  with_items: "{{ groups.get(mon_group_name, []) }}"
  when:
    - "item not in _monitor_addresses | default([]) | selectattr('name', 'defined') |  map(attribute='name') | list"
    - hostvars[item]['monitor_address_block'] is defined
    - hostvars[item]['monitor_address_block'] != 'subnet'
    - ip_version == 'ipv4'

- name: set_fact _monitor_address to monitor_address_block ipv6
  set_fact:
    _monitor_addresses: "{{ _monitor_addresses | default([]) + [{ 'name': item, 'addr': hostvars[item]['ansible_all_ipv6_addresses'] | ips_in_ranges(hostvars[item]['monitor_address_block'].split(',')) | last | ipwrap }] }}"
  with_items: "{{ groups.get(mon_group_name, []) }}"
  when:
    - "item not in _monitor_addresses | default([]) | selectattr('name', 'defined') |  map(attribute='name') | list"
    - hostvars[item]['monitor_address_block'] is defined
    - hostvars[item]['monitor_address_block'] != 'subnet'
    - ip_version == 'ipv6'

- name: set_fact _monitor_address to monitor_address
  set_fact:
    _monitor_addresses: "{{ _monitor_addresses | default([]) + [{ 'name': item, 'addr': hostvars[item]['monitor_address'] | ipwrap}] }}"
  with_items: "{{ groups.get(mon_group_name, []) }}"
  when:
    - "item not in _monitor_addresses | default([]) | selectattr('name', 'defined') | map(attribute='name') | list"
    - hostvars[item]['monitor_address'] is defined
    - hostvars[item]['monitor_address'] != 'x.x.x.x'

- name: set_fact _monitor_address to monitor_interface - ipv4
  set_fact:
    _monitor_addresses: "{{ _monitor_addresses | default([]) + [{ 'name': item, 'addr': hostvars[item]['ansible_' + (hostvars[item]['monitor_interface']|replace('-', '_'))][ip_version]['address'] | ipwrap }]  }}"
  with_items: "{{ groups.get(mon_group_name, []) }}"
  when:
    - "item not in _monitor_addresses | default([]) | selectattr('name', 'defined') | map(attribute='name') | list"
    - ip_version == 'ipv4'
    - hostvars[item]['monitor_address_block'] | default('subnet')  == 'subnet'
    - hostvars[item]['monitor_address'] | default('x.x.x.x') == 'x.x.x.x'
    - hostvars[item]['monitor_interface'] | default('interface') != 'interface'

- name: set_fact _monitor_address to monitor_interface - ipv6
  set_fact:
    _monitor_addresses: "{{ _monitor_addresses | default([]) + [{ 'name': item, 'addr': hostvars[item]['ansible_' + (hostvars[item]['monitor_interface']|replace('-', '_'))][ip_version][0]['address'] | ipwrap }] }}"
  with_items: "{{ groups.get(mon_group_name, []) }}"
  when:
    - "item not in _monitor_addresses | default([]) | selectattr('name', 'defined') | map(attribute='name') | list"
    - ip_version == 'ipv6'
    - hostvars[item]['monitor_address_block'] | default('subnet')  == 'subnet'
    - hostvars[item]['monitor_address'] | default('x.x.x.x') == 'x.x.x.x'
    - hostvars[item]['monitor_interface'] | default('interface') != 'interface'

- name: set_fact _current_monitor_address
  set_fact:
    _current_monitor_address: "{{ item.addr }}"
  with_items: "{{ _monitor_addresses }}"
  when: inventory_hostname == item.name