Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions roles/libvirt_manager/tasks/add_vm_to_inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Add host to runtime inventory
ansible.builtin.add_host:
name: "{{ _full_host_name }}"
groups: "{{ _group }}s"
groups: "{{ _inventory_groups }}"
ansible_ssh_user: "{{ _ssh_user }}"
ansible_host: "{{ _add_ansible_host | ternary(_ansible_host, omit) }}"
vm_type: "{{ _group }}"
Expand All @@ -11,14 +11,20 @@
ansible.builtin.lineinfile:
path: "{{ cifmw_libvirt_manager_tmp_inv_file }}"
create: true
line: "[{{ _group }}s]"
regexp: "^\\[{{ _group }}s\\]$"
line: "[{{ _inv_group }}]"
regexp: "^\\[{{ _inv_group }}\\]$"
state: present
mode: "0644"
loop: "{{ _inventory_groups }}"
loop_control:
loop_var: _inv_group

- name: Append host under proper group
ansible.builtin.lineinfile:
path: "{{ cifmw_libvirt_manager_tmp_inv_file }}"
insertafter: "^\\[{{ _group }}s\\]$"
insertafter: "^\\[{{ _inv_group }}\\]$"
line: "{{ _ini_line }}"
regexp: "^{{ _full_host_name | regex_escape() }} "
loop: "{{ _inventory_groups }}"
loop_control:
loop_var: _inv_group
37 changes: 29 additions & 8 deletions roles/libvirt_manager/tasks/generate_networking_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
cifmw_baremetal_hosts[item.key] is defined) |
ternary('baremetal', _std_group)
}}
_subtype_group: "{{ _group }}s"
_parent_group: "{{ _cifmw_libvirt_manager_layout.vms[_vm_type].parent_group | default('') }}"
_inventory_groups: >-
{{
[_subtype_group] +
([_parent_group] if _parent_group | length > 0 else [])
}}
_ocp_name: >-
{{
item.key | replace('_', '-') |
Expand Down Expand Up @@ -198,7 +205,6 @@
selectattr('name', 'match', _match) | first
}}
_dataset: |
{% set ns = namespace(ip_start=30) %}
networks:
{{ _lnet_data.name | replace('cifmw_', '') }}:
{% if _lnet_data.ranges[0].start_v4 is defined and _lnet_data.ranges[0].start_v4 %}
Expand All @@ -210,28 +216,43 @@
network-v6: '{{ net_6 }}'
{% endif %}
group-templates:
{% set ns = namespace(ip_start=30, rendered=[]) %}
{% for group in _cifmw_libvirt_manager_layout.vms.keys() if group != 'controller' and
((_cifmw_libvirt_manager_layout.vms[group].amount is defined and
(_cifmw_libvirt_manager_layout.vms[group].amount | int) > 0) or
_cifmw_libvirt_manager_layout.vms[group].amount is undefined) %}
{% set _gr = (group == 'crc') | ternary('ocp', group) %}
{% if _lnet_data.name | replace('cifmw_', '') in _cifmw_libvirt_manager_layout.vms[group].nets %}
{{ _gr }}s:
{% set _gr = (group == 'crc') | ternary('ocp', group) %}
{% set _effective_group = _cifmw_libvirt_manager_layout.vms[group].parent_group | default(_gr ~ 's') %}
{% set _group_networks = _cifmw_libvirt_manager_layout.vms[group].nets | default([]) %}
{% if _effective_group not in ns.rendered and _lnet_data.name | replace('cifmw_', '') in _group_networks %}
{% set _shared_length = namespace(total=0) %}
{% for _candidate in _cifmw_libvirt_manager_layout.vms | dict2items
if _candidate.key != 'controller' and
((_candidate.value.amount is defined and (_candidate.value.amount | int) > 0) or
_candidate.value.amount is undefined) %}
{% set _candidate_group = _candidate.value.parent_group | default((((_candidate.key == 'crc') | ternary('ocp', _candidate.key)) ~ 's')) %}
{% if _candidate_group == _effective_group and
(_lnet_data.name | replace('cifmw_', '') in (_candidate.value.nets | default([]))) %}
{% set _shared_length.total = _shared_length.total + (_candidate.value.amount | default(1) | int) %}
{% endif %}
{% endfor %}
{{ _effective_group }}:
networks:
{{ _lnet_data.name | replace('cifmw_', '') }}:
{% if cifmw_networking_definition['group-templates'][_gr ~ 's']['network-template'] is undefined %}
{% if cifmw_networking_definition['group-templates'][_effective_group]['network-template'] is undefined %}
{% if net_4 is defined %}
range-v4:
start: '{{ net_4 | ansible.utils.nthhost(ns.ip_start | int ) }}'
length: {{ _cifmw_libvirt_manager_layout.vms[group].amount | default(1) }}
length: {{ _shared_length.total }}
{% endif %}
{% if net_6 is defined %}
range-v6:
start: '{{ net_6 | ansible.utils.nthhost(ns.ip_start | int) }}'
length: {{ _cifmw_libvirt_manager_layout.vms[group].amount | default(1) }}
length: {{ _shared_length.total }}
{% endif %}
{% set ns.ip_start = ns.ip_start|int + (_cifmw_libvirt_manager_layout.vms[group].amount | default(1) | int ) + 1 %}
{% set ns.ip_start = ns.ip_start|int + (_shared_length.total | int ) + 1 %}
{% endif %}
{% set _ = ns.rendered.append(_effective_group) %}
{% endif %}
{% endfor %}
{% if cifmw_baremetal_hosts is defined and cifmw_baremetal_hosts | length > 0 %}
Expand Down
21 changes: 20 additions & 1 deletion roles/libvirt_manager/templates/all-inventory.yml.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{% set ns = namespace(parent_groups=[]) %}
{% for vm_name, vm_data in _cifmw_libvirt_manager_layout.vms.items()
if vm_data.manage | default(true) and
vm_data.amount | default(1) | int > 0 %}
{% if vm_data.parent_group is defined and vm_data.parent_group not in ns.parent_groups %}
{% set _ = ns.parent_groups.append(vm_data.parent_group) %}
{% endif %}
{% endfor %}
all:
children:
{% for vm in _cifmw_libvirt_manager_layout.vms.keys() if
(_cifmw_libvirt_manager_layout.vms[vm].manage | default(true) and
_cifmw_libvirt_manager_layout.vms[vm].amount | default(1) | int > 0) %}
{{ (vm == 'crc') | ternary('ocp', vm) }}s:
{% set _group = ((vm == 'crc') | ternary('ocp', vm)) ~ 's' %}
{{ _group }}:
vars:
{% if _cifmw_libvirt_manager_layout.vms[vm].target is defined %}
{% set _target = _cifmw_libvirt_manager_layout.vms[vm].target %}
Expand All @@ -20,6 +29,16 @@ virtual machines.
ansible_ssh_common_args: "-o StrictHostKeyChecking=no -J {{ ansible_user | default(ansible_user_id) }}@{{ _hostname }}"
{% endif %}
{% endfor %}
{% for parent in ns.parent_groups %}
{{ parent }}:
children:
{% for child_name, child_data in _cifmw_libvirt_manager_layout.vms.items()
if child_data.manage | default(true) and
child_data.amount | default(1) | int > 0 and
child_data.parent_group | default('') == parent %}
{{ ((child_name == 'crc') | ternary('ocp', child_name)) ~ 's' }}: {}
{% endfor %}
{% endfor %}
hypervisors:
hosts:
{% set _hypervisors = (
Expand Down
Loading