1
1
[[chapter_11_ansible]]
2
- == Infrastructure As Code with Ansbile
2
+ == Infrastructure As Code: Automated Deployments With Ansbile
3
3
4
4
[quote, 'Cay Horstman']
5
5
______________________________________________________________
@@ -17,13 +17,17 @@ but a key insight of the modern infrastructure management
17
17
is that automation really pays off in reducing maintenance burdens.
18
18
19
19
It's also key to making sure our tests give us true confidence over our deployments.
20
- If we go to the trouble of builiding a staging server,
20
+ If we go to the trouble of building a staging server,footnote:[
21
+ What I'm calling a "staging" server, some people would
22
+ call a "development" server, and some others would also like to distinguish
23
+ "preproduction" servers. Whatever we call it, the point is to have
24
+ somewhere we can try our code out in an environment that's as similar as
25
+ possible to the real production server.]
21
26
we want to make sure that it's as similar as possible to the production environment.
22
27
By automating the way we deploy, and using the same automation for staging and prod,
23
28
we give ourselves much more confidence.
24
29
25
- The buzzword for automating your deployments these days is
26
- "Infrastructure as Code".
30
+ The buzzword for automating your deployments these days is "Infrastructure as Code".
27
31
28
32
NOTE: Why not ping me a note once your site is live on the web,
29
33
and send me the URL?
@@ -101,7 +105,6 @@ but they broadly fall into two camps:
101
105
offering like Heroku or my old employers, PythonAnywhere.
102
106
103
107
104
-
105
108
((("PythonAnywhere")))
106
109
Particularly for small sites, a PaaS offers a lot of advantages,
107
110
and I would definitely recommend looking into them.
@@ -113,9 +116,10 @@ doesn't necessarily tell you about the others.
113
116
Any one of them might radically change their process or business model by the time you get to read this book.
114
117
115
118
Instead, we'll learn just a tiny bit of good old-fashioned server admin,
116
- including SSH and manual server config. They're unlikely to ever go away, and
117
- knowing a bit about them will get you some respect from all the grizzled
118
- dinosaurs out there.
119
+ including SSH and manual server config.
120
+ They're unlikely to ever go away,
121
+ and knowing a bit about them will get you some respect
122
+ from all the grizzled dinosaurs out there.
119
123
120
124
121
125
@@ -146,9 +150,9 @@ https://github.com/hjwp/Book-TDD-Web-Dev-Python/blob/master/server-quickstart.md
146
150
147
151
NOTE: Some people get to this chapter, and are tempted to skip the domain bit,
148
152
and the "getting a real server" bit, and just use a VM on their own PC.
149
- Don't do this. It's 'not' the same, and you'll have more difficulty
150
- following the instructions, which are complicated enough as it is. If
151
- you're worried about cost, have a look at the link above for free options.
153
+ Don't do this. It's _not_ the same, and you'll have more difficulty
154
+ following the instructions, which are complicated enough as it is.
155
+ If you're worried about cost, have a look at the link above for free options.
152
156
((("getting help")))
153
157
154
158
@@ -165,12 +169,12 @@ Just remember to substitute it in all the places I've hardcoded it below.
165
169
See the guide linked above if you need tips on creating a sudo user.
166
170
167
171
168
-
169
172
.General Server Debugging Tips
170
173
*******************************************************************************
171
174
172
- The most important lesson to remember from this chapter is to work
173
- incrementally, make one change at a time, and run your tests frequently.
175
+ The most important lesson to remember from this chapter is,
176
+ as always but more than ever, to work incrementally,
177
+ make one change at a time, and run your tests frequently.
174
178
175
179
When things (inevitably) go wrong, resist the temptation to flail about
176
180
and make other unrelated changes in the hope that things will start working again;
@@ -183,11 +187,7 @@ It's just as easy to fall into the Refactoring-Cat trap on the server!
183
187
184
188
185
189
186
-
187
-
188
-
189
- Configuring Domains for Staging and Live
190
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
190
+ === Configuring Domains for Staging and Live
191
191
192
192
We don't want to be messing about with IP addresses all the time,
193
193
so we should point our staging and live domains to the server.
@@ -213,16 +213,10 @@ so once you've set up your A-record,
213
213
you can check its progress on a "propagation checking" service like this one:
214
214
https://www.whatsmydns.net/#A/superlists-staging.ottg.eu.
215
215
216
- ////
217
216
I'm planning to host my staging server at 'superlists-staging.ottg.eu':
218
217
219
218
220
- NOTE: A clarification: in this chapter, we run tests 'against' our staging
221
- server, not 'on' our staging server. So we still run the tests from our
222
- own laptop, but they target the site that's running on the server.
223
- ////
224
-
225
- === A first cut of an ansible script
219
+ === A first Cut of an Ansible Script
226
220
227
221
Infrastructure-as-code tools, also called "configuration management" tools,
228
222
come in lots of shapes and sizes.
@@ -231,8 +225,8 @@ and you'll probably come across Terraform,
231
225
which is particularly strong on managing cloud services like AWS.
232
226
233
227
We're going to use Ansible, because it's relatively popular,
234
- because I'm biased that it happens to be written in Python,
235
228
because it can do everything we need it to,
229
+ because I'm biased that it happens to be written in Python,
236
230
and because it's probably the one I'm personally most familiar with.
237
231
238
232
Another tool could probably have worked just as well!
@@ -243,7 +237,16 @@ rather than specifying a procedural series of steps to be followed one by one.
243
237
244
238
245
239
Let's dip our toes into ansible,
246
- and see if we can get it to run a simple "hello world" container on our server:
240
+ and see if we can get it to run a simple "hello world" container on our server.
241
+
242
+ Here's what's called a "playbook" in ansible terminology.
243
+ It's in a format called YAML (Yet Another Markup Language),
244
+ which, if you've never come across before,
245
+ you will soon develop a love-hatefootnote:[
246
+ The "love" part is that yaml is very easy to _read_ and scan through at a glance.
247
+ The "hate" part is that the actual syntax is surprisingly fiddly to get right:
248
+ the difference between lists and key/value maps is subtle and I can never quite remember it honestly.]
249
+ relationship with.
247
250
248
251
249
252
[role="sourcecode"]
@@ -255,9 +258,9 @@ and see if we can get it to run a simple "hello world" container on our server:
255
258
- hosts: all
256
259
257
260
tasks:
258
- - name: Install podman
259
- ansible.builtin.apt:
260
- name: podman
261
+ - name: Install podman <1>
262
+ ansible.builtin.apt: <2>
263
+ name: podman <3>
261
264
update_cache: yes
262
265
become: true
263
266
@@ -270,12 +273,31 @@ and see if we can get it to run a simple "hello world" container on our server:
270
273
----
271
274
====
272
275
276
+ <1> An ansible playbook is a series of "tasks"
277
+ (so in that sense it's still quite sequential and procedural),
278
+ but the individual tasks themselves are quite declarative.
279
+ Each one usually has a human-readable `name` attribute.
280
+
281
+ <2> Each tasks uses an ansible "module" to do its work.
282
+ This one uses the `builtin.apt` module which provides
283
+ a wrapper around the `apt` Debian & Ubuntu package management tool.
284
+
285
+ <3> Each module then provides a bunch of parameters which control
286
+ how it works. Here we specify the `name` of the package we want to install ("docker")
287
+ and tell it update its cache first, which is required on a fresh server.
288
+
289
+ Most ansible modules have pretty good documentation,
290
+ check out the `builtin.apt` one for example.
291
+ I often skip to the https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html#examples[Examples section].
292
+
273
293
[subs="specialcharacters,quotes"]
294
+
274
295
----
275
296
$ *ansible-playbook --user=elspeth -i 192.168.56.10, infra/ansible-provision.yaml -vv*
276
297
----
277
298
278
- TODO show ansible output.
299
+ TODO: show ansible output.
300
+ TODO: stop using local ip
279
301
280
302
281
303
=== SSHing Into the Server and Viewing Container Logs
@@ -479,7 +501,7 @@ Ansible and the podman plugins have some modules for this:
479
501
containers.podman.podman_generate_systemd:
480
502
name: superlists
481
503
dest: ~/.config/systemd/user/
482
-
504
+
483
505
- name: Container must be started and enabled on systemd
484
506
ansible.builtin.systemd:
485
507
name: container-superlists
0 commit comments