Skip to content

Commit 463bd4a

Browse files
authored
Rita edits ch 11
1 parent e8abda8 commit 463bd4a

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

chapter_11_ansible.asciidoc

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[[chapter_11_ansible]]
2-
== Infrastructure As Code: Automated Deployments With Ansbile
2+
== Infrastructure As Code: Automated Deployments With Ansible
33

44
[quote, 'Cay Horstman']
55
______________________________________________________________
66
Automate, automate, automate.
77
______________________________________________________________
88

9+
// RITA: In this intro, please mention that you'll be using Ansible. Not only will it prep the reader, but it'll set you up to say "(or "become" in Ansible terminology)" in the User Accounts, SSH, and Privileges section before we actually get to the Ansible section.
910
((("deployment", "automating with Ansible", id="Dfarbric11")))
1011
((("infrastructure as code")))
1112
In this chapter we're going to spin up an actual server,
@@ -27,12 +28,14 @@ we want to make sure that it's as similar as possible to the production environm
2728
By automating the way we deploy, and using the same automation for staging and prod,
2829
we give ourselves much more confidence.
2930

30-
The buzzword for automating your deployments these days is "Infrastructure as Code".
31+
The buzzword for automating your deployments these days is "Infrastructure as Code" (IaC).
32+
// RITA: OK to add the acronym IaC? It's common enough.
3133

34+
// RITA: Perhaps move this to the It Worksss section so the reader sees it when their site actually goes live?
3235
NOTE: Why not ping me a note once your site is live on the web,
3336
and send me the URL?
3437
It always gives me a warm and fuzzy feeling...
35-
38+
Email me at [email protected].
3639

3740

3841

@@ -43,7 +46,7 @@ I'm making big changes to the deployment chapters.
4346
This chapter is still very fresh, but the content is all there,
4447
so you should be able to follow along.
4548
46-
But as always I really, really need fedback.
49+
But as always I really, really need feedback.
4750
So please hit me up at [email protected], or via
4851
https://github.com/hjwp/Book-TDD-Web-Dev-Python/issues[GitHub Issues]
4952
and Pull Requests.
@@ -122,7 +125,7 @@ from all the grizzled dinosaurs out there.
122125

123126
==== Spinning Up a Server
124127

125-
I'm not going to dictate how you do this--whether
128+
I'm not going to dictate how you spin up a server--whether
126129
you choose Amazon AWS, Rackspace, Digital Ocean, your own server in a data centre,
127130
or a Raspberry Pi in a cupboard under the stairs,
128131
any solution should be fine, as long as:
@@ -150,21 +153,21 @@ NOTE: Some people get to this chapter, and are tempted to skip the domain bit,
150153
Don't do this.
151154
It's _not_ the same, and you'll have more difficulty following the instructions,
152155
which are complicated enough as it is.
153-
If you're worried about cost, have a look at the link above for free options.
156+
If you're worried about cost, have a look at the guide I wrote for free options.
154157
((("getting help")))
155-
158+
// RITA: In general, please avoid saying above/below, as the final layout of the book will be determined later during Production. So, what's above now may end up on the previous page. It's better to reword it.
156159

157160
==== User Accounts, SSH, and Privileges
158161

159-
In these instructions, I'm assuming that you have a nonroot user account set up,
162+
In the following instructions, I'm assuming that you have a nonroot user account set up,
160163
and that it has "sudo" privileges,
161164
so whenever we need to do something that requires root access, we use sudo,
162165
(or "become" in Ansible terminology),
163166
and I'm explicit about that in the various instructions that follow.
164167

165168
My user is called "elspeth", but you can call yours whatever you like!
166-
Just remember to substitute it in all the places I've hardcoded it below.
167-
See the guide linked above if you need tips on creating a sudo user.
169+
Just remember to substitute it in all the places I've hardcoded it.
170+
See the guide I wrote if you need tips on creating a sudo user.
168171

169172

170173
.Security
@@ -258,10 +261,10 @@ rather than specifying a procedural series of steps to be followed one by one.
258261

259262
==== Installing Ansible
260263

261-
Take a look at the instructions here:
262-
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
264+
Take a look at the https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html [documentation] for instructions on how to install Ansible/
265+
// RITA: Please anchor all URLs to descriptive text.
263266

