From 1559bb1dadde5cf78b0fefd6b0506b17bdb98117 Mon Sep 17 00:00:00 2001 From: Marcin Sobczyk Date: Mon, 11 Jul 2022 17:09:22 +0200 Subject: [PATCH] HE: Filter IP addresses based on 'ipv6_deployment' At the beginning of HE deployment we set the 'ipv6_deployment' fact based on host's address. This var should be the source of truth for whether to use an IPv4 or IPv6 address for the HE VM. Until now we were simply picking up the first address that was reported by 'virsh' and using this throughout the rest of the process. This PR adds filtering of the reported addresses based on the value of 'ipv6_deployment'. Additionally we're picking the first address to make other usages of 'local_vm_ip' nicer. Signed-off-by: Marcin Sobczyk --- .../bootstrap_local_vm/02_create_local_vm.yml | 20 +++++++++++++------ .../03_engine_initial_tasks.yml | 4 ++-- .../01_create_target_hosted_engine_vm.yml | 12 +++++++++-- .../tasks/restore_backup.yml | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/roles/hosted_engine_setup/tasks/bootstrap_local_vm/02_create_local_vm.yml b/roles/hosted_engine_setup/tasks/bootstrap_local_vm/02_create_local_vm.yml index 9b8aec3ab..5689d663c 100644 --- a/roles/hosted_engine_setup/tasks/bootstrap_local_vm/02_create_local_vm.yml +++ b/roles/hosted_engine_setup/tasks/bootstrap_local_vm/02_create_local_vm.yml @@ -117,14 +117,22 @@ - name: Get local VM IP ansible.builtin.shell: virsh -r net-dhcp-leases default | grep -i {{ he_vm_mac_addr }} | awk '{ print $5 }' | cut -f1 -d'/' environment: "{{ he_cmd_lang }}" - register: local_vm_ip - until: local_vm_ip.stdout_lines|length >= 1 + register: local_vm_ip_candidates + until: local_vm_ip_candidates.stdout_lines|length >= 1 retries: 90 delay: 10 changed_when: true - - name: Debug var local_vm_ip + - name: Debug var local_vm_ip_candidates ansible.builtin.debug: - var: local_vm_ip + var: local_vm_ip_candidates + - name: Select first IPv6 address + ansible.builtin.set_fact: + local_vm_ip: "{{ local_vm_ip_candidates.stdout_lines | ansible.netcommon.ipv6 | first }}" + when: ipv6_deployment + - name: Select first IPv4 address + ansible.builtin.set_fact: + local_vm_ip: "{{ local_vm_ip_candidates.stdout_lines | ansible.netcommon.ipv4 | first }}" + when: not ipv6_deployment - name: Remove leftover entries in /etc/hosts for the local VM ansible.builtin.lineinfile: dest: /etc/hosts @@ -134,7 +142,7 @@ ansible.builtin.lineinfile: dest: /etc/hosts line: - "{{ local_vm_ip.stdout_lines[0] }} \ + "{{ local_vm_ip }} \ {{ he_fqdn }} # temporary entry added by hosted-engine-setup for the bootstrap VM" insertbefore: BOF backup: true @@ -146,7 +154,7 @@ timeout=300 - name: Set the name for add_host ansible.builtin.set_fact: - he_fqdn_ansible_host: "{{ local_vm_ip.stdout_lines[0] }}" + he_fqdn_ansible_host: "{{ local_vm_ip }}" - import_tasks: ../add_engine_as_ansible_host.yml rescue: - include_tasks: ../clean_localvm_dir.yml diff --git a/roles/hosted_engine_setup/tasks/bootstrap_local_vm/03_engine_initial_tasks.yml b/roles/hosted_engine_setup/tasks/bootstrap_local_vm/03_engine_initial_tasks.yml index 775acb1d4..17f178510 100644 --- a/roles/hosted_engine_setup/tasks/bootstrap_local_vm/03_engine_initial_tasks.yml +++ b/roles/hosted_engine_setup/tasks/bootstrap_local_vm/03_engine_initial_tasks.yml @@ -18,7 +18,7 @@ ansible.builtin.lineinfile: path: /etc/hosts line: - "{{ hostvars[he_ansible_host_name]['local_vm_ip']['stdout_lines'][0] }} {{ he_fqdn }} # hosted-engine-setup-{{ \ + "{{ hostvars[he_ansible_host_name]['local_vm_ip'] }} {{ he_fqdn }} # hosted-engine-setup-{{ \ hostvars[he_ansible_host_name]['he_local_vm_dir'] }}" - name: Reconfigure IPv6 default gateway ansible.builtin.command: ip -6 route add default via "{{ he_ipv6_subnet_prefix + '::1' }}" @@ -99,7 +99,7 @@ ansible.builtin.debug: msg: >- You can now connect from this host to the bootstrap engine VM using ssh as root - and the temporary IP address - {{ hostvars[he_ansible_host_name]['local_vm_ip']['stdout_lines'][0] }} + and the temporary IP address - {{ hostvars[he_ansible_host_name]['local_vm_ip'] }} - include_tasks: ../pause_execution.yml when: he_pause_before_engine_setup|bool - name: Restore a backup diff --git a/roles/hosted_engine_setup/tasks/create_target_vm/01_create_target_hosted_engine_vm.yml b/roles/hosted_engine_setup/tasks/create_target_vm/01_create_target_hosted_engine_vm.yml index 16bcd521a..6f704dd2a 100644 --- a/roles/hosted_engine_setup/tasks/create_target_vm/01_create_target_hosted_engine_vm.yml +++ b/roles/hosted_engine_setup/tasks/create_target_vm/01_create_target_hosted_engine_vm.yml @@ -5,11 +5,19 @@ - name: Get local VM IP ansible.builtin.shell: virsh -r net-dhcp-leases default | grep -i {{ he_vm_mac_addr }} | awk '{ print $5 }' | cut -f1 -d'/' environment: "{{ he_cmd_lang }}" - register: local_vm_ip + register: local_vm_ip_candidates changed_when: true + - name: Select first IPv6 address + ansible.builtin.set_fact: + local_vm_ip: "{{ local_vm_ip_candidates.stdout_lines | ansible.netcommon.ipv6 | first }}" + when: ipv6_deployment + - name: Select first IPv4 address + ansible.builtin.set_fact: + local_vm_ip: "{{ local_vm_ip_candidates.stdout_lines | ansible.netcommon.ipv4 | first }}" + when: not ipv6_deployment - name: Set the name for add_host ansible.builtin.set_fact: - he_fqdn_ansible_host: "{{ local_vm_ip.stdout_lines[0] }}" + he_fqdn_ansible_host: "{{ local_vm_ip }}" - import_tasks: ../add_engine_as_ansible_host.yml - name: Fetch host facts ovirt_host_info: diff --git a/roles/hosted_engine_setup/tasks/restore_backup.yml b/roles/hosted_engine_setup/tasks/restore_backup.yml index 97cf737e5..96f757003 100644 --- a/roles/hosted_engine_setup/tasks/restore_backup.yml +++ b/roles/hosted_engine_setup/tasks/restore_backup.yml @@ -26,7 +26,7 @@ You can now connect from this host to the bootstrap engine VM using ssh as root and the temporary IP address - - {{ hostvars[he_ansible_host_name]['local_vm_ip']['stdout_lines'][0] }} - + {{ hostvars[he_ansible_host_name]['local_vm_ip'] }} - and fix this issue. Please continue only after the backup is restored. To retry the command that failed, you can run, on the bootstrap engine VM: