Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible multi hop design

Tags:

ssh

ansible

I would like to run an ansible playbook on a target host passing through multiple hosts. The scenario looks similar to the one depicted in the picture:

enter image description here

I partially solved issue creating the ssh_config file in the Ansible project directory:

Host IP_HostN
        HostName IP_HOST_N
        ProxyJump Username1@IP_HOST_2:22,Username2@IP_HOST_2:22
        User UsernameN

and defining in the ansible.cfg in the Ansible project directory:

[ssh_connection]
ssh_args= -F "ssh_config"

The problem is that I need to insert automatically for each transient hosts and target host ssh username and password and I don't know how to automate this task. Moreover, python may not be installed on every transient node.

like image 546
gaetano Avatar asked Nov 15 '17 14:11

gaetano


1 Answers

I found a reasonably good workaround. According to the scenario below:

enter image description here

we create an ssh tunnel until the transient host that can directly reach the target host. We also create a local port binding with -L flag:

ssh  -J user_1@transient_host1:port_1 -p port_2 user_2@transient_host2  -L LOCAL_PORT:TARGET_HOST_IP:TARGET_HOST_PORT

Then we can directly enter into Target Host using the local binding:

ssh user_target_host@localhost -p LOCAL_PORT

In this way, we can run ansible playbooks on the local host configuring ansible variables accordingly:

ansible_host: localhost
ansible_user: user_target_host
ansible_port: LOCAL_PORT
ansible_password: password_target_host
like image 160
gaetano Avatar answered Oct 05 '22 02:10

gaetano