264-
The simplest thing to do is to install them into the virtualenv
267+
The simplest thing to do is to install Ansible into the virtualenv
265268
on our local machine:
266269

267270
[subs="specialcharacters,quotes"]
@@ -329,14 +332,14 @@ for.
329332

330333
<3> Each module then provides a bunch of parameters which control how it works.
331334
Here we specify the `name` of the package we want to install ("docker")
332-
and tell it update its cache first, which is required on a fresh server.
335+
and tell it to update its cache first, which is required on a fresh server.
333336

334337
Most Ansible modules have pretty good documentation,
335338
check out the `builtin.apt` one for example.
336339
I often skip to the
337340
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html#examples[Examples section].
338341

339-
342+
// RITA: Please add a sentence or two to introduce the following code block.
340343
[subs="specialcharacters,quotes"]
341344
----
342345
$ *ansible-playbook --user=elspeth -i staging.ottg.co.uk, infra/ansible-provision.yaml -vv*
@@ -393,7 +396,7 @@ Ansible scripts are particularly satisfying in this regard.
393396
=== SSHing Into the Server and Viewing Container Logs
394397

395398
Time to get into some good old-fashioned sysadmin!
396-
Let's SSH in to our server and see if we can see any evidence that our container has run.
399+
Let's SSH into our server and see if we can see any evidence that our container has run.
397400

398401
We use `docker ps -a` to view all containers, including old/stopped ones,
399402
and we can use `docker logs` to view the output from one of them:
@@ -443,10 +446,11 @@ and organisations will often run private ones,
443446
hosted by cloud providers like AWS.
444447

445448
So your process of getting an image onto a server is usually
449+
446450
* Push the image from your machine to the registry
447451
* Pull the image from the registry onto the server.
448452
Usually this step is implicit,
449-
in that you just specifying the image name in the format registry-url/image-name:tag,
453+
in that you just specify the image name in the format registry-url/image-name:tag,
450454
and then `docker run` takes care of pulling down the image for you.
451455

452456
But I don't want to ask you to create a DockerHub account,
@@ -510,6 +514,7 @@ In Ansible config, it looks like this:
510514
<3> And we use `docker_image` again but this time with `load_path` and `source: load`
511515
to import the image back on the server
512516

517+
// RITA: Please add a sentence or two to introduce the following code block.
513518
[subs="specialcharacters,quotes"]
514519
----
515520
$ *ansible-playbook --user=elspeth -i staging.ottg.co.uk, infra/ansible-provision.yaml -vv*
@@ -592,7 +597,7 @@ This means we don't have a dependency on having run `docker build` locally.
592597
with compatibility between Apple's new ARM-based chips and our server's
593598
x86/amd64 architecture.
594599
You could also use this `platform:` to cross-build docker images
595-
for a rasbperry pi from a regular PC, or vice-versa.
600+
for a Rasberry Pi from a regular PC, or vice-versa.
596601
It does no harm in any case.
597602

598603

@@ -626,7 +631,7 @@ KeyError: 'DJANGO_SECRET_KEY'
626631
[2024-02-26 22:19:15 +0000] [1] [ERROR] Reason: Worker failed to boot.
627632
----
628633

629-
Ah woops, we need to set those environment variables on the server too.
634+
Whoops, we need to set those environment variables on the server too.
630635

631636

632637
=== Using an env File to Store Our Environment Variables
@@ -636,8 +641,8 @@ But we don't want to hard-code secrets like SECRET_KEY into our Ansible files
636641
and commit them to our repo!
637642

638643
Instead, we can use Ansible to automate the creation of a secret key,
639-
and then save it to a file on the server, where it _ill be _relatively_ secure
640-
(better than saving it to version control and pushing it to GitHub in any case!)
644+
and then save it to a file on the server, where it _will_ be _relatively_ secure
645+
(which is better than saving it to version control and pushing it to GitHub in any case!).
641646

642647
We can use a so-called "env file" to store environment variables.
643648
Env files are essentially a list of key-value pairs using shell syntax,
@@ -652,7 +657,7 @@ So, just as we inject variables into our html templates in Django,
652657
we can use a templating language called "jinja2" to have variables in our env file.
653658
It's a common tool in Ansible scripts, and the syntax is very similar to Django's.
654659

655-
Here's what our template for the env file will looks like:
660+
Here's what our template for the env file will look like:
656661

