Skip to content

Commit 957ea21

Browse files
add first draw of lab 1
1 parent 10c834a commit 957ea21

File tree

1 file changed

+19
-271
lines changed

1 file changed

+19
-271
lines changed

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

Lines changed: 19 additions & 271 deletions
Original file line numberDiff line numberDiff line change
@@ -4,294 +4,42 @@ weight: 121
44
sectionnumber: 12
55
---
66

7-
In this lab we are going to learn how to use Event Driven Ansible.
8-
For the following tasks, server `node1` and `node2` act as webservers. You can use Lab 4.0 as a guideline.
7+
In this lab we are going to learn how Ansible uses modules and where to find information about developing a custom module.
98

109
### Task 1
1110

12-
* Point your webbrowser to the official documentation of `ansible-rulebook`.
13-
* Install and configure everything needed to run ansible-rulebook and source plugins.
14-
* Check the version of `ansible-rulebook`
11+
* In which language has a module to be written?
12+
* Point your webbrowser to the official documentation about developing modules.
13+
* Point your webbrowser to the official documentation about developing modules best practices.
14+
* Point your webbrowser to the official documentation about developing collections.
15+
* Point your webbrowser to the official documentation about debugging your module.
16+
* Which environment variable lets you keep the remote module files after executing the module (instead of deleting it)?
1517

1618
{{% details title="Solution Task 1" %}}
1719

