Kapitel 08: register/ignore_errors

This commit is contained in:
Sven Velt 2024-11-28 09:56:02 +01:00
parent e0f5beb46d
commit 559128450c
13 changed files with 191 additions and 0 deletions

12
08/grep_einfach.yml Normal file
View file

@ -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

View file

@ -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

25
08/grep_username.yml Normal file
View file

@ -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

View file

@ -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 }}"

13
08/weiteres/debug1.yml Normal file
View file

@ -0,0 +1,13 @@
---
- hosts: localhost
tasks:
- command: grep root /etc/passwd
register: output
- debug:
var: output
- debug:
var: output.stdout

14
08/weiteres/debug2.yml Normal file
View file

@ -0,0 +1,14 @@
---
- hosts: localhost
tasks:
- command: grep toor /etc/passwd
register: output
ignore_errors: yes
- debug:
var: output
- debug:
var: output.stdout

View file

@ -0,0 +1,9 @@
---
- hosts: localhost
tasks:
- command: grep root2 /etc/passwd
register: output
ignore_errors: True
- debug: var=output

View file

@ -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 }}"

View file

@ -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'

9
extra/changed_when.yml Normal file
View file

@ -0,0 +1,9 @@
---
- hosts: all
gather_facts: no
tasks:
- command: cat /etc/hosts
register: output
changed_when: '"ubu" in output.stdout'

View file

@ -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"

View file

@ -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 != ""

View file

@ -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