From 559128450c9f52152e93e435f3bfa637c9670a01 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 28 Nov 2024 09:56:02 +0100 Subject: [PATCH] Kapitel 08: register/ignore_errors --- 08/grep_einfach.yml | 12 +++++++++ 08/grep_einfach_fehler.yml | 13 ++++++++++ 08/grep_username.yml | 25 +++++++++++++++++++ 08/var_definiert_oder_nicht.yml | 18 ++++++++++++++ 08/weiteres/debug1.yml | 13 ++++++++++ 08/weiteres/debug2.yml | 14 +++++++++++ 08/weiteres/debug_folie.yml | 9 +++++++ 08/weiteres/debug_zusammen.yml | 18 ++++++++++++++ extra/changed_failed_when.yml | 18 ++++++++++++++ extra/changed_when.yml | 9 +++++++ extra/ein_rechner_failed.yml | 26 ++++++++++++++++++++ extra/wireguard-key-anlegen_changed_when.yml | 8 ++++++ extra/wireguard-key-anlegen_creates.yml | 8 ++++++ 13 files changed, 191 insertions(+) create mode 100644 08/grep_einfach.yml create mode 100644 08/grep_einfach_fehler.yml create mode 100644 08/grep_username.yml create mode 100644 08/var_definiert_oder_nicht.yml create mode 100644 08/weiteres/debug1.yml create mode 100644 08/weiteres/debug2.yml create mode 100644 08/weiteres/debug_folie.yml create mode 100644 08/weiteres/debug_zusammen.yml create mode 100644 extra/changed_failed_when.yml create mode 100644 extra/changed_when.yml create mode 100644 extra/ein_rechner_failed.yml create mode 100644 extra/wireguard-key-anlegen_changed_when.yml create mode 100644 extra/wireguard-key-anlegen_creates.yml diff --git a/08/grep_einfach.yml b/08/grep_einfach.yml new file mode 100644 index 0000000..eecc86f --- /dev/null +++ b/08/grep_einfach.yml @@ -0,0 +1,12 @@ +--- +- hosts: localhost + gather_facts: no + + tasks: + - name: grep aufrufen + command: grep root /etc/passwd + register: output + + - name: Rückgabe ausgeben + debug: var=output + diff --git a/08/grep_einfach_fehler.yml b/08/grep_einfach_fehler.yml new file mode 100644 index 0000000..50d5cbb --- /dev/null +++ b/08/grep_einfach_fehler.yml @@ -0,0 +1,13 @@ +--- +- hosts: localhost + gather_facts: no + + tasks: + - name: grep aufrufen + command: grep toor /etc/passwd + register: output + ignore_errors: true + + - name: Rückgabe ausgeben + debug: var=output + diff --git a/08/grep_username.yml b/08/grep_username.yml new file mode 100644 index 0000000..d09ba8e --- /dev/null +++ b/08/grep_username.yml @@ -0,0 +1,25 @@ +--- +- hosts: localhost + gather_facts: no + + tasks: + - name: Variable muss definiert sein + assert: + that: + - username is defined + + - name: Rufe grep auf + command: 'grep {{ username }} /etc/passwd' + register: result + ignore_errors: True + + - name: "{{ username }} existiert" + debug: + msg: '{{ username }} exists' + when: result is success + + - name: "{{ username }} existiert nicht" + debug: + msg: '{{ username }} does not exist' + when: result is failed + diff --git a/08/var_definiert_oder_nicht.yml b/08/var_definiert_oder_nicht.yml new file mode 100644 index 0000000..e987797 --- /dev/null +++ b/08/var_definiert_oder_nicht.yml @@ -0,0 +1,18 @@ +--- +- hosts: localhost + gather_facts: no + + tasks: + - debug: + msg: "{{ inhalt }}" + when: inhalt|default(False) + + - debug: + msg: "Variable nicht definiert" + when: inhalt is not defined + + - debug: + msg: + - "inhalt|default(False) → {{ inhalt|default(False) }} → {{ inhalt|default(False)|bool }}" + - "inhalt is not defined → {{ inhalt is not defined }} → {{ inhalt is not defined|bool }}" + diff --git a/08/weiteres/debug1.yml b/08/weiteres/debug1.yml new file mode 100644 index 0000000..37cf14a --- /dev/null +++ b/08/weiteres/debug1.yml @@ -0,0 +1,13 @@ +--- +- hosts: localhost + + tasks: + - command: grep root /etc/passwd + register: output + + - debug: + var: output + + - debug: + var: output.stdout + diff --git a/08/weiteres/debug2.yml b/08/weiteres/debug2.yml new file mode 100644 index 0000000..bf77aed --- /dev/null +++ b/08/weiteres/debug2.yml @@ -0,0 +1,14 @@ +--- +- hosts: localhost + + tasks: + - command: grep toor /etc/passwd + register: output + ignore_errors: yes + + - debug: + var: output + + - debug: + var: output.stdout + diff --git a/08/weiteres/debug_folie.yml b/08/weiteres/debug_folie.yml new file mode 100644 index 0000000..80b55fc --- /dev/null +++ b/08/weiteres/debug_folie.yml @@ -0,0 +1,9 @@ +--- +- hosts: localhost + tasks: + - command: grep root2 /etc/passwd + register: output + ignore_errors: True + + - debug: var=output + diff --git a/08/weiteres/debug_zusammen.yml b/08/weiteres/debug_zusammen.yml new file mode 100644 index 0000000..44a92a2 --- /dev/null +++ b/08/weiteres/debug_zusammen.yml @@ -0,0 +1,18 @@ +--- +- hosts: localhost + + tasks: + - command: grep root /etc/passwd + register: abc1 + ignore_errors: yes + + - command: grep toor /etc/passwd + register: abc2 + ignore_errors: yes + + - debug: + msg: "{{ abc1.failed }} --- {{ abc2.failed }}" + + - debug: + msg: "{{ abc1 is failed }} --- {{ abc2 is failed }}" + diff --git a/extra/changed_failed_when.yml b/extra/changed_failed_when.yml new file mode 100644 index 0000000..63b2e14 --- /dev/null +++ b/extra/changed_failed_when.yml @@ -0,0 +1,18 @@ +--- +- hosts: + - tn00-debian12 + - tn00-alpine3i + + gather_facts: no + + tasks: + - name: "Gebe CHANGED zurück, wenn /etc/hostname den String 'debian' enthält" + command: cat /etc/hostname + register: output + changed_when: '"debian" in output.stdout' + + - name: "Gebe FAILED zurück, wenn /etc/hostname den String 'alpine' enthält" + command: cat /etc/hostname + register: output + failed_when: '"alpine" in output.stdout' + diff --git a/extra/changed_when.yml b/extra/changed_when.yml new file mode 100644 index 0000000..5c396fb --- /dev/null +++ b/extra/changed_when.yml @@ -0,0 +1,9 @@ +--- +- hosts: all + gather_facts: no + + tasks: + - command: cat /etc/hosts + register: output + changed_when: '"ubu" in output.stdout' + diff --git a/extra/ein_rechner_failed.yml b/extra/ein_rechner_failed.yml new file mode 100644 index 0000000..9e74dcd --- /dev/null +++ b/extra/ein_rechner_failed.yml @@ -0,0 +1,26 @@ +--- +- hosts: + - tn00-debian12 + - tn00-alpine3i + + gather_facts: no + + tasks: + - name: Debian bricht ab + debug: + msg: "Hallo" + failed_when: '"debian" in inventory_hostname' + + +- hosts: + - tn00-debian12 + - tn00-alpine3i + + gather_facts: no + + tasks: + - name: "Wer lebt noch?" + debug: + msg: "Lebenszeichen" + + diff --git a/extra/wireguard-key-anlegen_changed_when.yml b/extra/wireguard-key-anlegen_changed_when.yml new file mode 100644 index 0000000..11592d0 --- /dev/null +++ b/extra/wireguard-key-anlegen_changed_when.yml @@ -0,0 +1,8 @@ +--- +- hosts: localhost + gather_facts: no + tasks: + - shell: "[ ! -f wg.pub ] && wg genkey | tee wg.priv | wg pubkey | tee wg.pub || true" + register: output + changed_when: output.stdout != "" + diff --git a/extra/wireguard-key-anlegen_creates.yml b/extra/wireguard-key-anlegen_creates.yml new file mode 100644 index 0000000..09b30cc --- /dev/null +++ b/extra/wireguard-key-anlegen_creates.yml @@ -0,0 +1,8 @@ +--- +- hosts: localhost + gather_facts: no + tasks: + - shell: "wg genkey | tee wg.priv | wg pubkey | tee wg.pub" + args: + creates: wg.pub +