18-
[https://ansible-rulebook.readthedocs.io/en/stable/index.html](https://ansible-rulebook.readthedocs.io/en/stable/index.html)
20+
* either Pyhon (Linux) or Powershell (Windows)
21+
* [https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html)
22+
* [https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_best_practices.html#developing-modules-best-practices](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_best_practices.html#developing-modules-best-practices)
23+
* [https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html#developing-collections](https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html#developing-collections)
24+
* [https://docs.ansible.com/ansible/latest/dev_guide/debugging.html#debugging-modules](https://docs.ansible.com/ansible/latest/dev_guide/debugging.html#debugging-modules)
25+
* `ANSIBLE_KEEP_REMOTE_FILES`
1926

20-
Fedora 36+:
21-
```bash
22-
sudo dnf --assumeyes install java-17-openjdk python3-pip
23-
export JAVA_HOME=/usr/lib/jvm/jre-17-openjdk
24-
pip install ansible ansible-rulebook
25-
ansible-galaxy collection install ansible.eda
26-
```
27-
28-
Enterprise Linux 9:
29-
```bash
30-
sudo dnf install java-17-openjdk
31-
export JAVA_HOME=/usr/lib/jvm/jre-17-openjdk
32-
sudo dnf install python3-pip
33-
python3 -m venv ~/python
34-
. ~/python/bin/activate
35-
pip install --upgrade pip
36-
pip install ansible ansible-rulebook
37-
38-
ansible-galaxy collection install ansible.eda
39-
40-
sudo dnf install systemd-devel gcc python3-devel
41-
42-
pip install -r ~/.ansible/collections/ansible_collections/ansible/eda/requirements.txt
43-
```
44-
45-
```bash
46-
ansible-rulebook --version
47-
```
48-
Output on EL9:
49-
```bash
50-
version__ = '1.0.0'
51-
Executable location = /home/ansible/python/bin/ansible-rulebook
52-
Drools_jpy version = 0.3.4
53-
Java home = /usr/lib/jvm/java-17-openjdk-17.0.7.0.7-3.el9.x86_64
54-
Java version = 17.0.7
55-
Python version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]
56-
```
5727
{{% /details %}}
5828

5929
### Task 2
6030

61-
* Write a playbook `webserver.yml` that installs the servers in group `web` as webservers. See Lab 4.0 for guidelines.
62-
* Ensure that the playbook also sets a webpage at `/var/www/html/index.html`.
63-
* Ensure that the inventory file `hosts` in the folder inventory has the group `web` with `node1` and `node2` as members.
64-
* Run the playbook `webserver.yml` and check that the webservers are up and running.
31+
* for possible content of the `my_module.py` file, see the [official documentation](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#creating-a-module)
32+
*
6533

6634
{{% details title="Solution Task 2" %}}
6735

6836
```bash
69-
cat webserver.yml
70-
```
71-
```bash
72-
---
73-
- hosts: web
74-
become: true
75-
tasks:
76-
- name: install httpd
77-
ansible.builtin.dnf:
78-
name:
79-
- httpd
80-
- firewalld
81-
state: installed
82-
- name: start and enable httpd
83-
ansible.builtin.systemd_service:
84-
name: httpd
85-
state: started
86-
enabled: true
87-
- name: put default webpage
88-
ansible.builtin.copy:
89-
content: "Ansible Labs by Puzzle ITC"
90-
dest: /var/www/html/index.html
91-
owner: root
92-
group: root
93-
mode: "0644"
94-
- name: start and enable firewalld
95-
ansible.builtin.systemd_service:
96-
name: firewalld
97-
state: started
98-
enabled: true
99-
- name: open firewall for http
100-
ansible.posix.firewalld:
101-
service: http
102-
state: enabled
103-
permanent: true
104-
immediate: true
105-
```
106-
```bash
107-
cat inventory/hosts
108-
```
109-
```bash
110-
[controller]
111-
control0 ansible_host=<ip-of-control0>
112-
113-
[web]
114-
node1 ansible_host=<ip-of-node1>
115-
node2 ansible_host=<ip-of-node2>
116-
```
117-
```bash
118-
ansible-playbook -i inventory/hosts webserver.yml
119-
sudo dnf install -y lynx
120-
lynx http://<ip-of-node1>
121-
lynx http://<ip-of-node2>
122-
```
123-
{{% /details %}}
124-
125-
### Task 3
126-
127-
* Write a rulebook `webserver_rulebook.yml` that checks if the webpages on `node1` and `node2` are up and running.
128-
* If the webpages are not available anymore, the `webserver.yml` playbook should be re-run.
129-
* Use `url_check` from the `ansible.eda` collection as the source plugin in your rulebook.
130-
* Check the availability of the websites every 8 seconds.
131-
132-
{{% alert title="Note" color="primary" %}}
133-
If you don't have the `ansible.eda` collection installed yet,
134-
`ansible-rulebook` would start, but fail because the `url_check` source plugin cannot be found.
135-
{{% /alert %}}
136-
137-
{{% details title="Solution Task 3" %}}
138-
```bash
139-
cat webserver_rulebook.yml
140-
```
141-
```bash
142-
---
143-
- name: rebuild webservers if site down
144-
hosts: web
145-
sources:
146-
- name: check webserver
147-
ansible.eda.url_check:
148-
urls:
149-
- http://<ip-of-node1>:80/
150-
- http://<ip-of-node2>:80/
151-
delay: 8
152-
rules:
153-
- name: check if site down and rebuild
154-
condition: event.url_check.status == "down"
155-
action:
156-
run_playbook:
157-
name: webserver.yml
158-
```
159-
{{% /details %}}
160-
161-
### Task 4
162-
163-
* Start `webserver_rulebook.yml` in verbose mode.
164-
* Stop the httpd service on `node1` with ansible from another terminal on `control0`
165-
and see how the playbook `webserver.yml` is re-run.
166-
(You could also just stop the service directly on `node1`.)
167-
168-
{{% details title="Solution Task 4" %}}
169-
```bash
170-
ansible-rulebook --rulebook webserver_rulebook.yml -i inventory/hosts --verbose
171-
172-
ansible node1 -i inventory/hosts -b -m ansible.builtin.systemd_service -a "name=httpd state=stopped"
173-
```
174-
{{% /details %}}
175-
176-
177-
### Task 5
178-
179-
* Write the rulebook `webhook_rulebook.yml` that opens a webhook on port 5000 of the control node `control0`.
180-
* The rulebook should re-run the playbook `webserver.yml`
181-
if the webhook receives a message matching exactly the string "webservers down".
182-
* Use `webhook` from the `ansible.eda` collection as the source plugin in your rulebook.
183-
184-
{{% details title="Solution Task 5" %}}
185-
```bash
186-
cat webhook_rulebook.yml
187-
```
188-
```yaml
189-
---
190-
- name: rebuild webserver if webhook receives message that matches rule condition
191-
hosts: web
192-
sources:
193-
- name: start webhook and listen for messages
194-
ansible.eda.webhook:
195-
host: 0.0.0.0
196-
port: 5000
197-
rules:
198-
- name: rebuild webserver if monitoring tool sends alert
199-
condition: event.payload.message == "webservers down"
200-
action:
201-
run_playbook:
202-
name: webserver.yml
203-
```
204-
{{% /details %}}
205-
206-
### Task 6
207-
208-
* Run the rulebook `webhook_rulebook.yml` in verbose mode.
209-
* Send the string "webservers running" to the webhook.
210-
* You can do this by issuing:
211-
`curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers running\"}" 127.0.0.1:5000/endpoint`
212-
* See how the message is received, processed, but no actions are taken since the message doesn't match the condition defined.
213-
* Now send the message "webservers down" to the webhook. See how the playbook `webserver.yml` is run.
214-
215-
{{% details title="Solution Task 6" %}}
216-
```bash
217-
ansible-rulebook --rulebook webhook_rulebook.yml -i inventory/hosts --verbose
218-
```
219-
```bash
220-
curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers running\"}" 127.0.0.1:5000/endpoint
221-
```
222-
```bash
223-
curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers down\"}" 127.0.0.1:5000/endpoint
37+
$ mkdir /home/ansible/techlab/library/
38+
$ touch /home/ansible/techlab/library/my_module.py
22439
```
22540
{{% /details %}}
22641

227-
### Task 7
228-
229-
* Write the rulebook `complex_rulebook.yml`. It has to meet the following requirements:
230-
* It should check for three things:
231-
* check if the website on one of the two webservers is down. (Same as Task 3 above)
232-
* check if the message matches exactly the string "webservers down" (Same as Task 5 above)
233-
* check if the message contains the string "ERROR" or "error"
234-
* If one of the criteria above are met, do two things:
235-
1. run the ansible shell module to print the string "WEBSERVER ISSUES, REMEDIATION IN PROGRESS."
236-
into the journald log. (Use the command `systemd-cat echo "WEBSERVER ISSUES, REMEDIATION IN PROGRESS."`)
237-
2. run playbook `webservers.yml`
238-
* Start the rulebook `complex_rulebook.yml` and send the message "webservers down" to the webhook again.
239-
240-
{{% details title="Solution Task 7" %}}
241-
242-
```bash
243-
cat complex_rulebook.yml
244-
```
245-
```bash
246-
---
247-
- name: rebuild webserver if webhook receives message that matches rule condition
248-
hosts: web
249-
sources:
250-
- name: check webserver
251-
ansible.eda.url_check:
252-
urls:
253-
- http://<ip-of-node1>:80/
254-
- http://<ip-of-node2>:80/
255-
delay: 8
256-
- name: start webhook and listen for messages
257-
ansible.eda.webhook:
258-
host: 0.0.0.0
259-
port: 5000
260-
rules:
261-
- name: rebuild webserver if any source reports an alert
262-
condition:
263-
any:
264-
- event.url_check.status == "down"
265-
- event.payload.message == "webservers down"
266-
- event.payload.message is search("ERROR",ignorecase=true)
267-
actions:
268-
- run_module:
269-
name: ansible.builtin.shell
270-
module_args:
271-
cmd: "systemd-cat echo \"WEBSERVER ISSUES, REMEDIATION IN PROGRESS.\""
272-
- run_playbook:
273-
name: webserver.yml
274-
```
275-
276-
```bash
277-
ansible-rulebook --rulebook complex_rulebook.yml -i inventory/hosts --verbose
278-
```
279-
```bash
280-
curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers down\"}" 127.0.0.1:5000/endpoint
281-
```
282-
Note, that you would have to open port 5000 on the firewall if the curl command is not sent from the controller itself.
283-
{{% /details %}}
284-
285-
### Task 8
286-
287-
* What source plugins are available in the `ansible.eda` collection?
288-
[Search the content of event-driven-ansible on GitHub.com](https://github.com/ansible/event-driven-ansible).
289-
290-
{{% details title="Solution Task 8" %}}
291-
[Event Driven Ansible on GitHub](https://github.com/ansible/event-driven-ansible/tree/main/extensions/eda/plugins/event_source)
292-
{{% /details %}}
293-
29442
### All done?
29543

296-
* [Ansible-rulebook documentation](https://ansible-rulebook.readthedocs.io/en/stable/)
297-
* [AnsibleAutomates YouTube channel for more examples](https://www.youtube.com/@AnsibleAutomation/videos)
44+
* [Ansible module development on YouTube](https://www.youtube.com/results?search_query=ansible+module+development)
45+
* Have a look at the [Puzzle OPNsense Ansible Collection](https://github.com/puzzle/puzzle.opnsense)

0 commit comments

Comments
 (0)