Programatically generate ssh hosts lines

This change will ensure that the ssh hosts lines we create are
programatically generated. This will ensure we're not running
into errors caused by missing or bad facts or generating lines
that do not make sense caused by bad string slicing or one off
jinja errors.

Related-Bug: #1837644
Related-Bug: #1855149
Closes-Bug: #1856893
Change-Id: I3696238b26d8567bc3145a22b2b9ec944cb14d13
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2019-12-17 15:05:22 -06:00 committed by Kevin Carter (cloudnull)
parent 30c7de49b0
commit 0a87cadd27
1 changed files with 22 additions and 12 deletions

View File

@ -47,18 +47,28 @@
run_once: true
set_fact:
ssh_known_hosts_lines: |-
{%- for host in groups['overcloud'] | intersect(play_hosts) %}
[{{ ctlplane_ip }}]*,[{{ host }}.{{ cloud_domain }}]*,[{{ host }}]*
{%- if (enabled_networks and enabled_networks | length > 0 and 'role_networks' in hostvars[host]
and hostvars[host]['role_networks'] and hostvars[host]['role_networks'] | length > 0) %},
{%- for network in enabled_networks %}
{%- if network in hostvars[host]['role_networks'] %}
[{{ hostvars[host][networks[network]['name'] ~ '_ip'] }}]*,[{{ host }}.{{ network.lower() }}]*,{% if 1 %}{% endif %}
[{{ host }}.{{ network.lower() }}.{{ cloud_domain }}]*{% if not loop.last %},{% endif %}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{{ ' ssh-rsa ' ~ hostvars[host]['ansible_ssh_host_key_rsa_public'] }}
{% for host in (groups['overcloud'] | intersect(play_hosts)) %}
{% set hostdata = hostvars[host] %}
{% if ('ansible_ssh_host_key_rsa_public' in hostdata) or (ansible_ssh_host_key_rsa_public is defined) %}
{% if (cloud_domain is defined) and (ctlplane_ip is defined) %}
{% set line = '[' ~ ctlplane_ip ~ ']*,[' ~ host ~ '.' ~ cloud_domain ~ ']*,[' ~ host ~ ']*' %}
{% set role_networks = hostdata['role_networks'] | default([]) %}
{% if ((enabled_networks is defined) and ((enabled_networks | length) > 0) and ((role_networks | length) > 0)) %}
{% set line = line ~ ',' %}
{% for network in enabled_networks %}
{% if network in role_networks %}
{% set line = line ~ '[' ~ hostdata[(networks[network]["name"] ~ '_ip')] ~ ']*,[' ~ host ~ '.' ~ network.lower() ~ ']*,' %}
{% set line = line ~ '[' ~ host ~ '.' ~ network.lower() ~ '.' ~ cloud_domain ~ ']*' %}
{% if not loop.last %}
{% set line = line ~ ',' %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% set line = line ~ ' ssh-rsa ' ~ hostdata['ansible_ssh_host_key_rsa_public'] | default(ansible_ssh_host_key_rsa_public) %}
{{ line }}
{% endif %}
{% endif %}
{% endfor %}
- name: Add host keys to temporary ssh_known_hosts