Automating Server Configuration with Ansible
local graph
Chapter 15 — Automating Server Configuration with Ansible
Section titled “Chapter 15 — Automating Server Configuration with Ansible”Nowadays, it’s not uncommon to have hundreds of servers that make up your organization’s infrastructure. As our user base grows, we’re able to scale our environment to meet the demands of our customers. As we scale our resources and add additional servers, the amount of time we spend configuring them and setting them up increases considerably. The time spent setting up new servers can be a major burden—especially if we need to create hundreds of servers within a small window of time. As workload demands increase, we need to have a solution in place to manage our infrastructure and quickly deploy new resources with as small a workload as possible. In this chapter, we explore the concept of configuration management along with automated deployments. This sure sounds complicated, but it’s not—you’ll be surprised how easy it is to automate your configuration.
In this chapter, we will cover:
- Understanding the need for configuration management
- Why Ansible?
- Creating a Git repository
- Getting started with Ansible
- Making your servers do your bidding
- Putting it all together – automating web server deployment
- Using Ansible’s pull method
In the introduction, I’ve already given you some examples of why you might want to consider building automation into your workflow and implement an effective solution in your environment. In the next section, we’ll explore the need for automation in more detail before we actually get started.
Understanding the need for configuration management
Section titled “Understanding the need for configuration management”When I first started working in the IT industry, it was a much different landscape than it is today. Servers were all physical, and any time you needed a new server, you literally needed to call a vendor and order one.
You waited for a week or two for the server to be built and sent to you. When it arrived, you installed it in a rack, set up an operating system, and then installed whatever applications you needed. You then tested the server for a while to make sure the combination of software, hardware, and drivers was stable and reliable. After some time, you’d deploy the new server into production.
Nowadays, it’s still the case that system administrators often need to purchase and install hardware, much like the process I mentioned in the previous paragraph. However, with virtual machines and containers, the physical hardware we install is commonly just a catalyst to host virtual resources. In the past, we had one physical server for each use case, which meant we needed to have very large server rooms. But in modern times, you may have a server with dozens of cores that are capable of running hundreds of virtual machines. But the problem of configuration remains—the process of setting up an operating system and applications is a very time-consuming endeavor.
As the landscape changed, the need for automation increased. Servers needed to be deployed quickly and efficiently. With the large number of servers in a typical data center, it became less and less practical to connect to each of them and configure them one by one every time a change was necessary. For example, when a security vulnerability hit the news, the typical administrator would need to manually install a patch on every server. This could take days or even weeks. That’s not very efficient.
To better deal with this issue, the concept of configuration management has become very popular. With configuration management, an administrator can write some sort of code (such as a script) and then use a utility to execute it across every server. Configuration management is also known as Infrastructure as Code (IaC) and basically lets the administrator define a set of guidelines for servers of various types and have them automatically be provisioned to meet those requirements. This automation saves a ton of work.
Configuration management also comes into play while provisioning a new server. Imagine defining some rules for a specific type of server, and having it come to life meeting those exact specifications. The applications you want it to have are installed during the provisioning process, configuration files are copied over, users are created, and firewall rules are put in place, all automatically as defined in your specification. Put even more simply, imagine setting up something like a web server with just a single command. No need to install Apache or do any of that manual work. You simply request a server, and the configuration management solution you have in place will take care of the rest.
IaC, which is basically a fancy term for configuration management, is essentially just the automated running of scripts on your servers. In this book, we’ve looked at automation already. In Chapter 6, Boosting Your Command-line Efficiency, we wrote a simple script that we could use to back up a server. That same mentality can be used for provisioning servers as well, by simply having a server run a script when it comes online. For existing servers, you can make a change once and apply that change to every server you manage, or even just a subset.
This is where configuration management utilities, such as Chef, Puppet, and others, come into play. Each of these solutions features a specific type of scripting language that is designed from the ground up to facilitate the provisioning of resources. With such utilities, there is typically some sort of program (or locally installed agent) that interprets the instructions from a central server and runs them on its clients. Each solution is relatively smart; it will determine what needs to be done and perform the steps.
If a requirement is met, the instruction is skipped. If a required resource is not present, it will be configured appropriately. One such configuration management solution is Ansible, which we will use in this chapter.
Why Ansible?
Section titled “Why Ansible?”In this chapter, I will show you how to set up Ansible, and then we will use it to automate some configuration tasks. By the end of this chapter, you’ll understand the basic concepts you can use to start the process of automating deployments in your organization. You may be wondering, then, why Ansible and not one of the other solutions, such as Chef or Puppet?
Some configuration management solutions are relatively heavy from a resource perspective. With other solutions, you’ll generally have a central server, which will run a master program. This program will periodically check in with each server under its control by communicating with the agent installed on each server. Then, the agent will receive instructions from the central server and carry them out.
This means that you’ll need to maintain a server with modest CPU and RAM requirements, and the agent on the client side of the communication will also spend valuable CPU in order to carry out the instructions. This resource utilization can be very heavy on both the primary and client servers.
Ansible is very different than the other solutions in that there is no agent at all. There is typically a server, but it’s not required to run any resource-intensive software. The entire configuration process happens via SSH, so you can even carry out the instructions from your workstation if you want to skip having to maintain a central server. Typically, the administrator will create a user account on each server, and then the central Ansible server (or workstation) will execute commands over SSH to update the configuration on each machine. Since there is no agent installed on each server, the process takes a lot less CPU. Of course, the instructions that Ansible gives your servers will definitely result in CPU usage, but certainly a lot less than the other solutions.
Ansible is typically set up by creating an inventory file, which contains a list of resources (servers) (in the form of hostnames or IP addresses that Ansible will be instructed to connect to and configure). If you want to add a new server, you simply make sure that a specific user account exists on that server, then you add it to the inventory list. If you want to remove it, you delete the line in the inventory file corresponding to that server. It’s very easy.
However, something that’s magical about Ansible is that you don’t even have to run a central server at all if you don’t want to. You can store your Ansible configuration in a Git repository, and have each server download code from the repository and run it locally. This means that if you do have a dynamic environment where servers come and go all the time (which is very common in cloud deployments), you don’t have to worry about maintaining an inventory file. Just instruct each server to download the code and provision themselves. This is known as the pull method of Ansible, which I will also show you.
While solutions such as Chef and Puppet have their merits and are definitely fun to use, I think you’ll find that Ansible scales better and gives you far more control over how these hosts are configured. While it’s up to you to figure out exactly how you want to implement Ansible, the creative freedom it gives you is second to none. I’ve been using Ansible for quite a while now, and I’m still finding new ways to use it. It’s a technology that will grow with you.
Creating a Git repository
Section titled “Creating a Git repository”For the examples in this chapter, it’s recommended that you create a Git repository to store your Ansible code. This isn’t required, as you can find other ways of hosting your code, but it’s highly recommended. This is especially true when we get to the pull method of Ansible at the end of this chapter. In this section, I’ll walk you through creating a repository. If you already know how to use GitHub, you can skip this section.
While a full walkthrough of Git is beyond the scope of this book, the basics are more than enough for following along here. When it comes to Git, you can simply install the git package on a server to have it host your code, but GitHub is probably the easiest way to get started. An added bonus is that GitHub is home to a lot of great projects you can benefit from, and browsing the code of these projects is a great way to become more accustomed to syntax rules with different scripting and programming languages. For our purposes, we’re going to use GitHub as a central place to store our Ansible code.
It probably goes without saying, but GitHub is a public resource. Any code you upload to the service will be available for all to see by default. Therefore, be mindful of the information you commit to your repository. Make sure you don’t include any personally identifiable information, passwords, blueprints, API keys, or anything else you don’t want the public to know about you or your organization. You can create a private repository to hide confidential information, but it’s still safer to not upload protected information at all (whether the repository is private or not).
To get started, create an account at https://www.github.com if you don’t already have one. This is a free process. Make sure you create a reasonably secure password here. After you have created an account, click on New repository, and then give it a name (simply calling it ansible is fine):

