Skip to content

Commit 139918b

Browse files
Merge pull request #255 from puzzle/psc_corrections
newest refinements after last workshop
2 parents 84ea187 + cad913c commit 139918b

File tree

6 files changed

+87
-21
lines changed

6 files changed

+87
-21
lines changed

content/en/docs/01/_index.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ ansible all -i hosts -m ansible.builtin.ping
184184

185185
* Configure the `ansible` user to have root privilege on all hosts in your inventory file.
186186
Also enable login without a password for this user.
187-
* Test the functionality by running `sudo -v` as user `ansible` on all nodes.
187+
* Test the functionality by running `sudo -v` as user `ansible` on all nodes. No output after `sudo -v` indicates that the sudoers configuration is set up correctly.
188188

189189
{{% details title="Solution Task 4" %}}
190190

content/en/docs/03/_index.en.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ Complete the next steps using ansible ad hoc commands:
145145
Possible solution 1:
146146

147147
```bash
148-
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=touch"
149-
ansible node2 -i hosts -m copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
150-
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=absent"
148+
ansible node2 -i hosts -m ansible.builtin.file -a "path=/home/ansible/testfile.txt state=touch"
149+
ansible node2 -i hosts -m ansible.builtin.copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
150+
ansible node2 -i hosts -m ansible.builtin.file -a "path=/home/ansible/testfile.txt state=absent"
151151
```
152152

153153
Possible solution 2:
154154
The copy module can create the file directly
155155

156156
```bash
157-
ansible node2 -i hosts -m copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
158-
ansible node2 -i hosts -m file -a "path=/home/ansible/testfile.txt state=absent"
157+
ansible node2 -i hosts -m ansible.builtin.copy -a "dest=/home/ansible/testfile.txt content='SOME RANDOM TEXT'"
158+
ansible node2 -i hosts -m ansible.builtin.file -a "path=/home/ansible/testfile.txt state=absent"
159159
```
160160
{{% /details %}}
161161

content/en/docs/05/01/_index.en.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ then do create a file `README.TXT` in this folder containing the text "This fold
1616
* The value of `<timestamp>` should contain a quite accurate timestamp of when `ansible-playbook` was run.
1717
* Run the playbook several times to see if it is really idempotent.
1818

19+
{{% alert title="Hint" color="primary" %}}
20+
There is an ansible fact for when ansible was run.
21+
{{% /alert %}}
22+
1923
{{% details title="Solution Task 1" %}}
2024
Below is a possible solution:
2125

content/en/docs/05/_index.en.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ $ ansible-playbook webserver.yml
8282
### Task 4
8383

8484
* Create a new role called `base`.
85-
Its file `tasks/main.yml` should import the files `motd.yml` and `packages.yml`.
86-
(Create both files under `tasks/`).
85+
Its file `tasks/main.yml` should import the taskfiles `motd.yml` and `packages.yml` by using the `import_tasks` module. Create the two files `motd.yml` and `packages.yml` under `tasks/`.
86+
* Tag the import of the `motd.yml` file with the string `motd` and the import of the `packages.yml` file with the string `packages`.
8787
* `motd.yml` should do the following: Use the variable `base_motd_content` to change the `/etc/motd` content to
8888
"This is a server\\n".
8989
Remember to move the template as well as the variable to a correct location in the `roles` folder.
@@ -163,6 +163,7 @@ Take notice of the different content of `/etc/motd` on the control node!
163163
Use a dependency in the `meta/main.yml` file.
164164
* Remove the play to run `base` role on all hosts in the `prod.yml` playbook.
165165
Run the playbook and see if role `base` was applied on hosts in the `web` group as well.
166+
* Run the the playbook once with the tag `motd` and once with the tag `packages`. Ensure, that only the tasks where run where the corresponding tag was set.
166167

167168
{{% details title="Solution Task 5" %}}
168169

@@ -179,6 +180,8 @@ $ cat prod.yml
179180
- httpd
180181

181182
$ ansible-playbook prod.yml
183+
$ ansible-playbook prod.yml -t motd
184+
$ ansible-playbook prod.yml -t packages
182185
```
183186

184187
{{% /details %}}

content/en/docs/06/_index.en.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ ansible-playbook secretservice.yml
188188
ansible node1,node2 -i hosts -b -m ansible.builtin.file -a "path=/etc/MI6 state=absent"
189189
```
190190

191+
If you moved the inventory file `hosts` in the labs before, then ensure to use the correct location:
192+
193+
```bash
194+
ansible node1,node2 -i inventory/hosts -b -m ansible.builtin.file -a "path=/etc/MI6 state=absent"
195+
```
196+
191197
{{% /details %}}
192198

193199
### Task 7

