Skip to content

Commit 3e0d451

Browse files
kenken64claude
andcommitted
Add Ansible and Docker Scout as separate workshops
- Extract Ansible content from workshop7 as Workshop #5 - Extract Docker Scout content from workshop7 as Workshop #9 - Update README table with new workshop entries Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent df45338 commit 3e0d451

3 files changed

Lines changed: 591 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ https://forms.gle/zUSeKCGSsXRyWvSR8
88
| S-DOEA - Workshop - Github Workshop | [Workshop #1](./workshop/workshop2.md) |
99
| S-DOEA - Workshop - Jenkins Installation | [Workshop #2](./workshop/workshop1.md) |
1010
| S-DOEA - Workshop - Containers and Container Management Workshop | [Workshop #3](./workshop/workshop5.md) |
11-
| S-DOEA - Workshop - Terraform and Ansible (IAC) | [Workshop #5](./workshop/workshop3-1.md) |
11+
| S-DOEA - Workshop - Infrastructure as Code - Terraform | [Workshop #4](./workshop/workshop3-1.md) |
12+
| S-DOEA - Workshop - Infrastructure as Code - Ansible | [Workshop #5](./workshop/workshop-ansible.md) |
1213
| S-DOEA - Workshop - DevOps in the Cloud | [Workshop #6](./workshop/workshop6.md) |
1314
| S-DOEA - Workshop - End to end DevOps Engineering and Automation | [Workshop #7](./workshop/workshop7.md) |
1415
| S-DOEA - Workshop - Lint, SAST, DAST Workshop | [Workshop #8](./workshop/workshop9.md) |
16+
| S-DOEA - Workshop - Container Security - Docker Scout | [Workshop #9](./workshop/workshop-docker-scout.md) |
1517

1618
<table>
1719
<tr>

workshop/workshop-ansible.md

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
# S-DOEA - Workshop 5 - Infrastructure as Code - Ansible
2+
3+
4+
## Ansible - Part 1
5+
6+
7+
The objective of this workshop is to automate the installation of Code-Server on a server
8+
9+
### Workshop
10+
Provision a Ubuntu server for this exercise. You can use Terraform or manually
11+
provision an instance on DigitalOcean's console.
12+
Once you have provisioned, note the IP address, root user and SSH keys used.
13+
Use these information to create an inventory file, inventory.yaml.
14+
Write a playbook that will use the inventory.yaml file to configure the
15+
server. The playbook should perform the following tasks
16+
17+
• Update the /lib/systemd/system/code-server.service file
18+
with the code server password; change the following line
19+
20+
21+
```
22+
Environment=PASSWORD=__PLACEHOLDER__
23+
```
24+
with the password, assuming that the password is mypassword
25+
26+
```
27+
Environment=PASSWORD="mypassword"
28+
```
29+
30+
• Update the /etc/nginx/sites-available/code-server.conf
31+
file with the domain code-<ipv4_address>.nip.io; change the line
32+
with server_name to
33+
34+
```
35+
server_name code-<ipv4_address>.nip.io;
36+
37+
```
38+
• Use systemd module to restart nginx and code-server services. You
39+
must also perform a daemon reload viz. set daemon_reload to yes.
40+
41+
42+
### Test
43+
Test your deployment by browsing to http://```<ip-address>```
44+
45+
### Submission
46+
When you have completed this workshop, commit your work to the repository.
47+
The instructor will clone your repository at the end
48+
49+
### Setup
50+
51+
1. Access your Digital Ocean account.
52+
53+
54+
2. Create a Ubuntu Droplet
55+
56+
<br>
57+
<img style="float: center;" src="./screens/ansible11.png">
58+
<br>
59+
60+
* Select Singapore as region
61+
* Select Ubuntu as the server Image v20.04 x64
62+
63+
<br>
64+
<img style="float: center;" src="./screens/ansible12.png">
65+
<br>
66+
67+
* Select cost saving server type (6 USD)
68+
69+
<br>
70+
<img style="float: center;" src="./screens/ansible13.png">
71+
<br>
72+
73+
* Choose the SSH authentication method and generate a fresh SSH key pair. Click the "New SSH Key" button, then follow the instructions provided on the right-hand side. Paste the contents of the "cat" command into the Digital Ocean text area.
74+
75+
<br>
76+
<img style="float: center;" src="./screens/ansible14.png">
77+
<br>
78+
79+
* Finalize the droplet
80+
81+
<br>
82+
<img style="float: center;" src="./screens/ansible15.png">
83+
<br>
84+
85+
3. Access the newly created ubuntu server
86+
87+
```
88+
ssh root@<public ip address>
89+
```
90+
91+
<br>
92+
<img style="float: center;" src="./screens/ansible16.png">
93+
<br>
94+
95+
4. Generate the PKI key pair on the logon server
96+
97+
```
98+
ssh-keygen
99+
```
100+
101+
<br>
102+
<img style="float: center;" src="./screens/ansible17.png">
103+
<br>
104+
105+
5. Add the public key content to the Digital Ocean account security section, name it as www-1
106+
107+
<br>
108+
<img style="float: center;" src="./screens/ansible18.png">
109+
<br>
110+
111+
<br>
112+
<img style="float: center;" src="./screens/ansible19.png">
113+
<br>
114+
<br>
115+
<img style="float: center;" src="./screens/Screenshot from 2024-04-18 09-12-57.png">
116+
<br>
117+
118+
6. Install terraform IAC tool on the ubuntu server
119+
120+
```
121+
sudo apt update
122+
123+
```
124+
125+
```
126+
sudo apt install snap
127+
```
128+
129+
```
130+
sudo snap install terraform --classic
131+
```
132+
133+
134+
7. Check the terraform version
135+
136+
```
137+
terraform --version
138+
```
139+
<br>
140+
<img style="float: center;" src="./screens/ansible22.png">
141+
<br>
142+
143+
8. Install Ansible on the Ubuntu instance
144+
145+
```
146+
sudo apt-add-repository ppa:ansible/ansible
147+
148+
sudo apt update
149+
150+
sudo apt install ansible-core
151+
```
152+
153+
<br>
154+
<img style="float: center;" src="./screens/Screenshot from 2023-12-11 14-18-17.png">
155+
<br>
156+
157+
9. Check the ansible version
158+
159+
```
160+
ansible --version
161+
```
162+
163+
### Implementation
164+
a. Create a directory called workshop02 in your course repository.
165+
166+
b. Read Step 1 and Step 2 of the following blog ( do not need to run any command in this page, FYI)
167+
<https://www.digitalocean.com/community/tutorials/how-to-set-up-the-code-server-cloud-ide-platform-on-ubuntu-20-04.html>
168+
169+
c. Change directory into the workshop02 folder
170+
171+
d. Create an ansible template file for server configuration. code-server.conf.j2
172+
173+
```
174+
server {
175+
listen 80;
176+
listen [::]:80;
177+
178+
server_name {{codeserver_domain}} {{ansible_host}};
179+
180+
location / {
181+
proxy_pass http://127.0.0.1:8080/;
182+
proxy_set_header Upgrade $http_upgrade;
183+
proxy_set_header Connection upgrade;
184+
proxy_set_header Accept-Encoding gzip;
185+
}
186+
}
187+
```
188+
e. Create an ansible template file code-server.service.j2
189+
190+
```
191+
[Unit]
192+
Description=code-server
193+
After=nginx.service
194+
195+
[Service]
196+
Type=simple
197+
Environment=PASSWORD={{codeserver_password}}
198+
ExecStart=/usr/bin/code-server --bind-addr 127.0.0.1:8080 --user-data-dir /var/lib/code-server --auth password
199+
Restart=always
200+
201+
[Install]
202+
WantedBy=multi-user.target
203+
```
204+
f. Create a terraform template file inventory.yaml.tftpl
205+
206+
```
207+
all:
208+
vars:
209+
ansible_connection: ssh
210+
ansible_user: root
211+
ansible_ssh_private_key: ${ssh_private_key}
212+
hosts:
213+
codeserver:
214+
ansible_host: ${codeserver_ip}
215+
codeserver_domain: ${codeserver_domain}
216+
codeserver_password : ${codeserver_password}
217+
```
218+
219+
g. Create a provider terraform script provider.tf
220+
221+
```
222+
terraform {
223+
required_providers {
224+
digitalocean = {
225+
source = "digitalocean/digitalocean"
226+
version = "2.26.0"
227+
}
228+
local = {
229+
source = "hashicorp/local"
230+
version = "2.4.0"
231+
}
232+
}
233+
}
234+
235+
provider digitalocean {
236+
token = var.do_token
237+
}
238+
239+
```
240+
241+
h. Create a variables terraform script variables.tf
242+
243+
```
244+
variable do_token {
245+
type = string
246+
sensitive = true
247+
}
248+
249+
variable do_region {
250+
type = string
251+
default = "sgp1"
252+
}
253+
254+
variable do_image {
255+
type = string
256+
default = "ubuntu-22-04-x64"
257+
}
258+
259+
variable do_size {
260+
type = string
261+
default = "s-1vcpu-1gb"
262+
}
263+
264+
variable do_ssh_key {
265+
type = string
266+
default = "www-1"
267+
}
268+
269+
variable ssh_private_key {
270+
type = string
271+
}
272+
273+
variable codeserver_password {
274+
type = string
275+
}
276+
277+
```
278+
279+
i. Create a resources terraform script resources.tf
280+
281+
```
282+
#ssh key
283+
data "digitalocean_ssh_key" "www-1" {
284+
name = var.do_ssh_key
285+
}
286+
287+
resource "digitalocean_droplet" "codeserver" {
288+
name = "codeserver"
289+
image = var.do_image
290+
region = var.do_region
291+
size = var.do_size
292+
293+
ssh_keys = [ data.digitalocean_ssh_key.www-1.id ]
294+
}
295+
296+
resource "local_file" "root_at_codeserver" {
297+
filename = "root@${digitalocean_droplet.codeserver.ipv4_address}"
298+
content = ""
299+
file_permission = "0444"
300+
}
301+
302+
resource "local_file" "inventory" {
303+
filename = "inventory.yaml"
304+
content = templatefile("inventory.yaml.tftpl",{
305+
codeserver_ip = digitalocean_droplet.codeserver.ipv4_address
306+
ssh_private_key = var.ssh_private_key
307+
codeserver_domain = "code-server-${digitalocean_droplet.codeserver.ipv4_address}.nip.io"
308+
codeserver_password = var.codeserver_password
309+
})
310+
file_permission = "0444"
311+
}
312+
313+
output codeserver_ip {
314+
value = digitalocean_droplet.codeserver.ipv4_address
315+
}
316+
317+
```
318+
319+
Remeber to generate the Digital Ocean Personal Access Token and export to the environment variable
320+
321+
```
322+
export DO_PAT=<your DO PAT>
323+
```
324+
325+
```
326+
terraform init
327+
```
328+
329+
```
330+
terraform plan -var "do_token=${DO_PAT}" -var "ssh_private_key=/root/.ssh/id_rsa" -var "codeserver_password=password123456"
331+
```
332+
333+
```
334+
terraform apply -auto-approve -var "do_token=${DO_PAT}" -var "ssh_private_key=/root/.ssh/id_rsa" -var "codeserver_password=password123456"
335+
```
336+
337+
Get the playbook.yml from the solution github repository
338+
339+
```
340+
ansible-playbook playbook.yaml -i inventory.yaml
341+
```
342+
343+
## Solution Repository URL
344+
345+
https://github.com/kenken64/aipc-jun2023/tree/main/workshop02

0 commit comments

Comments
 (0)