Kapitel 08: Lookups, Loops, Includes

This commit is contained in:
Sven Velt 2024-11-27 16:20:25 +01:00
parent 122b5da264
commit e0f5beb46d
14 changed files with 123 additions and 0 deletions

11
08/copy_3files_debug.yml Normal file
View file

@ -0,0 +1,11 @@
---
- hosts: localhost
tasks:
- name: "[DEBUG] Copy 3 files"
ansible.builtin.debug:
msg: "src={{ item.0 }} dest={{ item.1 }}"
loop:
- [config1.cfg, /etc/config1.cfg]
- [config2.cfg, /etc/config2.cfg]
- [config3.cfg, /etc/daemon/config3.cfg]

View file

@ -0,0 +1,11 @@
---
- hosts: localhost
tasks:
- name: "[DEBUG] Copy 3 files"
ansible.builtin.debug:
msg: "src={{ item|basename }} dest={{ item }}"
loop:
- /etc/config1.cfg
- /etc/config2.cfg
- /etc/daemon/config3.cfg

10
08/loop_include.yml Normal file
View file

@ -0,0 +1,10 @@
---
- hosts: localhost
tasks:
- include_tasks: loop_include_sub.yml
loop:
- Aussen-1
- Aussen-2
loop_control:
loop_var: outer

9
08/loop_include_sub.yml Normal file
View file

@ -0,0 +1,9 @@
---
- debug: "msg='Innen: {{ item }}, Außen: {{ outer }} - {{ loop_index }}'"
loop:
- Innen-1
- Innen-2
loop_control:
label: "Aktueller Durchlauf {{ item }} - {{ loop_index }}"
index_var: loop_index

7
08/template-loop.yml Normal file
View file

@ -0,0 +1,7 @@
---
- hosts: localhost
gather_facts: no
roles:
- template-loop

38
08/uebung_lookup.yml Normal file
View file

@ -0,0 +1,38 @@
---
- hosts: localhost
gather_facts: no
tasks:
- name: File-Loopup /etc/timezone
debug:
msg: "/etc/timezone: {{ lookup('file', '/etc/timezone') }}"
- name: Password-Lookup 1
debug:
msg: "Passwort: {{ lookup('password', '/tmp/.secret.pw') }}"
- name: Password-Lookup 2
debug:
msg: "Passwort: {{ lookup('password', '/tmp/.secret.pw') }}"
- name: rm /tmp/.secret.pw
file:
dest: /tmp/.secret.pw
state: absent
- name: Password-Lookup 3
debug:
msg: "Passwort: {{ lookup('password', '/tmp/.secret.pw') }}"
- name: Password-Lookup 4
debug:
msg: "Passwort: {{ lookup('password', '/tmp/.secret.pw') }}"
- name: dig-Lookup
debug:
var: "lookup('dig', 'heinlein-akademie.de', 'qtype=MX')"
- name: dig-Query
debug:
var: "query('dig', 'heinlein-akademie.de', 'qtype=MX')"

5
helper/08_packages.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/bash
sudo apt install --no-install-recommends --yes \
python3-dnspython \

View file

@ -0,0 +1,32 @@
---
# Template-Dateien:
# template-loop/templates/etc/abc.conf
# template-loop/templates/usr/local/share/abc.conf
# template-loop/templates/var/lib/abc.conf
- name: Template-Loop mit relativen Pfaden
debug:
msg: "Template-Argumente: src={{ item }} dest=/{{ item }} ..."
loop:
- etc/abc.conf
- usr/local/share/abc.conf
- var/lib/abc.conf
- name: Template-Loop mit absoluten Pfaden
debug:
msg: "Template-Argumente: src={{ item|replace('/', '', count=1) }} dest={{ item }} ..."
loop:
- /etc/abc.conf
- /usr/local/share/abc.conf
- /var/lib/abc.conf
- name: Template-Loop mit Mapping
debug:
msg: "Template-Argumente: src={{ item.key }} dest={{ item.value }} ..."
loop: "{{ configfiles|dict2items }}"
vars:
configfiles:
etc.conf: /etc/abc.conf
share.conf: /usr/local/share/abc.conf
lib.conf: /var/lib/abc.conf

View file

View file

View file