From 26d781da63ad0368921d6a0eb1f5efcb9bc479d1 Mon Sep 17 00:00:00 2001
From: Student User <student@workstation.lab.example.com>
Date: Wed, 19 Feb 2020 13:08:19 +0100
Subject: [PATCH] fix hosts template

---
 simple-fetch.yaml              |   11 +++++++++++
 stub-var.yaml                  |    7 ++++++-
 hosts.j2                       |    5 ++---
 play-with-hosts-template.yaml  |    2 ++
 motd-complex.j2                |    9 ++++++---
 play-with-complex-template.yml |    8 ++++++++
 6 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/hosts.j2 b/hosts.j2
index 377897d..7da9acf 100644
--- a/hosts.j2
+++ b/hosts.j2
@@ -1,5 +1,4 @@
 # template for /etc/hosts
 {% for host in groups['all'] %}
-{{ hostvars['host']['ansible_facts'] }}
-{# {{ hostvars['host']['ansible_facts']['default_ipv4']['address'] }} {{ hostvars['host']['ansible_facts']['fqdn'] }} {{ hostvars['host']['ansible_facts']['hostname'] }} #}
-{% endfor %}
\ No newline at end of file
+{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
+{% endfor %}
diff --git a/motd-complex.j2 b/motd-complex.j2
index 067253a..e12292c 100644
--- a/motd-complex.j2
+++ b/motd-complex.j2
@@ -8,8 +8,11 @@
 
 This system has the following IPv4 addresses configured:
 {% for myip in ansible_facts['all_ipv4_addresses'] %}
-- IP #{{ loop.index }}: {{ myip }}
-
-Debug: {{ loop }}
+- IP #{{ loop.index }} of {{ loop.length }}: {{ myip }}
 {% endfor %}
 
+{% for member in groups['server'] %}
+Part of the server group: {{ member }}
+{% endfor %}
+
+{{ ansible_facts['all_ipv4_addresses'] | to_nice_json }}
\ No newline at end of file
diff --git a/play-with-complex-template.yml b/play-with-complex-template.yml
index 40338a7..15c5473 100644
--- a/play-with-complex-template.yml
+++ b/play-with-complex-template.yml
@@ -2,6 +2,14 @@
 - name: Simple play for class DO407 which introduces complex j2 templates
   hosts: server, desktop, demoext
   become: true
+  vars:
+    users:
+      - bob:
+          name: Bob Andersen
+          home: /home/bob
+      - anne:
+          name: Anne Clark
+          home: /home/anne
   tasks:
     - name: use template to create /etc/motd
       template:
diff --git a/play-with-hosts-template.yaml b/play-with-hosts-template.yaml
index de8d2f4..7c77300 100644
--- a/play-with-hosts-template.yaml
+++ b/play-with-hosts-template.yaml
@@ -3,6 +3,8 @@
   hosts: server, desktop, demoext
   become: true
   tasks:
+    - debug:
+        msg: '{{ hostvars["servera"]["ansible_facts"]["default_ipv4"]["address"] }}'
     - name: use template to create /etc/hosts-new
       template:
         src: hosts.j2
diff --git a/simple-fetch.yaml b/simple-fetch.yaml
new file mode 100644
index 0000000..9f13e39
--- /dev/null
+++ b/simple-fetch.yaml
@@ -0,0 +1,11 @@
+---
+- name: simple fetch
+  hosts: server
+  become: true
+  tasks:
+    - name: fetch /etc/motd
+      fetch:
+        src: /etc/motd
+        #dest: '/tmp/motd-{{ ansible_facts["fqdn"] }}'
+        dest: /tmp/motds
+        flat: false
diff --git a/stub-var.yaml b/stub-var.yaml
index 2ea5ae4..da9c36a 100644
--- a/stub-var.yaml
+++ b/stub-var.yaml
@@ -5,4 +5,9 @@
   tasks:
     - name: print a var
       debug:
-        msg: '{{ myvar }}'
+        msg:
+          - Line 1
+          - var '{{ myvar }}'
+          - Line 2
+
+          

--
Gitblit v1.9.3