Skip to content

Commit 3922bf4

Browse files
author
Bharat Kunwar
committed
Parameterise use of gateway to act as a bridge to nodes on a private network
1 parent b0024d2 commit 3922bf4

File tree

2 files changed

+50
-42
lines changed

2 files changed

+50
-42
lines changed

tasks/main.yml

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@
4646
when: cluster_venv != None
4747

4848
- block:
49+
- name: Extract node objects
50+
set_fact:
51+
cluster_nodes: "{{ cluster_group.output_value | sum(attribute='nodes', start=[]) }}"
52+
53+
- name: Prepare access via intermediate gateway
54+
block:
55+
56+
- name: Extract gateway host username and ip
57+
set_fact:
58+
cluster_gw_user: "{{ cluster_params.cluster_groups | selectattr('name', 'equalto', cluster_gw_group) | map(attribute='user') | join }}"
59+
cluster_gw_ip: "{{ (cluster_group.output_value | selectattr('group', 'equalto', cluster_gw_group) | first).nodes | map(attribute='ip') | first }}"
60+
61+
- name: Add gateway to dynamic inventory
62+
add_host:
63+
hostname: "{{ cluster_gw_ip }}"
64+
ansible_host: "{{ cluster_gw_ip }}"
65+
ansible_user: "{{ cluster_gw_user }}"
66+
67+
- name: Wait for direct SSH access to the designated gateway
68+
wait_for:
69+
host: "{{ cluster_gw_ip }}"
70+
port: 22
71+
state: started
72+
timeout: "{{ cluster_ssh_timeout }}"
73+
74+
- name: Scan for SSH keys
75+
command: ssh-keyscan {{ cluster_gw_ip }}
76+
register: keyscan_cluster_gw
77+
changed_when: False
78+
79+
- name: Ensure gateway is in SSH known hosts
80+
blockinfile:
81+
block: |
82+
{% for key in keyscan_cluster_gw.stdout_lines %}
83+
{{ key }}
84+
{% endfor %}
85+
create: true
86+
marker: "# P3-APPLIANCES MANAGED BLOCK FOR {{ cluster_name }}"
87+
path: "~/.ssh/known_hosts"
88+
89+
when: cluster_gw_group is defined
90+
4991
- name: Ensure cluster inventory directory exists
5092
file:
5193
path: "{{ cluster_inventory | dirname }}"
@@ -56,46 +98,6 @@
5698
src: cluster_inventory.j2
5799
dest: "{{ cluster_inventory }}"
58100

59-
- name: Extract node objects
60-
set_fact:
61-
cluster_nodes: "{{ cluster_group.output_value | sum(attribute='nodes', start=[]) }}"
62-
63-
- name: Prepare access via intermediate gateway
64-
block:
65-
- name: Extract gateway host
66-
set_fact:
67-
cluster_gw: "{{ cluster_nodes | selectattr('name', 'equalto', cluster_gw_host) | first }}"
68-
69-
- name: Add gateway to dynamic inventory
70-
add_host:
71-
hostname: "{{ cluster_gw.ip }}"
72-
ansible_host: "{{ cluster_gw.ip }}"
73-
ansible_user: "stelfer"
74-
75-
- name: Wait for direct SSH access to the designated gateway
76-
wait_for:
77-
host: "{{ cluster_gw.ip }}"
78-
port: 22
79-
state: started
80-
timeout: "{{ cluster_ssh_timeout }}"
81-
82-
- name: Scan for SSH keys
83-
command: ssh-keyscan {{ cluster_gw.ip }}
84-
register: keyscan_cluster_gw
85-
changed_when: False
86-
87-
- name: Ensure gateway is in SSH known hosts
88-
blockinfile:
89-
block: |
90-
{% for key in keyscan_cluster_gw.stdout_lines %}
91-
{{ key }}
92-
{% endfor %}
93-
create: true
94-
marker: "# P3-APPLIANCES MANAGED BLOCK FOR {{ cluster_name }}"
95-
path: "~/.ssh/known_hosts"
96-
97-
when: cluster_gw_host is defined
98-
99101
- name: Wait for SSH access to the nodes
100102
wait_for:
101103
host: "{{ item.ip }}"
@@ -104,6 +106,6 @@
104106
timeout: "{{ cluster_ssh_timeout }}"
105107
with_items:
106108
- "{{ cluster_nodes }}"
107-
delegate_to: "{{ cluster_gw.ip | default('localhost') }}"
109+
delegate_to: "{{ cluster_gw_ip | default('localhost') }}"
108110

109111
when: cluster_state != 'absent'

templates/cluster_inventory.j2

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ cluster
1616
{% for group_data in cluster_group.output_value %}
1717
[{{ cluster_name }}_{{ group_data.group }}]
1818
{% for node_data in group_data.nodes %}
19-
{{ node_data.name }} ansible_host={{ node_data.ip }} ansible_user={{ cluster_params.cluster_groups | selectattr("name", "equalto", group_data.group) | map(attribute='user') | join }}
19+
{{ node_data.name }} ansible_host={{ node_data.ip }}
2020
{% endfor %}
2121

22+
[{{ cluster_name }}_{{ group_data.group }}:vars]
23+
ansible_user={{ cluster_params.cluster_groups | selectattr("name", "equalto", group_data.group) | map(attribute='user') | join }}
24+
{% if cluster_gw_group != group_data.group %}
25+
ansible_ssh_common_args='-o ProxyJump={{ cluster_gw_user }}@{{ cluster_gw_ip }}'
26+
{% endif %}
27+
2228
{% endfor %}
2329
# Specific roles for cluster deployment assignments
2430
{% for role in cluster_roles %}

0 commit comments

Comments
 (0)