memiah.autossh
AutoSSH
This is an Ansible Role that helps you set up and manage multiple SSH connections using autossh. It enables you to forward ports from your local server to remote servers securely.
Requirements
You need to have an SSH user with the right permissions to access the remote server, and an SSH key must be available on your local server (usually in /root/.ssh/id_rsa
or specified for each connection).
Role Variables
Here are the variables you can use, along with their default values (found in defaults/main.yml
):
- autossh_connections: A list where you define each SSH connection with these properties:
- id: A unique identifier for the connection (use "a-z,0-9,-").
- user: The SSH username for the remote server.
- server: The IP address or hostname of the remote server.
- server_key_type: The type of key for the remote server (optional).
- local_port: The local port you want to forward.
- dest_server: The destination IP/hostname on the remote server (optional).
- dest_port: The port on the remote server to connect to.
- identity_file: The path to the SSH private key (optional).
You can add as many connections as you need in this list.
autossh_path: The path to the autossh executable, default is
/usr/bin/autossh
.autossh_ssh_dir: The directory for storing SSH configurations, default is
/root/.ssh
.autossh_systemd_dir: The location for the systemd services, default is
/etc/systemd/system/
.autossh_known_hosts_file: The path to the known hosts file, default is
{{ autossh_ssh_dir }}/known_hosts
.autossh_default_server_key_type: The default key type for the remote server (defaults to "ecdsa"). You can change this for individual connections.
autossh_default_dest_server: The default IP/hostname for the remote server, usually set to localhost (127.0.0.1). You can override this for individual connections.
autossh_default_identity_file: The default path to the identity file (private key), default is
/root/.ssh/id_rsa
. You can change this for individual connections.
Dependencies
There are no additional dependencies.
Example Playbook
Here's a simple example of how to use this role in a playbook:
- hosts: servers
vars_files:
- vars/main.yml
roles:
- { role: memiah.autossh }
In vars/main.yml
:
autossh_connections:
- id: "example"
user: "username"
server: "remote.server"
local_port: "33061"
dest_port: "3306"
This setup will forward local port 33061
to port 3306
on remote.server
, logging in as [email protected]
.
A new service called autossh-example.service
will be created, which you can manage using systemctl commands:
systemctl status autossh-example.service
systemctl restart autossh-example.service
...
License
This project is licensed under the MIT/BSD license.
Author Information
This role was created in 2016 by Memiah Limited.
Install and configure autossh connections on RedHat systems.
ansible-galaxy install memiah.autossh