657662
[role="sourcecode"]
658663
.infra/env.j2 (ch11l004)
@@ -704,7 +709,7 @@ And here's how we use it in the provisioning script:
704709
<3> The `vars` section defines the variables we'll inject into our template.
705710

706711
<4> We actually use a built-in Ansible variable called `inventory_hostname`.
707-
This variable woul actually be available in the template already,
712+
This variable would actually be available in the template already,
708713
but I'm renaming it for clarity.
709714

710715
<5> This `lookup('password')` thing I copy-pasted from Stackoverflow.
@@ -721,20 +726,20 @@ NOTE: Using an env file to store secrets is definitely better than committing
721726
.Idempotency and Declarative Configuration
722727
*******************************************************************************
723728
724-
Infrastucture-as-code tools like Ansible aim to be "declarative",
729+
Infrastructure-as-code tools like Ansible aim to be "declarative",
725730
meaning that, as much as possible, you specify the desired state that you want,
726731
rather than specifying a series of steps to get there.
727732
728733
This concept goes along with the idea of "idempotency",
729734
which is wanting to get the same result when you run something for the first time,
730-
vs running it again on later occations.
735+
vs running it again on later occasions.
731736
732737
An example is the `apt` module that we used to install docker.
733738
It doesn't crash if docker is already installed, and in fact,
734739
Ansible is smart enough to check first before trying to install anything.
735740
736741
There is some subtlety here, for example, our templated env file
737-
will only be writen once, so the step is idempotent in the sense
742+
will only be written once, so the step is idempotent in the sense
738743
that it doesn't overwrite the file with a new random secret key every time you run it.
739744
But that does come with the downside that you can't easily add new variables to the file.
740745
@@ -874,7 +879,7 @@ on the server:
874879
----
875880
====
876881

877-
That gets us to
882+
That gets us to:
878883

879884
----
880885
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
@@ -885,7 +890,7 @@ element: [id="id_list_table"]; [...]
885890
=== Mounting the database on the server and running migrations
886891

887892
Taking a look at the logs from the server,
888-
we can see that the database is not initialised.
893+
we can see that the database is not initialised:
889894

890895
[subs="specialcharacters,quotes"]
891896
----
@@ -913,7 +918,7 @@ staging.ottg.co.uk : ok=9 changed=2 unreachable=0 failed=0
913918
skipped=0 rescued=0 ignored=0
914919
----
915920

916-
921+
// RITA: Please expand this intro sentence. Here's how to do what?
917922
Here's how
918923

919924
[role="sourcecode"]
@@ -962,6 +967,8 @@ Here's how
962967

963968
=== It workssss
964969

970+
// RITA: I'd prefer to call the section "It works!" with an exclamation point or two, but OK. Please expand the first sentence of the section to be more than just "Hooray." What was achieved? What does the following result indicate?
971+
965972
Hooray
966973

967974
[role="small-code"]
@@ -1003,7 +1010,7 @@ Docker into the mix, should things not go according to plan:
10031010
This is a good place to go check on environment variables,
10041011
port mappings, and exactly which image was running, for example.
10051012
1006-
- And you can inspect the image with
1013+
- You can inspect the image with
10071014
`docker image inspect superlists`.
10081015
You might need this to check the exact image hash,
10091016
to make sure it's the same one you built locally.
@@ -1095,8 +1102,8 @@ $ *git log --graph --oneline --decorate*
10951102
10961103
////
10971104

1098-
1099-
Anyway, you now have a live website! Tell all your friends!
1105+
// RITA: Perhaps add the note about the reader emailing you when their site goes live to this point. "Tell your mum! Tell me! Email me at....""
1106+
You now have a live website! Tell all your friends!
11001107
Tell your mum, if no one else is interested!
11011108
And, in the next chapter, it's back to coding again.((("", startref="Fstage11")))
11021109

@@ -1107,7 +1114,7 @@ And, in the next chapter, it's back to coding again.((("", startref="Fstage11"))
11071114
((("automated deployment", "additional resources")))
11081115
There's no such thing as the One True Way in deployment;
11091116
I've tried to set you off on a reasonably sane path,
1110-
but there's plenty of things you could do differently,
1117+
but there are plenty of things you could do differently,
11111118
and lots, lots more to learn besides.
11121119
Here are some resources I used for inspiration:
11131120

0 commit comments

Comments
 (0)