From 54a4e496b916c9a86af5429074e37bc09178482f Mon Sep 17 00:00:00 2001 From: Sven Anders Date: Tue, 10 Aug 2021 17:09:03 +0200 Subject: [PATCH] Handle host with unset ansible_host We are having all machines in DNS and want to be able to change IP addresses in DNS. So we do not use ansible_host in our host_vars/machine.yml As os_keystone is the first Ansible role we use. We will make similar changes to other roles later on. Change-Id: Ic9f43cc3f6b62b5098e85afcf55f008c022517f6 --- tasks/keystone_credential_distribute.yml | 2 +- tasks/keystone_fernet_keys_distribute.yml | 2 +- templates/keystone-credential-rotate.sh.j2 | 8 ++++++-- templates/keystone-fernet-rotate.sh.j2 | 9 +++++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tasks/keystone_credential_distribute.yml b/tasks/keystone_credential_distribute.yml index d767314b..948acaa8 100644 --- a/tasks/keystone_credential_distribute.yml +++ b/tasks/keystone_credential_distribute.yml @@ -21,7 +21,7 @@ -avz --delete {{ keystone_credential_key_repository }}/ - {{ keystone_system_user_name }}@{{ hostvars[item]['ansible_host'] }}:{{ keystone_credential_key_repository }}/ + {{ keystone_system_user_name }}@{{ hostvars[item]['ansible_host'] | default(item) }}:{{ keystone_credential_key_repository }}/ become: yes become_user: "{{ keystone_system_user_name }}" changed_when: false diff --git a/tasks/keystone_fernet_keys_distribute.yml b/tasks/keystone_fernet_keys_distribute.yml index 052d3ce6..eb284168 100644 --- a/tasks/keystone_fernet_keys_distribute.yml +++ b/tasks/keystone_fernet_keys_distribute.yml @@ -21,7 +21,7 @@ -avz --delete {{ keystone_fernet_tokens_key_repository }}/ - {{ keystone_system_user_name }}@{{ hostvars[item]['ansible_host'] }}:{{ keystone_fernet_tokens_key_repository }}/ + {{ keystone_system_user_name }}@{{ hostvars[item]['ansible_host'] | default(item) }}:{{ keystone_fernet_tokens_key_repository }}/ become: yes become_user: "{{ keystone_system_user_name }}" changed_when: false diff --git a/templates/keystone-credential-rotate.sh.j2 b/templates/keystone-credential-rotate.sh.j2 index 56f16f34..c2b4adb9 100644 --- a/templates/keystone-credential-rotate.sh.j2 +++ b/templates/keystone-credential-rotate.sh.j2 @@ -40,13 +40,17 @@ function autorotate { {% for host in groups['keystone_all'] %} {% if inventory_hostname != host %} - + {% if 'ansible_host' in hostvars[host] %} + {% set destination_host = hostvars[host]['ansible_host'] %} + {% else %} + {% set destination_host = inventory_hostname %} + {% endif %} # Fernet sync job to "{{ host }}" rsync -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ -avz \ --delete \ {{ keystone_credential_key_repository }}/ \ - {{ keystone_system_user_name }}@{{ hostvars[host]['ansible_host'] }}:{{ keystone_credential_key_repository }}/ + {{ keystone_system_user_name }}@{{ destination_host }}:{{ keystone_credential_key_repository }}/ {%- endif %} diff --git a/templates/keystone-fernet-rotate.sh.j2 b/templates/keystone-fernet-rotate.sh.j2 index 403b280d..16945e1a 100644 --- a/templates/keystone-fernet-rotate.sh.j2 +++ b/templates/keystone-fernet-rotate.sh.j2 @@ -29,18 +29,23 @@ function autorotate { {% for host in groups['keystone_all'] %} {% if inventory_hostname != host %} + {% if 'ansible_host' in hostvars[host] %} + {% set destination_host = hostvars[host]['ansible_host'] %} + {% else %} + {% set destination_host = inventory_hostname %} + {% endif %} # Fernet sync job to "{{ host }}" scp -o UserKnownHostsFile=/dev/null \ -o StrictHostKeyChecking=no \ $(ls -dtr {{ keystone_fernet_tokens_key_repository }}/* | sort -Vr) \ - {{ keystone_system_user_name }}@{{ hostvars[host]['ansible_host'] }}:{{ keystone_fernet_tokens_key_repository }}/ + {{ keystone_system_user_name }}@{{ destination_host }}:{{ keystone_fernet_tokens_key_repository }}/ rsync -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ -avz \ --delete \ {{ keystone_fernet_tokens_key_repository }}/ \ - {{ keystone_system_user_name }}@{{ hostvars[host]['ansible_host'] }}:{{ keystone_fernet_tokens_key_repository }}/ + {{ keystone_system_user_name }}@{{ destination_host }}:{{ keystone_fernet_tokens_key_repository }}/ {%- endif %}