#!/bin/bash

export AUTOSSH_PIDFILE=~/autossh-{{ item.ssh_server }}-{{ item.remote_port }}.pid
export AUTOSSH_POLL=60
export AUTOSSH_GATETIME=30
export AUTOSSH_DEBUG=1
export AUTOSSH_LOGFILE=~/autossh-{{ item.ssh_server }}-{{ item.remote_port }}.log

{% if local_key_file is defined %}
IDENTITY_ARG="-i {{ local_key_file }}"
{% else %}
IDENTITY_ARG=""
{% endif %}

{% if item.remote_ssh_port is defined %}
SSH_PORT="{{ item.remote_ssh_port }}"
{% else %}
SSH_PORT="22"
{% endif %}

/usr/bin/autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
  -o "ExitOnForwardFailure yes" -o "StrictHostKeyChecking {{ autossh_strict_host_key_checking }}" \
  ${IDENTITY_ARG} -p ${SSH_PORT} \
  -L {{ local_interface }}:{{ item.local_port }}:{{ remote_server }}:{{ item.remote_port }} \
  {{ item.ssh_user }}@{{ item.ssh_server }}
