additional course material RH294
Student User
2020-02-19 26d781da63ad0368921d6a0eb1f5efcb9bc479d1
fix hosts template
1 files added
5 files modified
42 ■■■■ changed files
hosts.j2 5 ●●●●● patch | view | raw | blame | history
motd-complex.j2 9 ●●●●● patch | view | raw | blame | history
play-with-complex-template.yml 8 ●●●●● patch | view | raw | blame | history
play-with-hosts-template.yaml 2 ●●●●● patch | view | raw | blame | history
simple-fetch.yaml 11 ●●●●● patch | view | raw | blame | history
stub-var.yaml 7 ●●●● patch | view | raw | blame | history
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 %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
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 }}
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:
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
simple-fetch.yaml
New file
@@ -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
stub-var.yaml
@@ -5,4 +5,9 @@
  tasks:
    - name: print a var
      debug:
        msg: '{{ myvar }}'
        msg:
          - Line 1
          - var '{{ myvar }}'
          - Line 2