Real World Ansible - Common Role Walkthrough

This video is available to view for members only.

Click here to Join!

Already a member?


In this video we look at how we can use Ansible to install and configure all the 'common' software packages and settings that we want every single time we build or provision a new server.

For me, this is one of the best features of Ansible.

Sure, we can deploy all kinds of cool software using a combination of Ansible Galaxy and our playbooks, but having a really nice base to work from - that is until now - would have meant building a standard master image with all our common software pre-installed.

Now, reading that you may very well be thinking: what could building our server this way offer that simply using / cloning a standard master does not?

It's a good question.

We also seem to be cloning from a master image here, so how are these two setups different anyway?

Let's explore the separate processes and look at how the two situations work out.

Cloning a Pre-Built Master Image

As software developers we are taught from very early in our careers that we should keep things DRY (Don't Repeat Yourself).

A prudent move therefore, should we be in a situation where we frequently need new base server images to be provisioned, would be to install our Ubuntu Server (as an example, but it could be any other OS), patch it up to date, install all the common things we need - git, ssh, fail2ban, etc - and then shut that machine down.

We might have added in some standard user accounts, but more often than not, we would keep this machine as generic as possible.

We call that machine our Master, Gold Image, or some other fancy term.

We never use that machine directly.

Instead, every time we need it, we take a clone of that image and then customise the clone to our needs.

If we discover some time later that we should have also installed a backup agent, added in another user account, or set up a specific user group then bad news, we not only need to edit our Master image, but also our clones.

What started out as a seemingly good idea has quite quickly become a maintainence headache.

Building Using Ansible

Remember that Master image that I just slagged off? Well, we still need it. Arf.

But not quite.

We could get away with building a brand new Ubuntu Server VM (or physical, if you're so inclined / rich enough) every time we need a new build. But why bother, if cloning only takes a few seconds compared to >10 mins to build the same image we already have.

Next, we configure an Ansible Playbook to install and configure all the common tools, user accounts, settings, and anything else we need.

There is effort involved in this - one time to set it up (skip that bit if you wish by using mine), then a few tweaks in the future as you refine / improve your process.

Need to add in another user?

No problem, tweak the group_vars, host_vars, or defaults/main.yml - or wherever else you have your User config - and run your playbook against your entire infrastructure. Bosh. All done. No headaches.

Same goes for backup agents, user groups, additional software you forget... anything!

The rigidity is gone.

Get Started

This video assumes you have been following along with the tutorial series.

If you have, watching this should hold no surprises. Either clone my repo or make your own and get started creating your perfect server config.

If you haven't watched any other videos in this series then I hope watching this opens your eyes to the power of Ansible.

The best thing is that whilst this just installs the basics, this can be the base of every other machine you build. Want a RabbitMQ server? Awesome, grab a RabbitMQ role from Ansible Galaxy and you're good to go. But even better, run your common role first and you also have a RabbitMQ with all your settings, users, groups, security tweaks... the works. Perfect, every time.

Ansible. Is. Freaking. Awesome!

Code For This Course

Get the code for this course.

Code For This Episode

Get the code for this episode.

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.

Episodes in this series

# Title Duration
1 How To Install Ubuntu Server in Oracle VirtualBox 10:21
2 How to Rename our Ubuntu Server 02:00
3 Installing Ansible on Ubuntu Server 00:33
4 Safety First, Safety Second - Snapshots are like Ctrl+Z 00:11
5 Managing the Ansible Inventory Hosts File 02:16
6 Ansible Ad Hoc Commands 04:27
7 Introduction to Ansible Playbooks 02:14
8 Ansible Handlers 01:38
9 Ansible Variables 03:16
10 Git Your Deploy Just Right 05:34
11 Ansible Roles 05:51
12 Looping in Ansible with_items 04:34
13 Ansible Files For Beginners 06:14
14 Variable Precedence - Where To Put Your Role Vars? 04:13
15 Ansible Templates 05:51
16 Ansible Inventory With Our Own Hosts Files 06:57
17 How to Manage Users with Ansible 08:32
18 Ansible Vault Tutorial 03:48
19 Ansible Galaxy Tutorial 10:03
20 Real World Ansible - Common Role Walkthrough 06:19
21 Ansible MySQL Tutorial 13:44
22 Ansible Symfony and nginx 09:37