From d3aa624d082fc0d1c6aff0df513efc13a377afe3 Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Nava Date: Fri, 20 Dec 2024 11:09:49 +0100 Subject: [PATCH] Add the hypervisors to the inventory For use cases like ShiftOnStack the deployment may need to tweak the hypervisor. We did not have a way to tell the deployment how to reach the hypervisor so this commit exposes the hypervisor Ansible instance to each host and creates a hypervisors group in the generated inventory. --- roles/libvirt_manager/templates/all-inventory.yml.j2 | 12 ++++++++++++ roles/libvirt_manager/templates/inventory.yml.j2 | 1 + roles/reproducer/tasks/configure_controller.yml | 1 + 3 files changed, 14 insertions(+) diff --git a/roles/libvirt_manager/templates/all-inventory.yml.j2 b/roles/libvirt_manager/templates/all-inventory.yml.j2 index 9b9d8499a7..0520cf9fd3 100644 --- a/roles/libvirt_manager/templates/all-inventory.yml.j2 +++ b/roles/libvirt_manager/templates/all-inventory.yml.j2 @@ -20,6 +20,18 @@ virtual machines. ansible_ssh_common_args: "-o StrictHostKeyChecking=no -J {{ ansible_user | default(ansible_user_id) }}@{{ _hostname }}" {% endif %} {% endfor %} +hypervisors: + hosts: +{% set _hypervisors = ( + ( _cifmw_libvirt_manager_layout.vms.values() | selectattr('target', 'defined') | map(attribute='target') | list) + + [inventory_hostname] ) | unique %} +{% for _hypervisor in _hypervisors %} + {{ _hypervisor }}: +{% set _hostname = hostvars[_hypervisor]['ansible_host'] | default(_hypervisor[_target]['inventory_hostname']) %} + ansible_host: "{{ _hostname }}" + ansible_ssh_private_key_file: "~/.ssh/ssh_{{ _hostname }}" + vars: +{% endfor %} localhosts: hosts: localhost: diff --git a/roles/libvirt_manager/templates/inventory.yml.j2 b/roles/libvirt_manager/templates/inventory.yml.j2 index 861155f5b7..17509fe351 100644 --- a/roles/libvirt_manager/templates/inventory.yml.j2 +++ b/roles/libvirt_manager/templates/inventory.yml.j2 @@ -14,4 +14,5 @@ {% else %} ansible_ssh_private_key_file: ~/.ssh/id_cifw {% endif %} + cifmw_hypervisor_host: "{{ _cifmw_libvirt_manager_layout.vms[host.key].target | default(inventory_hostname) }}" {% endfor %} diff --git a/roles/reproducer/tasks/configure_controller.yml b/roles/reproducer/tasks/configure_controller.yml index cc86bc3db6..71ad3e3958 100644 --- a/roles/reproducer/tasks/configure_controller.yml +++ b/roles/reproducer/tasks/configure_controller.yml @@ -179,6 +179,7 @@ marker: "## {mark} {{ _ssh_host }}" block: |- Host {{ _ssh_host }} {{ hostvars[host]['inventory_hostname'] }} + Hostname {{ _ssh_host }} IdentityFile ~/.ssh/ssh_{{ _ssh_host }} StrictHostKeyChecking no UserKnownHostsFile /dev/null