Skip to content

Commit 196f1e8

Browse files
committed
Continues work on session material for day 27
1 parent 1d81ab8 commit 196f1e8

1 file changed

Lines changed: 54 additions & 154 deletions

File tree

2024/day27.md

Lines changed: 54 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -15,185 +15,85 @@ This presentation will give a broad overview of Ansible and its architecture and
1515

1616
Participants will get first-hand insights into Ansible, its strengths, weaknesses, and the potential of event-driven automation within the DevOps world.
1717

18-
## Demos
19-
20-
<details>
21-
22-
<summary>Prerequisites</summary>
23-
24-
### Ansible Inventory
25-
2618
> [!NOTE]
27-
> For this inventory file to work, you need to create VMs accordingly and adjust the IP addresses to fit your lab environment.
28-
29-
Ansible utilizes so-called inventories to manage a list of hosts and groups of hosts. Below is the inventory for the demo environment used in this presentation.
30-
31-
```yaml
32-
hosts:
33-
webservers:
34-
hosts:
35-
webshop.example.com: # Ubuntu
36-
ansible_host: 192.168.1.10
37-
webserver: apache2
38-
company.example.com: # Ubuntu
39-
ansible_host: 192.168.1.11
40-
webserver: nginx
41-
internal.example.com: # CentOS Stream
42-
ansible_host: 192.168.1.12
43-
webserver: httpd
44-
```
45-
46-
You can copy-paste this inventory into a file called `hosts.yml` and use it for the following demos.
47-
48-
</details>
19+
> The below content is a copy of the [lab repository's] README for convenience.
4920
50-
<details>
51-
52-
<summary>Lab 1: Ansible Basics</summary>
53-
54-
### Demo 1: Ansible Basics
55-
56-
#### Ansible from the CLI via `ansible`
57-
58-
The first example installs a webserver on all hosts in the `webservers` group. The installed webserver is defined as a **host variable** in the inventory file `hosts.yml` (*see above*).
59-
60-
```console
61-
ansible \
62-
webservers \
63-
-i hosts.yml \
64-
-m package \
65-
-a 'name="{{ webserver }}"'
66-
```
67-
68-
#### Ansible from the CLI via `ansible-playbook`
69-
70-
The second example utilizes the following **playbook** to **install** and **start** the defined webserver on all hosts in the `webservers` group.
71-
72-
```yaml
7321
---
74-
- name: Install webservers
75-
hosts: webservers
76-
vars:
77-
package: "{{ webserver }}"
78-
become: true
79-
tasks:
80-
- name: Install webserver
81-
ansible.builtin.package:
82-
name: "{{ package }}"
83-
state: present
84-
85-
- name: Start webserver
86-
ansible.builtin.service:
87-
name: "{{ package }}"
88-
state: started
89-
```
90-
91-
Save this playbook as `playbook.yml` and run it with the following command.
9222

93-
```console
94-
ansible-playbook \
95-
-i hosts.yml \
96-
playbook.yml
97-
```
23+
# Event-Driven Ansible Lab
9824

99-
You will see a separated output for each task in the playbook. In the end, you should be able to access the webserver on each host in the `webservers` group.
25+
This is a lab designed to demonstrate Ansible and how Event-Driven Ansible (**EDA**) builds on top of its capabilities.
10026

101-
> [!TIP]
102-
> Ansible is **idempotent** - try running the playbook again and see how the output differs.
27+
The setup is done with Ansible, too. It will install **Ansible, EDA, Prometheus**, and **Alertmanager** on a VM to demonstrate some of the capabilities of EDA.
10328

104-
</details>
29+
## Prerequisites
10530

106-
<details>
31+
To follow along with this lab in its entirety, you will need four VMs:
10732

108-
<summary>Lab 2: Event-driven Ansible and Generic Webhooks</summary>
109-
110-
### Demo 2: Event-driven Ansible and Generic Webhooks
33+
> [!NOTE]
34+
> If you want to skip Ansible basics and go straight to EDA, you'll need just the `eda-controller.example.com` VM and can skip the others.
11135
112-
#### Prerequisites
36+
| VM name | OS |
37+
|--------------------|-------------|
38+
| eda-controller.example.com | CentOS/Rocky 8.9 |
39+
| company.example.com | CentOS/Rocky 8.9 |
40+
| internal.example.com | Ubuntu 22.04 |
41+
| webshop.example.com | OpenSUSE 15.5 |
11342

114-
For this demo, we will use `localhost` as the target host. Therefore, we need to adjust our inventory file `hosts.yml` accordingly:
43+
**You'll need to be able to SSH to each of these VMs as root using SSH keys.**
11544

116-
```yaml
117-
hosts:
118-
localhost: {}
45+
## Lab Setup
11946

120-
The first demo of event-driven Ansible shows how to use a generic webhook to trigger a playbook run. Copy the following rulebook into a file called `rulebook.yml`.
47+
### Clone the repository and create a Python virtual environment
12148

122-
```yaml
123-
- name: Listen to webhook events
124-
hosts: all
125-
sources:
126-
- ansible.eda.webhook:
127-
host: 0.0.0.0
128-
port: 5000
129-
rules:
130-
- name: Debug event output
131-
condition: event.payload.greeting is defined
132-
action:
133-
debug:
134-
msg: "Hello {{ event.payload.greeting }}!"
135-
136-
- name: Greet stranger
137-
condition: 1 == 1 # default case
138-
action:
139-
debug:
140-
msg: Hello World!
49+
```bash
50+
git clone https://github.com/mocdaniel/lab-event-driven-ansible.git
51+
cd lab-event-driven-ansible
52+
python3 -m venv .venv
53+
source .venv/bin/activate
14154
```
14255

143-
#### Start the EDA server
144-
145-
To start the EDA server, run the following command.
56+
### Install Ansible and other dependencies
14657

147-
```console
148-
ansible-rulebook \
149-
-i hosts.yml \
150-
--rulebook rulebook.yml
58+
```bash
59+
pip install -r requirements.txt
15160
```
15261

153-
#### Trigger the webhook
62+
### Create the inventory file
15463

155-
Once the EDA server is running, we can open a second terminal session and double-check that it is listening on the correct port:
156-
157-
```console
158-
netstat -lntup | grep 5000
159-
```
160-
161-
Now, we can trigger the webhook from our second terminal session using `curl`, first with empty input:
162-
163-
```console
164-
curl \
165-
-H "Content-Type: application/json" \
166-
-d '{}' \
167-
http://localhost:5000/endpoint
64+
```yaml
65+
---
66+
# hosts.yml
67+
webservers:
68+
hosts:
69+
webshop.example.com:
70+
ansible_host: <ip-address>
71+
webserver: nginx
72+
company.example.com:
73+
ansible_host: <ip-address>
74+
webserver: httpd
75+
internal.example.com:
76+
ansible_host: <ip-address>
77+
webserver: apache2
78+
eda_controller:
79+
hosts:
80+
eda-controller.example.com:
81+
ansible_host: <ip-address>
16882
```
16983
170-
If we switch over to the first terminal session, we should see the output of the second rule, which is the default case:
171-
172-
```console
173-
Hello World!
84+
### Install Needed Roles and Collections
85+
86+
```bash
87+
ansible-galaxy install -r requirements.yml
17488
```
17589

176-
Now, we can trigger the webhook again, this time with a payload:
90+
### Run the Setup Playbook
17791

178-
```console
179-
curl \
180-
-H "Content-Type: application/json" \
181-
-d '{"greeting": "Daniel"}' \
182-
http://localhost:5000/endpoint
183-
```
184-
185-
If we switch over to the first terminal session again, we should see the output of the first rule, which is the case for a defined `greeting` in the payload:
92+
After you created the inventory file and filled in the IP addresses, you can run the setup playbook:
18693

187-
```console
188-
Hello Daniel!
94+
```bash
95+
ansible-playbook playbooks/setup.yml
18996
```
19097

191-
</details>
192-
193-
## Resources
194-
195-
- [Ansible Documentation](https://docs.ansible.com/)
196-
- [Installing Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-and-upgrading-ansible)
197-
- [Ansible Galaxy](https://galaxy.ansible.com/)
198-
- [EDA Documentation](https://ansible.readthedocs.io/projects/rulebook/en/stable/introduction.html)
199-
- [Installing and Running EDA](https://ansible.readthedocs.io/projects/rulebook/en/stable/installation.html)
98+
> [!CAUTION]
99+
> Due to a known bug with Python on MacOS, you need to run `export NO_PROXY="*"` on MacOS before running the playbook

0 commit comments

Comments
 (0)