From 2cb913e38bc76e83d38293bd250a268efca976ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Jeanneret?= Date: Thu, 23 May 2024 16:07:42 +0200 Subject: [PATCH] Ensure we're targeting the right VMs If a user has multiple deployments, they therefore have mutliple blocks in their `~/.ssh/config`. While the ssh configuration allows to chose the right VM using the hypervisor name (for instance controller-0.builder2), the dynamic inventory built using `ansible.builtin.add_host` doesn't target the specific host. With `delegate_to`, it may happen ansible connects to a wrong VM from a previous deployment that's still running, and the user will face some really weird situations - for instance, facts being wrongs (this hit the networking_mapper and the fetched MAC addresses). With this patch, we now have specific host targeting, and we also get it in the logs, for example: ``` changed: [builder2 -> controller-0(controller-0.builder2)] ``` --- roles/libvirt_manager/tasks/manage_vms.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/roles/libvirt_manager/tasks/manage_vms.yml b/roles/libvirt_manager/tasks/manage_vms.yml index 5deb453242..65774e7485 100644 --- a/roles/libvirt_manager/tasks/manage_vms.yml +++ b/roles/libvirt_manager/tasks/manage_vms.yml @@ -110,7 +110,7 @@ ansible.builtin.add_host: name: "{{ vm_ip.nic.host }}" groups: "{{ (vm_type == 'crc') | ternary('ocp', vm_type) }}s" - ansible_host: "{{ extracted_ip }}" + ansible_host: "{{ vm_ip.nic.host }}.{{ inventory_hostname }}" ansible_ssh_user: "{{ _cifmw_libvirt_manager_layout.vms[vm_type].admin_user | default(_init_admin_user) }}" loop: "{{ vm_ips.results }}" loop_control: @@ -173,7 +173,7 @@ cmd: |- test -d /home/zuul && exit 0; set -xe -o pipefail; - echo "{{ vm_ip.nic.host }}" | sudo tee /etc/hostname; + echo "{{ vm_ip.nic.host }}.{{ inventory_hostname }}" | sudo tee /etc/hostname; sudo hostname -F /etc/hostname; sudo useradd -m -d /home/zuul zuul; echo "zuul ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/zuul; @@ -226,7 +226,7 @@ ansible.builtin.add_host: name: "{{ vm_ip.nic.host }}" groups: "{{ (vm_type == 'crc') | ternary('ocp', vm_type) }}s" - ansible_host: "{{ extracted_ip }}" + ansible_host: "{{ vm_ip.nic.host }}.{{ inventory_hostname }}" ansible_ssh_user: "{{ _cifmw_libvirt_manager_layout.vms[vm_type].admin_user | default('zuul') }}" loop: "{{ vm_ips.results }}" loop_control: