From 507a71182378164c326b1189c7ced5c42b6e8cab Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 25 Dec 2017 11:33:06 +0000
Subject: [PATCH] Improve commonauth_extra handling and introduce SSH proxy

---
 tasks/main.yml                            |  4 ++--
 templates/{etc_hosts => etc_hosts.jinja2} |  6 +++---
 templates/ssh_config                      | 18 ----------------
 templates/ssh_config.jinja2               | 25 +++++++++++++++++++++++
 4 files changed, 30 insertions(+), 23 deletions(-)
 rename templates/{etc_hosts => etc_hosts.jinja2} (90%)
 delete mode 100644 templates/ssh_config
 create mode 100644 templates/ssh_config.jinja2

diff --git a/tasks/main.yml b/tasks/main.yml
index 4eb2531..6221bbf 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -9,7 +9,7 @@
 
   - name: "Create The /etc/hosts File"
     template:
-      src: 'etc_hosts'
+      src: 'etc_hosts.jinja2'
       dest: '/etc/hosts'
       owner: 'root'
       group: 'root'
@@ -17,7 +17,7 @@
 
   - name: "Copy SSH Config For Server-To-Server Communication For Admins"
     template:
-      src: 'ssh_config'
+      src: 'ssh_config.jinja2'
       dest: '/home/{{ item }}/.ssh/config'
       owner: '{{ item }}'
       group: 'root'
diff --git a/templates/etc_hosts b/templates/etc_hosts.jinja2
similarity index 90%
rename from templates/etc_hosts
rename to templates/etc_hosts.jinja2
index ce413a4..e4201d9 100644
--- a/templates/etc_hosts
+++ b/templates/etc_hosts.jinja2
@@ -46,9 +46,9 @@ ff02::2	  ip6-allrouters
 {% endif %}
 {% endfor %}
 
-{% for host in commonauth_extras|default([]) %}
+{% for hostname, host in (commonauth_extras|default([])).iteritems() %}
 {% if host.ip is defined %}
-{{host.ip}}{% for name in host['names'] %} {{name}}{% endfor %}
-{% endif %}
+{{ host.ip }} {{ hostname }}{% for alias in host.aliases|default([]) %} {{ alias }}{% endfor %}
 
+{% endif %}
 {% endfor %}
diff --git a/templates/ssh_config b/templates/ssh_config
deleted file mode 100644
index af81dc0..0000000
--- a/templates/ssh_config
+++ /dev/null
@@ -1,18 +0,0 @@
-# See all parameters at http://www.openbsd.org/cgi-bin/man.cgi?query=ssh_config
-
-StrictHostKeyChecking no
-ForwardAgent no
-
-{% for host in commonauth_extras|default([]) %}
-{% if host.ssh is defined %}
-{% for name in host.names %}
-Host {{ name }}
-{% for key, value in host.ssh.iteritems() %}
-  {{ key }} {{ value }}
-{% endfor %}
-
-{% endfor %}
-{% endif %}
-{% endfor %}
-Host *
-  IdentityFile ~/.ssh/id_rsa
diff --git a/templates/ssh_config.jinja2 b/templates/ssh_config.jinja2
new file mode 100644
index 0000000..9440e6e
--- /dev/null
+++ b/templates/ssh_config.jinja2
@@ -0,0 +1,25 @@
+# See all parameters at http://www.openbsd.org/cgi-bin/man.cgi?query=ssh_config
+
+StrictHostKeyChecking no
+ForwardAgent no
+
+{% if commonauth_extras is defined %}
+{% for hostname, host in commonauth_extras.iteritems() %}
+{% if host.ssh is defined or host.proxy is defined %}
+Host {{ hostname }}{% for alias in host.aliases|default([]) %} {{ alias }}{% endfor %}
+
+{% if host.ssh is defined %}
+{% for key, value in host.ssh.iteritems() %}
+  {{ key }} {{ value }}
+{% endfor %}
+{% endif %}
+{% if host.proxy is defined %}
+  ForwardAgent yes
+  ProxyCommand /usr/bin/ssh {{ host.proxy }} /bin/nc %h %p
+{% endif %}
+
+{% endif %}
+{% endfor %}
+{% endif %}
+Host *
+  IdentityFile ~/.ssh/id_rsa
-- 
GitLab