Figure 15.1: Creating an Ansible repository on GitHub
In the example screenshot, I created a repository that is Public, which means exactly that—anyone will be able to view the code. You can create Private repositories as well, if you prefer. Since we’re not going to include confidential information in the repository during the examples in this book, we don’t need to worry about that right now.
Once the repository is created using Create repository, we’ll need to download it locally. For that, we’ll need to install the git package:
sudo apt install gitNext, we should set up our local Git client so that we can fill out our name and email address, otherwise Git will most likely complain. To do this, we can issue the following commands, substituting the content in the quotes with your information:
git config user.email "[email protected]"git config user.name "John Doe"To download our repository, the following will do the trick (ignore the warning about the repository being empty if you see such a message):
git clone https://github.com/myusername/ansible.gitNow you have the Git repository downloaded locally. Right now, the repository doesn’t include anything useful. To create a file within the repository, you simply change your working directory to be inside the repository folder that was downloaded when you cloned it, and create whatever files you want inside. By default, Git doesn’t care about any files you create inside the repository until you add them. For example, we can create a test file and commit it to the repository with the following commands:
echo "this is a test" > testfile.txtgit add testfile.txtgit commit -m "initial commit"With these commands, we used echo to create a test file with some text in it. Then, we used the git add command to tell Git that we want the file to be a part of our repository. Finally, we finalized our changes by using git commit, along with the -m flag and a message about the commit. At this point, the changes only exist locally. To push our changes back to GitHub, we use the following command from inside the repository directory:
git push origin mainBy following the on-screen prompts (GitHub username and password), our changes will be placed inside our actual repository.
So, how does this help us when it comes to configuration management? Typically, the code that administrators use to provision servers will be placed inside a repository for safekeeping. If the local copy of the code is lost, it can simply be cloned again from the repository. GitHub is a safe place to put our code, since we can be reasonably sure that our code won’t disappear as the service is very stable (you still may want to create a local backup to be safe). Whether you’re using Ansible, Chef, Puppet, or another utility, it’s common practice to keep the code in a Git repository. With regard to Ansible, this will directly impact us in the last section of this chapter since we’ll be using the ansible-pull command, which expects a repository URL.
In practice, when we create Ansible playbooks, you should commit those changes back to the repository. I won’t specifically call on you to do that, so go ahead and keep that in mind as we go. When you create a new playbook, add it to the repository, then commit it. If you make changes to existing files, commit those changes. Be sure to use the git push command to push your changes back to the repository. For example, if you created a file inside the repository named myplaybook.yml, you would execute commands such as these:
git add myplaybook.ymlgit commit -m "insert message about the commit here"git push origin mainGo ahead and practice this a bit before you move on. Even if you don’t use Ansible in production, understanding the basics of Git is invaluable, as you’ll almost definitely need it at some point in your career.
Getting started with Ansible
Section titled “Getting started with Ansible”The first thing to know about Ansible is that it changes constantly. New versions with exciting features are released regularly, and it shows no sign of slowing down whatsoever. There is a lot of excitement around this technology, so it’s regularly improving.
The reason I’m bringing this up is that although the examples in this book have been written for (and tested on) Ubuntu 22.04, new versions of Ansible are released regularly outside of Ubuntu’s repositories, and not only include new features but syntax changes as well. For our needs in this book, the version of Ansible included in the repositories should be more than fine. However, if you were to look at examples of Ansible playbooks online, they might be written for a newer version (or even an older version). If for any reason you have an issue with a particular example written for Ansible, a good first step in terms of troubleshooting is to compare the version of Ansible the tutorial was written for against the version installed. When it comes to Ubuntu 22.04, the version of Ansible that’s made available is 2.10.x. You can install a newer version of Ansible from the official website, but for the purposes of this book, we’ll use the version that’s in the default repositories.
Go ahead and install ansible via apt:
sudo apt install ansibleNow you should have the ansible-playbook command available, which is the main binary that we’ll be using as we explore Ansible. There are other commands that Ansible provides us, but we’re not concerned with them.
In order to follow along with the remainder of this chapter, it’s recommended that you have at least two servers to work with; the more the better. If you have a Virtual Machine (VM) solution such as VirtualBox available, simply create additional VMs. To save time, consider cloning an existing VM a few times (just make sure you don’t overload your computer/server by over-allocating resources).
The most common workflow of Ansible works something like this: you have a main server or workstation, on which Ansible is installed. While you don’t need an agent on the clients, they will, however, need OpenSSH installed and configured as that’s how Ansible communicates. To make things easy, it’s recommended to have a dedicated Ansible user on each machine, and the Ansible user on the server should be able to connect to each machine without a password. It doesn’t matter what you call the Ansible user; you can simply use ansible or something clever. We covered how to create SSH keys in Chapter 10, Connecting to Networks, so refer to that if you need a reminder. Creating users was covered in Chapter 2, Managing Users and Permissions. In a nutshell, here are the things you should work on in order to set up your environment for Ansible:
- Install Ansible on a central server or workstation
- Create a user for Ansible on each machine you want to manage configuration on
- Create the same user on your central server or your local machine
- Set up the Ansible user on the server so that it can connect to clients via SSH without a password
- Configure
sudoon the client machines so that the Ansible user can execute commands withsudowith no password
In previous chapters, we covered how to create users and SSH keys, but we have yet to cover the last point. Assuming you named your Ansible user ansible, create the following file:
/etc/sudoers.d/ansibleInside that file, place the following text:
ansible ALL=(ALL) NOPASSWD: ALLNext, we need to ensure that the file is owned by root:
sudo chown root:root /etc/sudoers.d/ansibleFinally, we need to adjust the permissions of the file:
sudo chmod 440 /etc/sudoers.d/ansibleGo ahead and test this out. On the server, switch to the ansible user:
sudo su - ansibleThen, to test this out, use SSH to execute a command on a remote machine:
ssh 192.168.1.123 sudo ls /etcHow does this work? You may or may not know this, but if you use SSH to execute just one command, you don’t necessarily need to set up a persistent connection. In this example, we first switch to the ansible user. Then, we connect to 192.168.1.123 (or whatever the IP address of the client is) and tell it to execute sudo ls /etc. Executing an ls command with sudo may seem like a silly thing to do, but it’s great—it allows you to test whether or not sudo works without doing anything potentially dangerous. Listing the contents of a directory is about as innocent as you can get.
It may seem like an awful lot of steps in order to get configuration management working. But make sure you think with a system administrator’s mindset—these setup tips can be automated. In my case, I have a Bash script that I run on each of my servers that sets up the required user, keys, and sudo access specifically for Ansible. Anytime I want to add a new server to Ansible, I simply run that script on the machine just once, and from that point forward Ansible will take care of the rest.
What should have happened is that the command should have executed and printed the contents of /etc without prompting you for a password. If this doesn’t work, make sure you have completed each of the recommended steps. You should have an ansible user on each machine, and that user should have access to sudo without a password since we created a file for that user in /etc/sudoers.d. If the SSH portion fails, check the log file at /var/log/auth.log for clues, as that is where related errors will be saved. Once you have met these requirements, we can get automating with Ansible!
Making your servers do your bidding
Section titled “Making your servers do your bidding”As server administrators, we’re control freaks. There are few things more exciting than executing a command and having every single server obey it and carry it out. Now that we have Ansible set up, that’s exactly what we’re going to do. I’m assuming by now you have some machines you want to configure, and they’re all set up to communicate via SSH with your central server. Also, as I mentioned before, I highly recommend you utilize something like Git to store your configuration files, but that’s not required for this section.
Setting up an inventory file and configuring Ansible settings
Section titled “Setting up an inventory file and configuring Ansible settings”First, we’ll need an inventory file, which is a special text file Ansible expects to find that tells it where to find servers to connect to. In previous versions, the process of installing the ansible package would provide you with some default configuration, located in /etc/ansible. In Ubuntu 22.04, at least with the version of Ansible that’s offered in the default repositories, this is no longer the case. For our purposes, that’s fine, though – we were going to create an empty configuration anyway.
First, let’s create the directory that we’ll be using for the configuration on our “controller” server (the one we’ll be using to control, or “configure,” other nodes):
sudo mkdir /etc/ansibleAfter that, we can create an empty inventory file:
sudo touch /etc/ansible/hostsThere’s actually a way to avoid needing to create an inventory file, which we’ll get into later in this chapter.
We should also make sure that only the Ansible user account can read it. Execute the following command to change ownership (replace ansible with whatever user account you chose as your Ansible account if you’re using something different):
sudo chown ansible /etc/ansible/hostsNext, modify the permissions such that only the owner can view or change the file:
sudo chmod 600 /etc/ansible/hostsNext, let’s edit the /etc/ansible/hosts file and populate it. If you did end up having a default hosts file created for you when you installed Ansible, you can simply empty the file since we’ll be creating our own anyway. What we’ll do with this file is populate it with the IP addresses of the servers we wish to manage. Feel free to make a backup of the original host file, if it already exists. For right now, we’re only including the IP addresses of the nodes we want to control with Ansible, so the file will end up looking similar to the following:
192.168.1.145192.168.1.125192.168.1.166That’s it; it’s simply just a list of IP addresses. I bet you were expecting some long configuration with all kinds of syntax requirements? Sorry to disappoint. All you need to do is copy a list of IP addresses of the servers you want to manage into this file. If you have DNS names set up for the machines you want to configure, you can use them instead:
myhost1.mydomain.commyhost2.mydomain.commyhost3.mydomain.comSince Ansible understands IP addresses as well as DNS names, we can use either or a combination of both in order to set up our inventory file. We can also split up our hosts within the inventory file between different roles, but that is outside the scope of this book. I do recommend learning about roles in Ansible if you wish to take your knowledge further (see the Relevant videos section for more information).
If you decide not to store your inventory file at /etc/ansible/hosts, you must tell Ansible where to find it. There is another important file to Ansible, and that is its configuration file, located at /etc/ansible/ansible.cfg. Inside this file, we can fine-tune Ansible to get the best performance possible. While we won’t go over this file in detail, just know that you can seriously increase the performance of Ansible by fine-tuning its settings, and Ansible will read settings from its configuration file every time it runs. In our case, if we wish to store our inventory file somewhere other than /etc/ansible/hosts, we will need to add the following two lines to this file (most likely, you’ll need to create this file since it probably doesn’t already exist at this point):
[defaults]inventory = /path/to/hostsremote_user = jayWith the first setting, we’re basically telling Ansible where to find its inventory file. There are many more configuration items we can place in the ansible.cfg file to configure it further, but that’s all we need to configure right now. With the second line, we’re setting a default user to use for Ansible playbooks, which must exist on any servers you wish to manage with it.
Similar to the inventory file, Ansible also checks the local directory for a file named ansible.cfg to fetch its configuration, so you could actually include the configuration file in the Git repository as well, and then execute Ansible commands from within the repository directory. This works because Ansible will check for the existence of a configuration file in your current working directory, and use it if it’s found there. You may want to be careful about including your configuration file in your Git directory, though. While it’s not as private as the inventory file, it can potentially contain privileged information. Therefore, you may want to keep the file at /etc/ansible/ansible.cfg and manage it outside of the Git repository if you include anything private in the file (for example, encryption keys).
Now we can test out whether or not Ansible is working at this point. Thankfully, this is also easy. Just simply execute the following command:
ansible all -m pingThe results should look similar to this:

