Kapitel 08: Conditionals, Register

This commit is contained in:
Sven Velt 2024-09-18 14:10:22 +02:00
parent c2da4dcb4e
commit 83ebe7833b
9 changed files with 129 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

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

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

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