slides/ansible-techlab/puzzle-demo.md

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,28 @@ Templates sind dazu da, um komplexe Files zu erstellen (Variabeln sowie `if` / `
10371037
{{ vm.ip }} {{ vm.name }}
10381038
{% endfor %}
10391039
```
1040+
1041+
<!-- .slide: class="master-content" > -->
1042+
***
1043+
1044+
## Playbook Keywords
1045+
1046+
- "Functionality" / "Behaviour"
1047+
- Different for Play/Task/Role/Block
1048+
- https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html
1049+
1050+
<!-- .slide: class="master-content" > -->
1051+
***
1052+
1053+
## Keyword examples
1054+
1055+
- register
1056+
- no_log
1057+
- run_once
1058+
- retry
1059+
- delay
1060+
- until
1061+
10401062
<!-- .slide: class="master-content" > -->
10411063
***
10421064

@@ -1045,6 +1067,8 @@ Templates sind dazu da, um komplexe Files zu erstellen (Variabeln sowie `if` / `
10451067
- https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html
10461068

10471069
<!-- .slide: class="master-content" > -->
1070+
1071+
10481072
***
10491073
## Tags
10501074
Example:
@@ -1427,7 +1451,7 @@ The task's value of `notify` must match the handler's value of `name` or `listen
14271451
## Handlers
14281452
Example Playbook:
14291453
1430-
```yaml [1|2-9|10-14]
1454+
```yaml [9|11]
14311455
- hosts: all
14321456
tasks:
14331457
- name: Write configuration file
@@ -1448,7 +1472,7 @@ Example Playbook:
14481472
## Handlers
14491473
Example Playbook:
14501474

1451-
```yaml [9|10|11|15]
1475+
```yaml [9|11|15]
14521476
- hosts: all
14531477
tasks:
14541478
- name: Put configuration file
@@ -1485,11 +1509,10 @@ Caveat:
14851509

14861510
→ best practice: use `listen`
14871511
<!-- .slide: class="master-content" > -->
1488-
***
1512+
14891513
## Handlers
14901514
Only the second handler will run when a task notifies `restart web services`
1491-
1492-
```yaml
1515+
```yaml [1-9|2,6]
14931516
handlers:
14941517
- name: restart web services
14951518
ansible.builtin.systemd_service:
@@ -1501,10 +1524,13 @@ handlers:
15011524
state: restarted
15021525
```
15031526
<!-- .slide: class="master-content" > -->
1527+
1528+
***
1529+
15041530
***
15051531
## Handlers
15061532
Both handlers are run when a task notifies `restart web services`
1507-
```yaml [6,11]
1533+
```yaml [1-11|6,11]
15081534
handlers:
15091535
- name: Restart memcached
15101536
ansible.builtin.service:
@@ -1518,6 +1544,7 @@ handlers:
15181544
listen: "restart web services"
15191545
```
15201546
<!-- .slide: class="master-content" > -->
1547+
15211548
***
15221549
## Handlers
15231550
That was not entirely true...
@@ -1526,13 +1553,15 @@ That was not entirely true...
15261553
become: true
15271554
pre_tasks:
15281555
- name: ...
1556+
roles:
1557+
- myrole
15291558
tasks:
15301559
- name: ...
15311560
post_tasks:
15321561
- name: ...
15331562
```
15341563

1535-
Handlers are triggered after pre_tasks, tasks, post_tasks...
1564+
Handlers are triggered after pre_tasks, roles, tasks, post_tasks...
15361565
<!-- .slide: class="master-content" > -->
15371566
***
15381567
## Error Handling
@@ -1551,7 +1580,7 @@ Continue even when task failed:
15511580
## Error-Handling
15521581
Define failed from output of command:
15531582

1554-
```yaml
1583+
```yaml [1-5|5]
15551584
- name: See if my service is running
15561585
ansible.builtin.command:
15571586
cmd: systemctl status my_service
@@ -1571,11 +1600,12 @@ https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html
15711600
always:
15721601
# do this always
15731602
```
1603+
--> kind of rollback mechanism
15741604
<!-- .slide: class="master-content" > -->
15751605
***
15761606
## Bonus Level: Blocks!
15771607

1578-
```yaml
1608+
```yaml [1-16|2-7|8-11|12-16]
15791609
- name: Demonstrating error handling in blocks
15801610
block:
15811611
- name: I force a failure
@@ -1597,21 +1627,44 @@ https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html
15971627
***
15981628
## Bonus Level: Blocks!
15991629
Blocks can come in handy to group `when` clauses:
1600-
```yaml
1630+
```yaml [1-7|2|2,3]
16011631
- name: Grouping two debugs together
1602-
when: 'web' in groups <- 'when'-statement before 'block' for better readability
1632+
when: 'web' in groups # 'when'-statement before 'block' for better readability
16031633
block:
16041634
- ansible.builtin.debug:
16051635
msg: 'this task...'
16061636
- ansible.builtin.debug:
16071637
msg: '...and this task will run if the host is in the web group'
1638+
```
1639+
<!-- .slide: class="master-content" > -->
16081640

1641+
***
1642+
## Bonus Level: Blocks!
1643+
```yaml
1644+
- name: loops over blocks don't work...
1645+
block:
1646+
- name: ...
1647+
loop:
1648+
- gentoo
1649+
- ubuntu
16091650
```
16101651

1611-
But: You cannot loop over a `block`.
1652+
Use loops with `ansible.builtin.include_tasks` instead!
1653+
1654+
<!-- .slide: class="master-content" > -->
1655+
1656+
***
1657+
## Bonus Level: Blocks!
1658+
```yaml
1659+
- name: loop over include_tasks
1660+
ansible.builtin.include_tasks: taskfile.yml
1661+
loop:
1662+
- gentoo
1663+
- ubuntu
1664+
```
16121665

1613-
Use loops with `ansible.builtin.include_tasks` instead
16141666
<!-- .slide: class="master-content" > -->
1667+
16151668
***
16161669
# Lab 5.1: Ansible Roles - Handlers and Blocks
16171670

0 commit comments

Comments
 (0)