Figure 15.2: Testing Ansible
Depending on how many servers you have set up, you should see that output one or more times. You may think that all that command has done is a simple ping test, but ping means something different to Ansible than usual. It’s actually attempting to make a connection to the server, to test its availability. If it fails, double-check that the hosts are available over SSH. Success here simply means that Ansible is able to communicate with your hosts via SSH. Now that the communication exists, we can build some actual configuration.
Configuring client servers
Section titled “Configuring client servers”Ansible uses something called a playbook in order to store configuration. A playbook is essentially a file in the YAML format with instructions that Ansible understands, that are interpreted as a set of tasks run against hosts A full guide on YAML isn’t something we’ll be covering here – you don’t have to master this format or even fully understand it to use it with Ansible. That will automatically come in time. The takeaway here is that YAML is simply the format that Ansible uses, and it’s a file format that’s not specific to Ansible itself. A playbook is basically just a collection of instructions written in YAML format, and each individual instruction is known as a play.
You can think of this with the analogy of a sport, like football. Although I don’t know the first thing about football, I do know that football coaches have playbooks containing things that they want their players to do, and each action by a player is a play. It’s the same concept here.
Let’s write our first playbook. Create a file called packages.yml in your local Ansible directory. You can fill it with this content (make sure you include the hyphens):
---- hosts: all become: true tasks: - name: Install htop ansible.builtin.apt: name: htopWe can run this playbook with the following command:
ansible-playbook packages.ymlThis will produce an output that looks something like the following:

