Skip to content
Snippets Groups Projects
Commit 06584ee3 authored by Alexander Block's avatar Alexander Block
Browse files

Add support for bastion hosts

parent 33585fa6
No related branches found
No related tags found
No related merge requests found
......@@ -5,3 +5,4 @@ temp
.idea
*.tfstate
*.tfstate.backup
/ssh-bastion.conf
\ No newline at end of file
[ssh_connection]
pipelining=True
ssh_args = -F ./ssh-bastion.conf -o ControlMaster=auto -o ControlPersist=30m
control_path = ~/.ssh/ansible-%%r@%%h:%%p
[defaults]
host_key_checking=False
gathering = smart
......
---
- hosts: localhost
gather_facts: False
roles:
- bastion-ssh-config
- hosts: all
any_errors_fatal: true
gather_facts: false
......@@ -16,7 +21,7 @@
any_errors_fatal: true
gather_facts: true
- hosts: all:!network-storage
- hosts: all:!network-storage:!bastion
any_errors_fatal: true
roles:
- { role: kubernetes/preinstall, tags: preinstall }
......
......@@ -7,6 +7,9 @@
# node5 ansible_ssh_host=95.54.0.16 # ip=10.3.0.5
# node6 ansible_ssh_host=95.54.0.17 # ip=10.3.0.6
# ## configure a bastion host if your nodes are not publicly reachable
# bastion ansible_ssh_host=xxx.xxx.xxx.xxx
# [kube-master]
# node1
# node2
......
---
- set_fact:
has_bastion: "{{ 'bastion' in groups['all'] }}"
- set_fact:
bastion_ip: "{{ hostvars['bastion']['ansible_ssh_host'] }}"
when: has_bastion
# As we are actually running on localhost, the ansible_ssh_user is your local user when you try to use it directly
# To figure out the real ssh user, we delegate this task to the bastion and store the ansible_ssh_user in real_user
- set_fact:
real_user: "{{ ansible_ssh_user }}"
delegate_to: bastion
when: has_bastion
- name: create ssh bastion conf
become: false
template: src=ssh-bastion.conf dest="{{ playbook_dir }}/ssh-bastion.conf"
{% if has_bastion %}
{% set vars={'hosts': ''} %}
{% set user='' %}
{% for h in groups['all'] %}
{% if h != 'bastion' %}
{% if vars.update({'hosts': vars['hosts'] + ' ' + hostvars[h]['ansible_ssh_host']}) %}{% endif %}
{% endif %}
{% endfor %}
Host {{ bastion_ip }}
Hostname {{ bastion_ip }}
StrictHostKeyChecking no
ControlMaster auto
ControlPath ~/.ssh/ansible-%r@%h:%p
ControlPersist 5m
Host {{ vars['hosts'] }}
ProxyCommand ssh -W %h:%p {{ real_user }}@{{ bastion_ip }}
StrictHostKeyChecking no
{% endif %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment