Skip to content

Commit

Permalink
Merge branch 'psc_rulebook'
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlypenguin committed Aug 17, 2023
2 parents 829e192 + b76c671 commit 4b5a9a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
47 changes: 32 additions & 15 deletions content/en/docs/11/01/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ In this lab we will have a closer look at events and facts.

### Task 1

* Copy the rulebook from Task 11.3. to a new one with the name `debug_event_rulebook.yml`.
* Copy the rulebook from Lab 11 Task 3 to a new one with the name `debug_event_rulebook.yml`.
* Substitute the `run_playbook` action with a `debug` action.
* That debug action should print out all information from the event.
* Run the rulebook in verbose mode and look at the part of the output generated by the debug action.
* Stop the httpd service on node1.
* Run the rulebook in verbose mode. The debug action should show all information about the event.

{{% details title="Solution Task 1" %}}
```bash
cat debug_event_rulebook.ym`
cat debug_event_rulebook.yml
```
```bash
---
Expand All @@ -29,13 +30,15 @@ cat debug_event_rulebook.ym`
- http://<ip-of-node2>:80/
delay: 8
rules:
- name: check if site down and rebuild
- name: check if site down and debug
condition: event.url_check.status == "down"
action:
debug:
var: event
```
```bash
ansible node1 -i inventory/hosts -b -m service -a "name=httpd state=stopped"
```
```bash
ansible-rulebook --rulebook debug_event_rulebook.yml -i inventory/hosts -vv
```
Expand All @@ -52,9 +55,12 @@ ansible-rulebook --rulebook debug_event_rulebook.yml -i inventory/hosts -vv
### Task 2
* Rewrite the rulebook `debug_event_rulebook.yml`:
* Use `run_playbook` action to start a playbook named `sos.yml`
* Use a `run_playbook` action to start a playbook named `sos.yml`
* The playbook `sos.yml` should create an unattended sos report labeled with the fully qualified collection name of the source plugin used. Be sure to install the appropriate packages so that the sos report can be created.
* The name of the source plugin should be taken from the json output as a variable.
* The creation of the sos report takes quite some time.
* Ensure that the condition is throttled to run the action once within 5 minutes at most.
* The delay of the source check should stay at 8 seconds.
* Run the rulebook `debug_event_rulebook.yml` and ensure the sos reports on the webservers have the needed label.
Expand All @@ -63,12 +69,14 @@ There are good onlinetools to convert [one-line json to multiline json](https://
{{% /alert %}}
{{% details title="Solution Task 2" %}}
See the documentation on how to [throttle event storms](https://ansible.readthedocs.io/projects/rulebook/en/stable/conditions.html#throttle-actions-to-counter-event-storms-reactive).
```bash
cat debug_event_rulebook.yml
```
```bash
---
- name: show event json if site down
- name: run sos playbook if site down
hosts: web
sources:
- name: check webserver
Expand All @@ -80,6 +88,10 @@ cat debug_event_rulebook.yml
rules:
- name: check if site down and rebuild
condition: event.url_check.status == "down"
throttle:
once_within: 5 minutes
group_by_attributes:
- event.meta.source.type
action:
run_playbook:
name: sos.yml
Expand All @@ -100,15 +112,18 @@ cat sos.yml
state: installed

- name: create a sos report unattended containing no sensitive information
ansible.builtin.command: "sos report --clean --batch --label {{ ansible_eda.event.meta.source.type }}"
ansible.builtin.command: |
"sos report --clean --batch --label {{ ansible_eda.event.meta.source.type }}"
```
```bash
ansible-rulebook --rulebook debug_event_rulebook.yml -i inventory/hosts -vv
```
```bash
...
2023-06-27 11:45:17,300 - ansible_rulebook.builtin - INFO - Calling Ansible runner
2023-06-28 11:15:53,766 - ansible_rulebook.builtin - INFO - ruleset: show event \
json if site down, rule: check if site down and rebuild
2023-06-28 11:15:53,766 - ansible_rulebook.builtin - INFO - Calling Ansible runner

PLAY [web] *********************************************************************

Expand All @@ -119,13 +134,15 @@ TASK [install sos package] *****************************************************
ok: [node1]

TASK [create a sos report unattended containing no sensitive information] ******
ok: [node1] => {
"msg": "sos report --clean --batch --label ansible.eda.url_check"
}
changed: [node1]
...
PLAY RECAP *********************************************************************
node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2023-06-27 11:45:20,712 - ansible_rulebook.builtin - DEBUG - Cancel Queue reading task
node1 : ok=3 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
2023-06-28 11:17:59,741 - ansible_rulebook.builtin - INFO - Ansible Runner \
Queue task cancelled
2023-06-28 11:17:59,742 - ansible_rulebook.builtin - INFO - Playbook rc: 0, \
status: successful
...
```
{{% /details %}}
Expand Down
36 changes: 21 additions & 15 deletions content/en/docs/11/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ sectionnumber: 11

In this lab we are going to learn how to use Event Driven Ansible. For the following tasks, server `node1` and `node2` act as webservers. You can use Lab 4.0 as a guideline.

{{% alert title="Note" color="primary" %}}
Note, that as of June 2023, EDA is still in a developer preview state. Documentation and all content is work in progress!
The installation of `ansible-rulebook` and the `ansible.eda` collection works fine on newer Fedora Systems as well as RHEL / Rocky Linux 9. At present times, you could have a harder time on other operating systems. Be warned...
{{% /alert %}}

### Task 1

* Point your webbrowser to the official documentation of `ansible-rulebook`.
* Install and configure everything needed to run ansible-rulebook and source plugins.
* Check version of 'ansible-rulebook'
* Check the version of `ansible-rulebook`

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

Expand Down Expand Up @@ -64,6 +59,7 @@ Python version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red
### Task 2

* Write a playbook `webserver.yml` that installs the servers in group `web` as webservers. See Lab 4.0 for guidelines.
* Ensure that the playbook also sets a webpage at `/var/www/html/index.html`.
* Ensure that the inventory file `hosts` in the folder inventory has the group `web` with `node1` and `node2` as members.
* Run the playbook `webserver.yml` and check that the webservers are up and running.

Expand All @@ -88,6 +84,12 @@ cat webserver.yml
name: httpd
state: started
enabled: yes
- name: put default webpage
ansible.builtin.copy:
content: "Ansible Labs by Puzzle ITC"
dest: /var/www/html/index.html
owner: root
group: root
- name: start and enable firewalld
ansible.builtin.service:
name: firewalld
Expand All @@ -113,7 +115,7 @@ node2 ansible_host=<ip-of-node2>
```
```bash
ansible-playbook -i inventory/hosts webserver.yml
dnf install -y lynx
sudo dnf install -y lynx
lynx http://<ip-of-node1>
lynx http://<ip-of-node2>
```
Expand All @@ -132,7 +134,9 @@ If you don't have the `ansible.eda` collection installed yet, `ansible-rulebook`

{{% details title="Solution Task 3" %}}
```bash
$ cat webserver_rulebook.yml
cat webserver_rulebook.yml
```
```bash
---
- name: rebuild webservers if site down
hosts: web
Expand Down Expand Up @@ -174,7 +178,9 @@ ansible node1 -i inventory/hosts -b -m service -a "name=httpd state=stopped"
{{% details title="Solution Task 5" %}}
```bash
$ cat webhook_rulebook.yml
cat webhook_rulebook.yml
```
```yaml
---
- name: rebuild webserver if webhook receives message that matches rule condition
hosts: web
Expand Down Expand Up @@ -220,9 +226,9 @@ curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers down\"}"
* check if the message matches exactly the string "webservers down" (Same as Task 5 above)
* check if the message contains the string "ERROR"
* If one of the criterias above are met, do two things:
1. run the ansible shell module to print the string "WEBSERVER ISSUES, REMEDIATION IN PROGRESS." into the journald log. (The command to do so is "systemd-cat echo "WEBSERVER ISSUES, REMEDIATION IN PROGRESS.")
1. run the ansible shell module to print the string "WEBSERVER ISSUES, REMEDIATION IN PROGRESS." into the journald log. (Use the command `systemd-cat echo "WEBSERVER ISSUES, REMEDIATION IN PROGRESS."`)
2. run playbook `webservers.yml`
* Start the rulebook `complex_rulebook.yml` and do the same test as in Task 4 and Task 6.
* Start the rulebook `complex_rulebook.yml` and send the message "webservers down" to the webhook again.
{{% details title="Solution Task 7" %}}
Expand Down Expand Up @@ -266,14 +272,14 @@ ansible-rulebook --rulebook complex_rulebook.yml -i inventory/hosts --verbose
```bash
curl -H 'Content-Type: application/json' -d "{\"message\": \"webservers down\"}" 127.0.0.1:5000/endpoint
```
Note, that you would have to open port 5000 on the firewall if the curl command is not send from the controller itself.
Note, that you would have to open port 5000 on the firewall if the curl command is not sent from the controller itself.
{{% /details %}}
### Task 9
### Task 8
* What source plugins are available in the `ansible.eda` collection?
* What source plugins are available in the `ansible.eda` collection? [Search the content of event-driven-ansible on github.com](https://github.com/ansible/event-driven-ansible).
{{% details title="Solution Task 10" %}}
{{% details title="Solution Task 8" %}}
[Event Driven Ansible on Github](https://github.com/ansible/event-driven-ansible/tree/main/extensions/eda/plugins/event_source)
{{% /details %}}
Expand Down

0 comments on commit 4b5a9a8

Please sign in to comment.