Figure 15.3: Ansible in action
Just like that, all of the hosts in your inventory file will have the htop package installed. It really doesn’t matter which package you install, so long as it exists in the repositories; I just used htop as a simple example. But once you run it, you should see an overview of what was changed. Ansible will tell you how many items your hosts updated, how many tasks have failed, and how many targets weren’t reachable at the time the playbook was run.
Let’s take a closer look at what the instructions in this sample playbook do. The hyphens at the beginning are part of the YAML format, so we really don’t need to get into that. Spacing is very important with the YAML format, as you need to be consistent throughout.
In my examples, I am inserting two spaces underneath each heading. A heading starts with a hyphen:
- hosts: allHere, we declare which hosts we want to have the commands apply to. I added all here, which basically runs the configuration against every host in the inventory file. With advanced usage, you can actually create roles in Ansible and divide your hosts between them, such as a web server, database server, and so on. Then, you can apply configuration only to hosts inside a particular role. We’re not going to get into that in this chapter, but just know that it is possible.
The next line is become:
become: trueThis line is basically Ansible’s term for describing sudo. We’re telling Ansible to use sudo to execute the commands, since installing packages requires root privileges. The next part of the playbook is as follows:
tasks:This line starts the next section, which is where we place our individual tasks. Next, we name our new task (Ansible refers to a “task” as a “play”):
- name: Install htopWith name, we give the play a name. This isn’t required but you should always include it. The importance of this is that whatever we type here is what is going to show up in the logs if we enable logging, and will also print to the terminal as the play runs. We should be descriptive here, as it will certainly help if a play fails and we need to locate it in a log file that has hundreds of lines. Next, we utilize the apt module and tell it to install a package, htop in this case:
ansible.builtin.apt: name: htopWe use the ansible.builtin.apt module simply because Ubuntu uses the apt command to manage packages, but modules exist for all of the popular Linux distributions. Ansible’s support of package managers among various distributions is actually fairly extensive. All of the major distributions, such as Red Hat, Fedora, openSUSE, Arch Linux, and Debian, are supported (and those are just the ones I’ve used in my lab off the top of my head). If you want to execute a play against a server that’s running a distribution other than Ubuntu, simply adjust ansible.builtin.apt to something else, such as ansible.builtin.dnf.
Since Ansible allows you to use additional resources from Galaxy, a site that’s specific to Ansible that gives you access to additional capabilities, we use ansible.builtin.apt instead of simply apt in order to clarify that we’re using a built-in module, not something we’re using from an external resource.
This new naming scheme is new since the previous edition of this book was published. The older versions of the playbooks should still work just fine, but since the naming scheme has changed since then, we’ll use the new naming style going forward.
You can, of course, add additional packages by simply adding more plays to the existing playbook:
---- hosts: all become: true tasks: - name: Install htop ansible.builtin.apt: name: htop - name: Install git ansible.builtin.apt: name: git - name: Install vim-nox ansible.builtin.apt: name: vim-noxHowever, that’s not an extremely efficient method. I’ll show you how we can combine multiple similar plays in one play. Sure, you don’t have to, but I think you’ll agree that this method looks cleaner:
---- hosts: all become: true tasks: - name: Install packages ansible.builtin.apt: name: - git - htop - vim-noxWith the new format, we include just one play to install multiple packages. This is similar to the concept of a for loop if you have programming knowledge. For every package we list, it will run the ansible.builtin.apt module against it. If we want to add additional packages, we just add a new one to the list. Simple.
We can also copy files to our hosts. Consider the following example playbook, which I will call copy_files.yml:
---- hosts: all become: true tasks: - name: copy SSH motd ansible.builtin.copy: src: motd dest: /etc/motdThen, you can run this playbook with the following command:
ansible-playbook copy_files.ymlInside the same directory, create a file called motd and place any text in it. It doesn’t really matter what you type into the file, but this file in particular acts as a message that is printed any time a user logs into a server. When you run the playbook, it will copy that file over to the server at the destination you configured. Since we created a message of the day (motd), we should see the new message the next time we log in to the server.
By now, you’re probably seeing just how useful Ansible can be. Sure, we only installed a few packages and copied one file. We could’ve performed those tasks easily ourselves without Ansible, but this is only a start. Ansible lets you automate everything, and we have to start somewhere. You can tell it to not only install packages and copy files, but you can also use it to start services, apply configuration file templates, and much more—it will surprise you. In fact, you can go as far as to automate the setup of a web server, a user’s workstation… you name it!
Putting it all together – automating web server deployment
Section titled “Putting it all together – automating web server deployment”Speaking of automating the setup of a web server, why don’t we go ahead and do exactly that? It’ll be another simple example, but it will serve you well if we demonstrate more of what Ansible can do. We will set up a playbook to perform the following tasks:
- Install Apache
- Start the
apache2service - Copy an HTML file for the new site
First, let’s set up the playbook to simply install Apache. I called mine apache.yml, but the name is arbitrary:
---- hosts: all become: true tasks: - name: Install Apache ansible.builtin.apt: name: apache2No surprises here; we’ve already installed a package at this point. Let’s add an additional instruction to start the apache2 service:
---- hosts: all become: true tasks: - name: Install Apache ansible.builtin.apt: name: apache2 - name: Start the apache2 services ansible.builtin.service: name: apache2 state: startedSo far, the syntax should be self-explanatory. Ansible has a service module, which can be used to start a service on a host. In this case, we start the apache2 service (the service would’ve already been started when apache2 was installed, but at least this way we can make sure it’s started). You do have to know the service name ahead of time, but you don’t have to pay attention to what utility needs to be used in the background to start the service. Ansible already knows how to start services on all the popular distributions and takes care of the specifics for you in the background.
Having a play that starts apache2 may seem a bit redundant since most packages you install on an Ubuntu server will automatically start the associated service as soon as it’s installed (and apache2 is no exception). But when we write automation code, it’s important to be clear and explicit about what the desired end result is supposed to be. Even though apache2 will automatically start once Ansible installs the package, we’re adding a service play to clarify the fact that it needs to be running, so anyone looking at it will know what’s expected. Also, we can make sure apache2 is enabled by adding one more line to that section of the code (note the bold line):
- name: Start the apache2 services ansible.builtin.service: name: apache2 state: started enabled: trueI’ll leave it up to you whether or not to include that extra line, but the takeaway is to be as clear and direct as you can when writing automation, so there’s no confusion about what the end result is supposed to be.
Well, that was easy. Let’s create a simple web page for Apache to serve for us. It doesn’t need to be fancy, we just want to see that it works. Create the following content inside a file named index.html in the same working directory as the other Ansible files:
<html><title>Ansible is awesome!</title><body> <p>Ansible is amazing. With just a small text file, we automated the setup of a web server!</p></body></html>As you can see, that HTML file is fairly lame, but it will work just fine for what we need. Next, let’s add another instruction to our Apache playbook:
---- hosts: all become: true tasks: - name: Install Apache ansible.builtin.apt: name: apache2 - name: Start the apache2 services ansible.builtin.service: name: apache2 state: started
- name: Copy index.html ansible.builtin.copy: src: index.html dest: /var/www/html/index.htmlWith the copy module, we can copy a file from our local Ansible directory to the server. All we need to do is provide it with a source (src) and destination (dest).
Let’s go ahead and run the new playbook:
ansible-playbook apache.ymlThis will produce an output like the following:

