Thank you everyone for coming.
If you were expecting the Postgres talk, that was the one before, so
you might need to watch the video stream.
So, Ansible best practices,
I thought about calling it "Ansible, my best practices",
so, just warning ahead, this is things I stumbled on using Ansible
for the last 2-3 years and
those are very specific things I found that worked very well for me.
About me, I do also freelance work, do a lot of Ansible in there,
I'm also the Debian maintainer for Ansible with Harlan Lieberman-Berg
If there are any bugs in the package, just report them.
The talk will be roughly divided into 4 parts.
The first part will be about why you actually want to use config management
and why you specifically want to use Ansible.
So, if you're still SSHing into machines and editing config files,
you're probably a good candidate for using Ansible.
Then, the second part will be about good role and playbook patterns
that I have found that work really well for me.
The third chapter will be about typical antipatterns I've stumbled upon,
either in my work with other people using Ansible,
or the IRC support channel, for example.
The fourth part will be like advanced tips and tricks you can use
like fun things you can do with Ansible.
Quick elevator pitch, what makes config management good?
It actually also serves as a documentation of changes on your servers over time
so if you just put the whole config management in a git repo
and just regularly commit,
you will actually be able to say
"Why doesn't this work? It used to work a year ago"
You can actually check why.
Also, most config management tools have a lot better error reporting than
your self-written bash scripts that do whatever.
And usually, you have a very good reproducibility with config management
and also idempotency, meaning that if you run, for example, a playbook several times
you will always get the same result.
Also, it's great if you work in small team or you admin ??? in the company
and you have some people working on a few things too.
It makes team work a lot easier and you will save a lot of time actually
debugging things when things break.
What makes Ansible good?
Comparing it to Chef or Puppet for example it's really easy to set up,
you start with two config files, you have it installed and you're ready to go.
It's also agentless, so whatever machines you actually want to control,
the only thing you they really need to have is an SSH daemon and Python 2.6+
so that's virtually any Debian machine you have installed and
that ???
Ansible also supports configuration of many things like
networking equipment or even Windows machines,
they don't need SSH but they use the WinRM
but Ansible came a bit late to the game so Ansible's still not as good
in coverage like for example Puppet,
which literally, you can configure any machine on the planet with that,
as long as it has a CPU.
Next step, I will talk about good role patterns.
If you've never worked with Ansible before,
this is the point when you watch the video stream,
that you pause it and start working a few weeks with it
and then unpause the actual video.
A good role should ideally have the following layout.
So, in the "roles" directory, you have the name of the role and task/main.yml
You have the following rough layout.
At the beginning of the role, you check for various conditions,
for example using the "assert" task to for example check that
certain variables are defined, things are set,
that it's maybe part of a group, things like that you actually want to check.
Then, usually, you install packages, you can use apt on CentOS machines,
yum, or you can do a git checkout or whatever,
then usually you do some templating of files where you have certain abstraction
and the variables are actually put into the template and
make the actual config file.
There's also good to point out that the template module actually has
a "validate" parameter,
that means you can actually use a command to check your config files for syntax errors
and if that fails, your playbook will fail before actually deploying that config file
so you can for example use Apache with the right parameters to actually do
a check on the syntax of the file.
That way, you never end up with a state where there's a broken config.
In the end, you usually…
When you change things, you trigger handlers to restart any ???
If you use variables, I recommend putting sensible defaults in
defaults/main.yml
and then you only have to override those variables on specific cases.
Ideally, you should have sensible defaults you want to have to get whatever things
you want to have running.
When you start working with it and do that a bit more,
you notice a few things and that is
your role should ideally run in "check mode".
"ansible-playbook" has --check that basically is just a dry run of
your complete playbook
and with --diff, it will actually show you for example file changes,
or file mode changes, stuff like that
and won't actually change anything.
So if you end up editing a lot of stuff, you can use that as a check.
I'll later get to some antipatterns that actually break that thing.
And, ideally, the way you change files and configs and states,
you should make sure that when the actual changes are deployed,
and you run it a second time,
that Ansible doesn't report any changes
because if you end up writing your roles fairly sloppy, you end up having
a lot of changes and then,
in the end of the report, you have like 20 changes reported and
you kind of then know those 18, they're always there
and you kind of miss the 2 that are important, that actually broke your system
If you want to do it really well, you make sure that it doesn't report any changes
when you run it twice in a row.
Also, a thing to consider is you can define variables in the "defaults" folder
and also in the "vars" folder,
but if you look up how variables get inherited, you'll notice that
the "vars" folder is really hard to actually override,
so you want to avoid that as much as possible.