From e38dca55f2c8f7445d6585d22332617ff7a2b2aa Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Thu, 2 Jun 2016 16:20:11 +0200
Subject: [PATCH] Improve handling of /etc/hosts files

---
 tasks/main.yml | 15 ++++++++++++++-
 tasks/more.yml | 17 +++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 tasks/more.yml

diff --git a/tasks/main.yml b/tasks/main.yml
index 51d81a8..7dbe11f 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -24,7 +24,20 @@
       mode='644'
     with_flattened:
       - '{{ admins }}'
-      - '{{ jailusers }}'
+      - '{{ jailusers|default([]) }}'
+
+  - name: "a1"
+    shell: ls ~/.ansible/hostvars
+    become: no
+    register: fact_files
+    failed_when: false
+    changed_when: false
+    ignore_errors: true
+
+  - include: more.yml
+    with_items: '{{ fact_files.stdout_lines|default([]) }}'
+    loop_control:
+      loop_var: inventory_name
 
   - name: 'Remember that this role had been run'
     set_fact: role_commonconnect_completed=true
diff --git a/tasks/more.yml b/tasks/more.yml
new file mode 100644
index 0000000..bdd2654
--- /dev/null
+++ b/tasks/more.yml
@@ -0,0 +1,17 @@
+---
+# file: roles/commonconnect/tasks/more.yml
+
+- name: "Read facts"
+  shell: cat ~/.ansible/hostvars/{{ inventory_name }}
+  become: no
+  register: result_facts
+
+- set_fact: facts="{{ result_facts.stdout | from_json }}"
+
+- name: "Add Hosts to Hostfile"
+  lineinfile:
+    dest='/etc/hosts'
+    regexp='.* {{ item }}$'
+    line='{{ facts[item].static_ipv4 }} {{ item }}'
+    state=present
+  with_items: '{{ facts }}'
-- 
GitLab