Figure 15.4: Installing Apache and copying a default site file with Ansible
Within a few minutes, you should have at least one web server configured by Ansible. In a real production environment, you would’ve only run this on servers within a specific role, but roles are beyond the scope of this chapter. But from the simple playbook we created, you should be able to see the power of this amazing piece of software:

Figure 15.5: An example of a web page provisioned by Ansible
At this point in our exploration into configuration management with Ansible, we’ve installed packages, started services, and copied files. Admittedly, this isn’t much, but it’s really all you need in order to practice the basics. The documentation for Ansible includes guides on how to do all sorts of things, and you’ll be able to utilize the various modules it provides to perform many different tasks.
To explore Ansible further, I recommend that you think about things you do on a regular basis that you would benefit from automating. Things like installing security updates, creating user accounts, setting passwords, and enabling services to automatically start at boot time are tasks that are easy to get started with.
In addition, you may want to consider adding a simple cron job to run under your ansible user, to run the playbook every hour or so. We covered cron jobs in Chapter 7, Controlling and Managing Processes. Adding a cron job shouldn’t add any overhead from a resource perspective, since Ansible won’t actually be doing much unless you add a new command. In more advanced usage, you’ll want to have Ansible check out code from a repository and then apply the configuration if it has changed. This way, all you have to do is commit to a Git repository and all of your servers will download the configuration and run it at the next scheduled time. One of the things I love most about Ansible is that it’s easy to get started, but you’ll continue to find new ways to use it and benefit from it.
Using Ansible’s pull method
Section titled “Using Ansible’s pull method”The way we set up our Ansible configuration in the previous section works very well if we have a list of specific servers that we want it to manage. To add a new server, we create the user account and set up the SSH configuration on the new host, and then add it to the inventory file. If we decommission that server, we simply remove it from the inventory file. This works well in a static environment, where servers you deploy typically stay around for a while. In a dynamic environment, though, this may not work as well.
Dynamic environments are very typical in the cloud. With cloud computing, you typically have one or more virtual servers that provide a service to your company or users. These servers may come and go at any time. With dynamic environments, servers will come online as needed to handle load and will also get decommissioned automatically as load decreases. Therefore, you never really know when a server is going to come online, and having to manually provision a server in such an environment is inefficient.
For this reason, Ansible’s inventory file may not be a good fit for dynamic infrastructure. There certainly are ways to make Ansible’s inventory work in such an environment, as you can actually replace the inventory file with an executable script that can make API calls and customize itself for your infrastructure if you so desire. However, that’s out of the scope of this book, and there’s an easier method anyway.
As we’ve seen, Ansible uses an inventory file and connects to every server listed in that file. However, Ansible also features a pull mode, where instead of having a central server that connects to other machines, each server in pull mode will actually run Ansible against itself. In my opinion, this is a great way to use Ansible and it doesn’t seem to get the attention it deserves. First, I’ll explain the theory of how it works, and then we can work through an actual example.
With pull mode, you’ll want to have your Ansible playbooks inside a Git repository. This repository must be accessible from the servers you will manage. For example, if you store the Git repository on GitHub, you’ll want to make sure the servers can access GitHub externally. If you host your own Git server internally, you’ll want to make sure your servers are able to access it through your firewall or any security rules you may have in place.
Pull mode is used with the ansible-pull command, which comes bundled with Ansible. The syntax looks like the following:
ansible-pull -U https://github.com/myusername/ansible.gitOf course, you’d replace the URL with the actual HTTP or HTTPS URL to your Git repository. However, that’s basically it. The ansible-pull command simply expects the -U option (short for URL) along with the URL to a Git repository.
In order for this to work, you’ll need a playbook inside the repository with a special name, local.yml. If you don’t declare a specific playbook with Ansible, it will expect to find a playbook with that name inside the root of the repository. If you choose to use a name for the main playbook that’s something other than local.yml, you’ll need to specify it:
ansible-pull -U https://github.com/myusername/ansible.git myplaybook.ymlIn this example, the ansible-pull command will cache the Git repository located at the specified URL locally, and run the playbook myplaybook.yml that you would have inside the repository. One thing you may find is that Ansible might complain about not finding an inventory file, even though that’s the entire point of the ansible-pull command. You can ignore this error. This will likely be fixed in Ansible at some point in the future, but as of the time of this writing, it will print a warning if it doesn’t detect an inventory file.
With the theory out of the way, let’s work through an actual example. If you’ve been following along so far, we created a playbook in the previous section that automates the deployment of a hypothetical web server. We can reuse that code. However, it’s best practice to have a file with the name local.yml, so you can simply rename the apache.yml playbook we created earlier to local.yml. There’s one small change we need to make to the file, which I’ve highlighted here:
---- hosts: localhost become: true tasks: - name: Install Apache ansible.builtin.apt: name=apache2 - name: Start the apache2 services ansible.builtin.service: name: apache2 state: started - name: Copy index.html ansible.builtin.copy: src: index.html dest: /var/www/html/index.htmlSince we’re executing the playbook locally (without SSH) we changed the hosts: line to point to localhost, to instruct Ansible that we want to execute the commands locally rather than remotely. Now, we can push this playbook to our Git repository and execute it directly from the repository URL.
Pay careful attention to the hosts: line of any playbook you intend to run. If you are using the pull method, this line will need to be changed from hosts: all to hosts: localhost, the reason being, we are executing the playbooks directly on localhost, rather than from a remote SSH connection. If you don’t make this change, you’ll see an error similar to the following:
ERROR! Specified hosts and/or --limit does not match any hostsBefore you run the playbook, you’ll want to first switch to your Ansible user, since the playbook will need to be run as a user with sudo privileges since it will execute system-level commands:
sudo su - ansibleThen, execute the playbook:
ansible-pull -U https://github.com/myusername/ansible.gitIf we kept the name as apache.yml, we would just specify that:
ansible-pull -U https://github.com/myusername/ansible.git apache.ymlKeep in mind that since ansible-pull executes playbooks directly on localhost, the playbook must be executed by a user that has access to sudo. If sudo is not configured to run as that user without a password, the playbook will fail as it’s not interactive (it won’t ask for the password). You can also use sudo in front of the ansible-pull command and provide the password before it runs, but that won’t work if you set it up to run automatically via cron.
If all goes according to plan, the playbook repository should be cached to the server, and the instructions carried out. If there is an error, Ansible is fairly good about presenting logical error messages. As long as the user has permission to execute privileged commands on the server, the required files are present in the repository, and the server has access to download the repository (a firewall isn’t blocking it), then the playbook will run properly.
When it comes to implementing the pull method in production across various server types, there are several ways we can go about this. One way is to have a separate playbook per server type. For example, perhaps you’d have an Apache playbook, as well as playbooks specific to database servers, file servers, user workstations, and so on. Then, depending on the type of server you’re deploying, you’d specify the appropriate playbook when you called the ansible-pull command. If you’re using a service such as cloud computing, you can actually provide a script for each server to execute upon creation. You can then instruct the service to automatically run the ansible-pull command any time a new server is created. In AWS, for example, you can use a feature known as user data to place a script for a server to execute when it’s launched for the first time. This saves you from having to provision anything manually. For this to work, you would first include a command for the server to install Ansible itself, and then the next command would be the ansible-pull command along with the URL to the repository. Just those two lines would completely automate the installation of Ansible and the application of your playbook. Try to think of the possibilities ahead of time so you can understand the many ways that automation can benefit you.
While provisioning new servers properly and efficiently is very important, so too is maintaining your existing servers. When you need to install a new package or apply a security update, the last thing you want to do is connect to all of your servers manually and update them one by one. The ansible-pull command allows simple management as well; you just simply run the command again. Every time you run ansible-pull, it will download the latest version of the code within your repository and run it. If there are any changes, they will be applied, while things that have already been applied will be skipped. For example, if you include a play to install the apache2 package, Ansible won’t reinstall the package if you run the playbook a second time; it will skip that play since that requirement has already been met.
One trick worth knowing with ansible-pull is the -o option. This option will ensure that the playbook inside the repository is only run if there have been any actual changes to the repository. If you haven’t committed any changes to the repository, it will skip the entire thing. This is very useful if you set up the ansible-pull command syntax to be run periodically via cron, for example, every hour.
If you don’t include the -o option, Ansible will run the entire playbook every hour. This will consume valuable CPU resources for no good reason at all. With the -o option, Ansible will only use as much CPU as required for simply checking the repository for changes. The playbook will only be run if you actually commit changes to the repository.
The introduction to Ansible within this chapter has been very basic, as we’ve only used the very core of the required components. By looking deeper into Ansible, you’ll find more advanced techniques, and some more clever ways to implement it. Examples include automating firewall rules, security patches, and user passwords, as well as having Ansible send you an email any time a server is successfully provisioned (or even when that fails). Basically, just about anything you can do manually, you can automate with Ansible. In the future, I recommend looking at server administration with an automation mindset. As I’ve mentioned several times before, if you will need to perform a task more than once, automate it. Ansible is one of the best ways of automating server administration.
Summary
Section titled “Summary”In this chapter, we took a look at configuration management using Ansible. Ansible is an exciting technology that is exploding in popularity. It gives you the full power of configuration management utilities such as Chef or Puppet, without all the resource overhead. It allows you to automate just about everything. During our exploration, we walked through installing packages, copying files, and starting services. Near the end of the chapter, we worked through an example of using Ansible to provision a simple web server, and we even explored the pull method, which is very useful in dynamic environments. These concepts form the basis of knowledge that can be expanded to automate more complex rollouts.
The next chapter will be fun: we’ll set up our very own virtualization server with KVM. This is one of my favorite topics, and I’m sure you’ll enjoy it too. See you there!
Further reading
Section titled “Further reading”- Ansible documentation: https://learnlinux.link/ansible-docs
- Ansible Roles documentation: https://learnlinux.link/reuse-roles
- Ansible config file documentation article: https://learnlinux.link/a-config
ansible-pulldocumentation: https://learnlinux.link/a-pull- How to manage your workstation configuration with Ansible: https://learnlinux.link/a-ws
- Git basics: https://learnlinux.link/git-book
- Setting up Git (GitHub): https://learnlinux.link/setup-git