Using Ansible for Configuration as Code
local graph
Chapter 11 — Using Ansible for Configuration as Code
Section titled “Chapter 11 — Using Ansible for Configuration as Code”In this chapter, we are going to cover configuration management (CM), Configuration as Code (CaC), and our tool of choice for it: Ansible.
We will cover the following topics:
- CM systems and CaC
- Ansible
- Ansible Galaxy
- Handling secrets
- Ansible Tower and alternatives
- Advanced topics
Technical requirements
Section titled “Technical requirements”For this chapter, you will need a Linux system that you can access through ssh. If your main operating system is Windows, you will need another Linux system to play the role of the control node. As of now, the Ansible project does not support Windows as a control node.
CM systems and CaC
Section titled “CM systems and CaC”Setting up and maintaining a system other than a hobbyist server (and even those, maybe, too) poses a serious challenge: how do you ensure that the system is installed and configured correctly and according to expectations? When you have to install a new server that is identical in configuration, how do you ensure that? In the past, a way of doing it was documenting the current configuration after the installation process was done. This would be a document describing the hardware, operating system, installed software, created users, and configuration applied. Any person who wanted to recreate it would have to follow steps to achieve the configuration described in the document.
The very next logical step is to write shell scripts that achieve the same goal with one additional improvement over the manual process: the scripts—properly written, tested, and maintained—do not require manual work, except, maybe, the initial system installation. But a properly set up environment would take care even of this.
The scripts, however, also have some defects or deficiencies. One of them is the fact that you need to account in your scripts for unfinished execution. This could happen for various reasons and would leave the system in a partially configured state. Executing the script again would perform all configuration actions from the start, sometimes leading to unexpected results. One way to account for incomplete runs would be to wrap every configuration action in a check, to see whether it had been performed previously. That would lead to the configuration script becoming larger and, eventually, evolving into a library of configuration functions and check functions.
The task of developing and maintaining such a tool can be daunting and will probably require a whole team. Still, the results are probably worth the effort.
Writing and maintaining documentation that describes the desired state of the system may, at first glance, be simpler and more desirable than the previously mentioned method of automating. The script cannot recover from an incomplete execution. The best it can do is inform the sysop about failure, log the error, and stop gracefully. Manually performed configuration allows the sysop to work around any obstacles and inadequacies in the procedure and edit the document to reflect the current state on the go.
Still, a properly developed and tested script turns out to be better. Let us enumerate the reasons:
- If the script executes without an error, it is guaranteed to perform actions without a mistake. Time and again, it has been proven that a human is the element most prone to errors in IT.
- If the script exits prematurely, the action of updating it to account for the new requirements is a perfect equivalent to updating the documentation.
- People are known to be pretty bad at maintaining documentation. The Holy Grail of programming is self-documenting code, rendering comments unnecessary, thus eliminating the risk of comments being out of sync with the code.
- The script can be executed on multiple systems at once, scaling very well, if not infinitely. Humans can perform the configuration of one system at a time with minimal risk of making a mistake.
- Configuration kept in the form of a script or program benefits from typical programming techniques, such as automated testing, dry runs, and static analysis. More so, keeping the code in a repository allows us to easily track a history of changes and integrate it with ticket-tracking tools.
- Code is unequivocal, which cannot be said about written language. A document may leave space for interpretation; a script won’t.
- Automating configuration lets you move to other, more interesting tasks, leaving the computers to do what they do best—performing repetitive, boring tasks well.
The world of programming and system administration has a tendency to turn small projects into larger ones with a vibrant community of developers and users. It was only a matter of time before CM systems were born. They take the burden of developing and managing portions of the code responsible for configuration actions off your shoulders. The CM system developers write the code, test it, and deem it stable. What you are left with is an action of writing configuration files or directives that tell the system what to do. Most of these systems will be able to cover the most popular platforms, allowing you to describe configuration once and run it with the same expected results on commercial Unix systems, such as AIX or Solaris, as on Linux or Windows.
Configuration files for these systems are easily stored in a version control system such as Git. They are easily understandable by a human, which allows for simple review by your colleagues. They can be checked for syntax errors by automated tools and allow you to concentrate on the most important part of the whole endeavor: the configuration.
This approach of keeping your configuration as a set of scripts or other data instead of a procedure to be followed manually is known as CaC.
The CaC approach is becoming more important as the number of systems to be managed grows and the demand for fast and efficient configuration scales up. In the world of DevOps, it is usual practice to set up tens and hundreds of systems a day: systems for developers, testers, and production systems to manage new levels of demand for the service. Managing it manually would be an impossible task. Well-implemented CaC allows to run this task with a click of a button. Thus, developers and testers can deploy their own systems without bothering sysops. Your task will be to develop, maintain, and test the configuration data.
If there is one thing sure in the world of programming, it is that there’s never going to be only one solution. The same goes for CM tools.
Alternatives for Ansible include SaltStack, Chef, Puppet, and CFEngine, which is the oldest one; its initial release date was 1993, so it’s 30 years old as of the time of writing this book. In general, those solutions differentiate between each other with a method of enforcing configuration (pull or push) and an approach of describing the system’s state (imperative or declarative).
Imperative means that we describe the state of the server with commands for the tool to perform. Imperative programming focuses on describing how a given tool operates step by step.
Declarative, on the other hand, means we focus on what the CaC tool should accomplish without specifying all the details of how it should achieve the result.
SaltStack
Section titled “SaltStack”SaltStack is an open source CM tool that allows for the management of complex IT infrastructure at scale. It enables the automation of routine tasks such as package installation, user management, and software configuration, and is designed to work across a wide range of operating systems and platforms. SaltStack was founded by Thomas Hatch in 2011. The first release of SaltStack, version 0.8.0, was also made in 2011.
SaltStack works by utilizing a master-slave architecture, where a central salt-master communicates with salt-minions running on remote machines to execute commands and manage configurations. It operates in a pull method of enforcing configuration: minions pull the latest manifest from the master server.
Once the minion is installed and configured, we can use SaltStack to manage the server’s configuration. Here’s an example nginx.sls file that would install and configure nginx:
nginx: pkg.installed/etc/nginx/sites-available/yourdomain.tld.conf: file.managed: - source: salt://nginx/yourdomain.tld.conf - user: root - group: root - mode: 644In this example, the first line specifies that the nginx package should be installed on the target server. The next two lines define the configuration file for a hypothetical website, example.com, which is copied to /etc/nginx/sites-available/yourdomain.tld.conf.
To apply this state file to a server, we would use the state.apply command in the SaltStack command-line interface, specifying the name of the state file as the argument:
admin@myhome:~$ salt 'webserver' state.apply nginxThis would send the instructions in the nginx.sls file to the salt-minion running on the web server machine, which would execute the necessary steps to ensure that nginx is installed and configured correctly.
Chef is a powerful open source CM tool that allows users to automate the deployment and management of infrastructure, applications, and services. It was first released in 2009 by Opscode, which was later acquired by Chef Software Inc. Since then, Chef has been widely adopted by IT professionals and DevOps teams to streamline their workflows and reduce the time and effort required for managing complex systems.
Chef works by defining the desired state of an infrastructure in a set of code files, called cookbooks. A cookbook is a collection of recipes that describe how to install, configure, and manage a specific piece of software or service. Each recipe contains a series of resources, which are pre-built modules that can perform specific tasks, such as installing a package or configuring a file. Chef uses a declarative approach to CM, meaning that users define what they want the system to look like, and Chef takes care of the details of how to get there.
To install nginx using Chef, you would first need to create a cookbook that includes a recipe for installing nginx. This recipe would use the package resource to install the nginx package and the service resource to ensure that the nginx service is running. You could also use other resources, such as file, directory, or template, to configure nginx ’s settings, depending on your requirements.
Once you had created the cookbook, you would upload it to a Chef server, which acts as a central repository for cookbooks and their associated metadata. You would then use Chef’s command-line tool, called knife, to configure the target system to use the cookbook. This involves associating the system with a Chef environment, which defines the set of cookbooks and their versions that should be applied to the system. You would then use the chef-client command to run the Chef client on the target system, which will download and apply the necessary cookbooks and recipes to bring the system into the desired state.
Here’s an example of installing and configuring nginx:
# Install Nginx packagepackage 'nginx'# Configure Nginx serviceservice 'nginx' do action [:enable, :start]end# Configure Nginx sitetemplate '/etc/nginx/sites-available/yourdomain.tld.conf' do source 'nginx-site.erb' owner 'root' group 'root' mode '0644' notifies :restart, 'service[nginx]'endThis recipe uses three resources, as follows:
package: This installs thenginxpackage using the default package manager on the system.service: This starts and enables thenginxservice so that it will automatically start on boot and stay running.template: This creates a configuration file fornginxby generating it from a template file. The template file (nginx-site.erb) is written in Embedded Ruby (ERB) format and is located in thetemplatesdirectory of the cookbook. Thenotifiesattribute tells Chef to restart thenginxservice if the configuration file changes.
Once you have created this recipe in a cookbook, you can use the knife command to upload the cookbook to a Chef server. You can then use the chef-client command to apply the recipe to a target system, which will install and configure nginx according to the recipe.
Puppet
Section titled “Puppet”Puppet is an open source CM tool that allows system administrators to automate the deployment, configuration, and management of infrastructure. It was created by Luke Kanies in 2005 and released under the Apache License 2.0.
Puppet works by defining the desired state of infrastructure resources in a declarative language, known as the Puppet language. Administrators can define the configuration of servers, applications, and other infrastructure components in Puppet code, which can then be applied consistently across multiple systems.
Puppet consists of a master server and multiple agent nodes. The master server acts as a central repository for Puppet code and configuration data, while the agent nodes execute the Puppet code and apply the desired state to the system.
Puppet has a robust ecosystem of modules, which are pre-written Puppet code that can be used to configure common infrastructure resources. These modules are available in Puppet Forge, a public repository of Puppet code.
Here’s an example Puppet manifest that installs nginx and creates a configuration file similar to what we did with SaltStack and Chef:
# Install Nginxpackage { 'nginx': ensure => installed,}# Define the configuration template for the domainfile { '/etc/nginx/sites-available/yourdomain.tld.conf': content => template('nginx/yourdomain.tld.conf.erb'), owner => 'root', group => 'root', mode => '0644', notify => Service['nginx'],}# Enable the site by creating a symbolic link from sites-available to sites-enabledfile { '/etc/nginx/sites-enabled/yourdomain.tld.conf': ensure => 'link', target => '/etc/nginx/sites-available/yourdomain.tld.conf', require => File['/etc/nginx/sites-available/yourdomain.tld.conf'],}# Restart Nginx when the configuration changesservice { 'nginx': ensure => running, enable => true, subscribe => File['/etc/nginx/sites-enabled/yourdomain.tld.conf'],}Once you’ve created a manifest and put it on the Puppet server, it will be picked up by the Puppet agent installed on your server and executed. Communication, the same as in SaltStack, is being secured by the TLS protocol using the same mechanism as the HTTPS servers on the internet.
The agent nodes run a Puppet agent process, which connects to the master server over TCP port 8140. The agent sends a certificate signing request (CSR) to the server, which the administrator must approve. Once the CSR is approved, the agent is granted access to the server’s Puppet configuration.
When the agent runs, it sends a request to the master server for its configuration. The server responds with a catalog of resources that should be applied to the node. The catalog is generated based on the Puppet code and manifests stored on the server, as well as any external data sources or hierarchies that are configured.
The agent then applies the catalog to the node, which involves making any necessary changes to the node’s configuration to ensure it matches the desired state defined in the catalog. This may involve installing packages, updating configuration files, or starting or stopping services.
The agent sends reports back to the server after applying the catalog, which can be used for monitoring and auditing purposes. The server can also use this information to detect changes to the node’s configuration that were not made through Puppet and to take corrective action if necessary.
CFEngine
Section titled “CFEngine”CFEngine is an open source CM system that allows users to automate the deployment, configuration, and maintenance of IT systems. It was founded by Mark Burgess in 1993 and has since become a popular tool for managing large-scale IT infrastructures. CFEngine is known for its powerful and flexible language for describing system configurations and enforcing policies, making it a great choice for complex IT environments.
CFEngine’s first release was in 1994, making it one of the oldest CM tools in existence. Since then, CFEngine has undergone numerous updates and improvements to keep up with changing IT environments and emerging technologies. The latest release of CFEngine, version 3.18, includes features such as improved encryption, enhanced monitoring capabilities, and better support for cloud infrastructure.
CFEngine has gained popularity over the years due to its robust functionality, ease of use, and strong community support. It’s still being used today by many organizations and is actively developed, so it is a safe option to manage the configuration of your servers using this tool.
An example CFengine configuration will be presented here. It is, out of necessity, only a snipped and not a complete configuration:
############################################################### cf.main - for master infrastructure server###################################################################### BEGIN cf.main###control: access = ( root ) # Only root should run this site = ( main ) domain = ( example.com ) sysadm = ( [email protected] ) repository = ( /var/spool/cfengine ) netmask = ( 255.255.255.0 ) timezone = ( CET )#################################################################files: Prepare:: /etc/motd m=0644 r=0 o=root act=touchIn this section, we have explained what CaC is and why it is an important tool in the toolbelt of system administrators. We have briefly described the most popular tools available to you. In the next section, we will introduce our tool of choice—Ansible.
Ansible
Section titled “Ansible”In this section, we are going to introduce you to Ansible, our tool of choice when it comes to CaC.
Ansible is a tool written for managing the configuration of systems and devices. It is written in Python and its source code is freely available to anyone for downloading and modification (within the limits of its license, which is Apache License 2.0). The name “Ansible” comes from Ursula K. Le Guin’s book Rocannon’s World and denotes a device that allows instantaneous communication no matter the distance.
Some interesting characteristics of Ansible are set out here:
- Modularity: Ansible is not a monolithic tool. Rather, it’s a core program with each task it knows how to perform written as a separate module—a library, if you will. Since this was the design from the start, it produced a clean API that anyone can use to write their own modules.
- Idempotence: No matter how many times you perform a configuration, the result is always the same. This is one of the most important and fundamental characteristics of Ansible. You don’t have to know which actions have been performed. When you extend the configuration and run the tool again, it is its job to find out the state of the system and only apply new actions.
- Agentlessness: Ansible doesn’t install its agent on the configured system. That is not to say it doesn’t need anything at all. To execute the Ansible scripts, the target system will need some means of connecting to it (most often, the SSH server running) and the Python language installed. There are several advantages born from this paradigm, including the following:
- Ansible is not concerned with communication protocol. It uses SSH, but it doesn’t implement it, leaving the details to the operating system, SSH server, and client. An advantage is that you can freely swap one SSH solution with another for whatever reasons, and your Ansible playbooks should work as intended. Also, Ansible doesn’t concern itself with securing the SSH configuration. This leaves developers to concentrate on what the system is really about: configuring your systems.
- The Ansible project does not need to develop and maintain separate programs for managed nodes. This not only frees developers from unneeded burdens but also limits the possibility of security exploits being discovered and used against target machines.
- In an agent-utilizing solution, if, for any reason, the agent program stops working, there is no way to deliver new configurations to the system. SSH servers are usually very widely used, and the probability of failure is negligible.
- Using SSH as the communication protocol lowers the risk of a firewall blocking the communication port for the CM system.
- Ansible is not concerned with communication protocol. It uses SSH, but it doesn’t implement it, leaving the details to the operating system, SSH server, and client. An advantage is that you can freely swap one SSH solution with another for whatever reasons, and your Ansible playbooks should work as intended. Also, Ansible doesn’t concern itself with securing the SSH configuration. This leaves developers to concentrate on what the system is really about: configuring your systems.
- Declarativeness: A person writing Ansible playbooks shouldn’t trouble themselves much with how things should be done, just what the desired state of the system is after Ansible is done. For example, if the sysop wants Ansible to ensure that
nginxis installed, the proper configuration entry would look like this:- name: install nginxpackage:name: nginxstate: present
The main way of working with Ansible is through writing configuration files in a special syntax called YAML. YAML is a syntax created specifically for configuration files and is loosely based on Python formatting. Indentations play a significant role in YAML files. YAML’s home page presents a full cheat sheet card (https://yaml.org/refcard.html) for the syntax. However, the most important parts are presented here, as we will be working mostly with those files in this chapter:
- They are clear text files. This means they can be viewed and edited using the simplest editors such as Notepad, Vim, Emacs, or whatever is your favorite tool for working with text files.
- Indentations are used to denote scope. Tabulators are not permitted for indentations and it is customary to use spaces for this purpose.
- A new document opens with three hyphens (
-). One file can have more than one document. - Comments, as in Python, start with a hash (
#) and continue until the end of the line. The comment must be surrounded by whitespace characters; otherwise, it will be treated as a literal hash (#) within a text. - Text (strings) can be unquoted, single-quoted (
'), or double-quoted ("). - When a list is specified, each member is denoted by a hyphen (
-) character. Each item will be on a separate line. If single-line representation is required, the list items can be enclosed in square brackets ([]) with entries separated by commas (,). - Associative arrays are represented by a key-value pair with each key separated from the value by a colon and space. If they have to be presented in one line, the array is enclosed in curly brackets (
{}) and pairs are separated by commas (,).
If the preceding rules are not very transparent now, don’t panic. We are going to write proper Ansible configuration files, and things will become clear as we go.
Ansible divides machines into two groups: the control node is the computer that stores configuration directives and will connect to the target machines and configure them. There can be more than one control node. The target machines are called inventories. Ansible runs actions against computers in the inventory listed. Inventories are often written in an initialization (INI) format, which is simple and easy to follow, as explained here:
- Comments start with a semicolon (
;) - Sections are named and the names are enclosed in square brackets (
[]) - Configuration directives are stored in pairs, each pair on its own line, with the key and value separated by an equals sign (
=)
We will see an example of an inventory file shortly. There is, however, the possibility of having so-called dynamic inventories, which are generated automatically by means of a script or a system with each run of Ansible.
The main configuration file that we will be interacting with is called a playbook. A playbook is an entry point for Ansible. This is where the tool will start the execution. Playbooks can include other files (and this is often done).
Target hosts can be broken down into groups based on custom criteria: operating system, role within organization, physical location—whatever is required. The groups are called roles.
A single action that is to be performed is called a task.
Basics of using Ansible
Section titled “Basics of using Ansible”The first thing to do is to install Ansible. This is pretty straightforward on all major Linux distributions and can be equally so on macOS. We encourage you to figure out the solution for your chosen operating system.
For Debian-based distributions, the following command should suffice:
$ sudo apt-get install ansibleFor Fedora Linux distributions, you’d run the following command:
$ sudo dnf install ansibleTo install the latest version of Ansible, however, we recommend using the Python virtual environment and its pip tool, like so:
$ python3 -m venv venvIn the preceding code, we have activated the virtual environment using the venv python3 module. It will create a special venv directory that contains all important files and libraries that allow us to set up a Python virtual environment. Next, we have the following:
$ source venv/bin/activateIn the preceding code line, we have read a special file that sets the environment by configuring the shell. Next, we have this:
$ pip install -U pipRequirement already satisfied: pip in ./venv/lib/python3.11/site-packages (22.3.1)Collecting pip Using cached pip-23.0.1-py3-none-any.whl (2.1 MB)Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 22.3.1 Uninstalling pip-22.3.1: Successfully uninstalled pip-22.3.1Successfully installed pip-23.0.1In the preceding code, we have upgraded pip, a Python package installer. In the next step, we are going to actually install Ansible, also using pip. The output will be shortened for brevity:
$ pip install ansibleCollecting ansible Using cached ansible-7.3.0-py3-none-any.whl (43.1 MB)Collecting ansible-core~=2.14.3Installing collected packages: resolvelib, PyYAML, pycparser, packaging, MarkupSafe, jinja2, cffi, cryptography, ansible-core, ansibleSuccessfully installed MarkupSafe-2.1.2 PyYAML-6.0 ansible-7.3.0 ansible-core-2.14.3 cffi-1.15.1 cryptography-39.0.2 jinja2-3.1.2 packaging-23.0 pycparser-2.21 resolvelib-0.8.1As you can see in the preceding code, pip informs us that Ansible and its dependencies were installed successfully.
Either of those ways of installing Ansible will download and install all the packages required to run Ansible on your computer.
The simplest command you can run is called ad hoc ping. It’s so basic that it is one of the most prevalent first uses of Ansible in tutorials and books. We are not going to deviate from it. The following command tries to connect to a specified host and then prints the result of the trial:
$ ansible all -m ping -i inventoryhostone | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong"}In the preceding command, we have told Ansible to run against all hosts in inventory, use the ping module (-m ping), and use an inventory file named inventory (-i inventory). If you don’t specify the inventory file, Ansible will try to use /etc/ansible/hosts, which is generally a bad idea.
We will delve into the inventory file in a moment, a few paragraphs ahead.
A module is a small Python library written for Ansible and should ideally map 1:1 with a command. In the preceding example, we run the ping module (which we can also understand as a ping command). It is not, however, the same as the OS ping command, which sends a specially crafted network packet to determine whether a host is up. The Ansible ping command will also try to log in, to determine whether the credentials are correct.
By default, Ansible uses the SSH protocol with a private-public key pair. In a normal operation, you don’t want to use password-based authentication, and Ansible will choose a key-based one.
Ansible is very good at providing self-explanatory information as a result of the execution. The preceding output tells us that Ansible was able to connect to all nodes in the inventory (there’s only one), python3 is installed there, and nothing was changed.
The inventory file is a simple but powerful tool to list, group, and provide variables for managed nodes. Our example inventory file is pasted as follows:
[www]hostone ansible_host=192.168.1.2 ansible_ssh_private_key_file=~/.ssh/hostone.pem ansible_user=adminThe preceding code is two lines. The first one declares a group of nodes called www. The second declares a node called hostone. Since this is not a name resolvable by DNS, we have declared its IP address using the ansible_host variable. Then, we point to the proper ssh key file and declare which username should be used while logging in (admin). There are more things we can define in this file. Very detailed information about writing your inventory file can be found in the Ansible project documentation (https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html).
We are unable to cover all aspects of Ansible and dig deeper into most of the features that we are going to use here. Packt Publishing, however, has a very good selection of books on Ansible that you may wish to choose if you want to deepen your knowledge—for example, Mastering Ansible, Fourth Edition, by James Freeman and Jesse Keating.
Tasks lie at the heart of the Ansible configuration. They are exactly that: tasks to be run against managed nodes. Tasks are contained in plays. They can be placed there directly (put into a playbook) or indirectly (included via a role).
There’s a special type of task called handles. This is a task that will be executed only when notified by another task.
Ansible’s documentation defines roles as “ A limited distribution of reusable Ansible content (tasks, handlers, variables, plugins, templates and files) for use inside of a play.” For our purpose, we can think of them as a mechanism to group parts of the play. A role can be a type of host we are going to run the play against: web server, database server, Kubernetes node, and so on. By breaking down playbooks into roles, we can manage separate required tasks more easily and efficiently. Not to mention the files containing those become more readable, since we limit the number of tasks within and group them by their function.
Plays and playbooks
Section titled “Plays and playbooks”Plays are the context in which tasks are performed. While plays are somewhat of an ephemeral concept, playbooks are their physical representations: YAML files in which plays are defined.
Let’s look at an example of a playbook. The following playbook will install nginx and php packages on a managed node:
---- name: Install nginx and php hosts: www become: yes tasks: - name: Install nginx package: name: nginx state: present - name: Install php package: name: php8 state: present - name: Start nginx service: name: nginx state: startedThe first line (three dashes) marks the beginning of a new YAML document. The next line names the whole playbook. Playbook names should be short but descriptive. They are going to end up in logs and debugging information.
Next, we inform Ansible that this play should be run against nodes in the inventory placed in the www group. We also tell Ansible to use sudo when executing the commands. This is required as all distributions that we cover in our guide require root privileges to install and remove packages.
Then, we start the tasks section. Each task is given a name, the name of the module (command) we are going to use, and the command is given options and arguments. As you can see, the indentation declares the scope. If you are familiar with the Python programming language, this should be intuitive for you.
Before we run this playbook, let’s use a very useful tool, ansible-lint:
$ ansible-lint install.yamlWARNING: PATH altered to expand ~ in it. Read https://stackoverflow.com/a/44704799/99834 and correct your system configuration.WARNING Listing 9 violation(s) that are fatalyaml[trailing-spaces]: Trailing spacesinstall.yaml:1yaml[truthy]: Truthy value should be one of [false, true]install.yaml:4fqcn[action-core]: Use FQCN for builtin module actions (package).install.yaml:6 Use \`ansible.builtin.package\` or \`ansible.legacy.package\` instead.[...]yaml[empty-lines]: Too many blank lines (1 > 0)install.yaml:18Read documentation for instructions on how to ignore specific rule violations. Rule Violation Summary count tag profile rule associated tags 1 yaml[empty-lines] basic formatting, yaml 1 yaml[indentation] basic formatting, yaml 3 yaml[trailing-spaces] basic formatting, yaml 1 yaml[truthy] basic formatting, yaml 3 fqcn[action-core] production formattingFailed after min profile: 9 failure(s), 0 warning(s) on 1 files.I have cut part of the output for brevity, but you can see that the tool printed information about violations of YAML syntax and Ansible best practices. Failures are types of errors that will stop the execution of the playbook. Warnings are just that: the playbook will be executed, but there are some errors that go against best practices. Let’s correct our playbook, as follows:
---- name: Install nginx and php hosts: www become: true tasks: - name: Install nginx ansible.builtin.package: name: nginx state: present - name: Install php ansible.builtin.package: name: php8 state: present - name: Start nginx ansible.builtin.service: name: nginx state: startedWe can now run the playbook using the ansible-playbook command:
$ ansible-playbook -i inventory install.yamlPLAY [Install nginx and php] ********************************************************************TASK [Gathering Facts] ********************************************************************ok: [hostone]TASK [Install nginx] ********************************************************************changed: [hostone]TASK [Install php] ********************************************************************fatal: [hostone]: FAILED! => {"changed": false, "msg": "No package matching 'php8' is available"}PLAY RECAP ********************************************************************hostone : ok=2 changed=1 reachable=0 failed=1 skipped=0 rescued=0 ignored=0In the preceding output, we have instructed the ansible-playbook command to use an inventory file called inventory and run a playbook named install.yaml. The output should be self-explanatory: we are informed of the name of the play we run. Then, we see a list of managed nodes that Ansible will try to execute action against. Then, we see tasks and a list of nodes that the tasks succeeded or failed at. The nginx task was a success on hostone. However, installing php failed. Ansible gives us the exact reason for it: there is no php8 package available for our managed node. Sometimes, a resolution is pretty obvious, but sometimes it requires a bit of digging around. After checking with our distribution, we find out that the actual php package available there is php7.4. After quickly correcting the offending line, we run the playbook again, as follows:
$ ansible-playbook -i inventory install.yamlPLAY [Install nginx and php] ********************************************************************TASK [Gathering Facts] ********************************************************************ok: [hostone]TASK [Install nginx] ********************************************************************ok: [hostone]TASK [Install php] ********************************************************************changed: [hostone]TASK [Start nginx] ********************************************************************ok: [hostone]PLAY RECAP ********************************************************************hostone : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Notice the change in the output. First, Ansible tells us that nginx on hostone is okay. This means that Ansible was able to determine that the package was already installed, and it took no action. Then, it told us that the php7.4 installation was successful (changed: [hostone]) on the hostone server.
The preceding playbook is short, but we hope it demonstrates the usefulness of the tool. Playbooks are executed in a linear manner, from top to bottom.
There’s one problem with our playbook. While it only installs two packages, you may be worried about maintainability and readability if you need to install tens or hundreds of packages. Having a separate task for each of them is troublesome. There’s a solution. You can create a list of items that a given task will be executed for—something akin to a loop. Let’s edit the tasks part of the playbook, as follows:
tasks: - name: Install nginx ansible.builtin.package: name: '{{ item }}' state: present with_items: - nginx - php7.4 - gcc - g++ - name: Start nginx ansible.builtin.service: name: nginx state: startedWe have added two packages to demonstrate a more complete run. Notice the lack of a separate task for php7.4.
Before running this play, it is always a good idea to check it with ansible-lint. Here’s how we can do that:
$ ansible-lint install.yamlPassed with production profile: 0 failure(s), 0 warning(s) on 1 files.Now, after ansible-lint gives us the green light, let’s play this playbook:
$ ansible-playbook -i inventory install.yamlPLAY [Install nginx and php] ********************************************************************TASK [Gathering Facts] ********************************************************************ok: [hostone]TASK [Install nginx] ********************************************************************ok: [hostone] => (item=nginx)ok: [hostone] => (item=php7.4)changed: [hostone] => (item=gcc)changed: [hostone] => (item=g++)TASK [Start nginx] ********************************************************************ok: [hostone]PLAY RECAP ********************************************************************hostone : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Assume we want to install a page configuration in nginx. Ansible is able to copy files. We can set it to copy nginx virtual server configuration, but we only want to restart nginx once: at the service setup.
We can do it via notify and handlers. I’ll paste the whole playbook ahead:
---- name: Install nginx and php hosts: www become: true tasks: - name: Install nginx ansible.builtin.package: name: '{{ item }}' state: present with_items: - nginx - php7.4 - gcc - g++ notify: - Start nginx - name: Copy service configuration ansible.builtin.copy: src: "files/service.cfg" dest: "/etc/nginx/sites-available/service.cfg" owner: root group: root mode: '0640' - name: Enable site ansible.builtin.file: src: "/etc/nginx/sites-available/service.cfg" dest: "/etc/nginx/sites-enabled/default" state: link notify: - Restart nginx handlers: - name: Start nginx ansible.builtin.service: name: nginx state: started - name: Restart nginx ansible.builtin.service: name: nginx state: restartedNotice the whole new section related to copying files. We also create a symlink (an alias to a file) so that the site is enabled. The preceding example is incomplete. It takes more than that to actually set up a virtual server with nginx. We have shortened it for brevity’s sake and only to demonstrate a principle.
The execution of this play yields the following output:
$ ansible-playbook -i inventory install.yamlPLAY [Install nginx and php] ********************************************************************TASK [Gathering Facts] ********************************************************************ok: [hostone]TASK [Install nginx] ********************************************************************ok: [hostone] => (item=nginx)ok: [hostone] => (item=php7.4)ok: [hostone] => (item=gcc)ok: [hostone] => (item=g++)TASK [Copy service configuration] ********************************************************************ok: [hostone]TASK [Enable site] ********************************************************************ok: [hostone]PLAY RECAP ********************************************************************hostone ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0As soon as you start using Ansible in a real production environment, it becomes obvious that even with the looping technique we demonstrated previously, the playbook grows pretty fast and becomes unwieldy. Thankfully, there’s a way to break the playbook into smaller files.
One of the ways to do it is to segregate tasks into roles. As mentioned earlier, a role is a bit of an abstract concept, so the easiest way to think about it is to consider it a group. You group tasks depending on the criteria that are relevant to you. While the criteria are totally up to you, it is pretty common for roles to refer to the type of function a given managed node performs. One managed node can perform more than one function, but it still is probably wise to separate the functions into their own roles. Thus, the HTTP server would be one role, the database server would be another, and the file server would be yet another.
Roles in Ansible have a predetermined directory structure. There are eight standard directories defined, although you only need to create one.
The following code is copied from the Ansible documentation (https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html):
roles/ common/ # this hierarchy represents a "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # bar.txt # <-- files for use with the copy resource foo.sh # <-- script files for use with the script resource vars/ # main.yml # <-- variables associated with this role defaults/ # main.yml # <-- default lower priority variables for this role meta/ # main.yml # <-- role dependencies library/ # roles can also include custom modules module_utils/ # roles can also include custom module_utils lookup_plugins/ # or other types of plugins, like lookup in this caseThe roles directory exists on the same level as your playbook. The most common subdirectories you will probably see are tasks, templates, files, vars, and handlers. Within each subdirectory, Ansible will look for a main.yml, main.yaml, or main file (all of them have to be valid YAML files). Their contents will be automatically available to the playbook. So, how would this work in practice? Within the same directory that our install.yaml playbook exists, we will create a roles directory. Within this directory, we will create another one: www. Inside that one, we will create directories: tasks, files, and handlers. A similar structure will be created with development as a role:
roles/ www/ # this hierarchy represents a "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file files/ # service.cfg # <-- files for use with the copy resource development/ # this hierarchy represents a "role" tasks/ # main.yml # <-- tasks file can include smaller files if warrantedThe development role has only tasks subdirectory because we don’t require any additional bells and whistles. Why the development role, though? When you look back at our current playbook, you’ll notice that we mix up installing things for the www server and development packages—namely, compilers. It is bad practice, even if they will end up on the same physical server.
Thus, we are going to edit our inventory file so that we have two separate roles:
[www]hostone ansible_host=192.168.1.2 ansible_ssh_private_key_file=~/.ssh/hostone.pem ansible_user=admin[development]hostone ansible_host=192.168.1.3 ansible_ssh_private_key_file=~/.ssh/hostone.pem ansible_user=adminBoth groups contain only one node, which is a bad thing to do overall. We do it only for the purpose of this guide. Don’t install compilers and development software on your HTTP server, especially on the production one.
Now, we have to move some stuff around within the playbook. The install.yml file will become much shorter, as we can see here:
---- name: Install nginx and php hosts: www roles: - www become: true- name: Install development packages hosts: development roles: - development become: trueWe actually have two plays in this playbook. One of them is for www hosts, and the other is for development hosts. After we give the play a name and list host groups that we wish it to run against, we use keyword roles and list actual roles to be used. As you can see, Ansible is going to locate the proper roles automatically, as long as you adhere to the directory structure explained previously. There are ways to include roles directly by specifying their full path, but we’re not going to cover them here.
Now, for the www role, we’ll execute the following code:
---- name: Install nginx ansible.builtin.package: name: '{{ item }}' state: present with_items: - nginx - php7.4 notify: - Start nginx- name: Copy service configuration ansible.builtin.copy: src: "files/service.cfg" dest: "/etc/nginx/sites-available/service.cfg" owner: root group: root mode: '0640'- name: Enable site ansible.builtin.file: src: "/etc/nginx/sites-available/service.cfg" dest: "/etc/nginx/sites-enabled/default" state: link notify: - Restart nginxYou should notice several changes from the get-go:
- There is no
taskskeyword in this document. This is implicit by the fact that this is themain.yamlfile within thetaskssubdirectory. - Indentation has been moved left.
- There are no handlers here.
- We have removed the installation of the
gccandg++packages.
Let’s now see the handlers:
---- name: Start nginx ansible.builtin.service: name: nginx state: started listen: "Start nginx"- name: Restart nginx ansible.builtin.service: name: nginx state: restarted listen: "Restart nginx"The overall changes are the same, as noted here:
- We have removed the
handlerskeyword - We have moved the indentation to the left
Now, let’s see roles/development/tasks/main.yaml:
---- name: Install compilers ansible.builtin.package: name: '{{ item }}' state: present with_items: - gcc - g++This is very simple. We may have increased the complexity of the directory structure, but we have gained the simplicity of tasks and plays. The gains are well worth the trade-off, especially when your playbooks grow in size and complexity.
There are many advantages to using a CaC tool, as outlined here:
- Configuration can be linted—that is, checked for syntax errors by automation tools
- It can be applied infinitely with the same outcomes
- It can be run on many systems in parallel
- It can be kept in a version control system, such as Git, where the history of changes and comments for them are stored and can be viewed at any time
- It can be run by automation tools, removing the need for a human factor besides writing playbooks
In this subsection, we have shown how to write simple Ansible playbooks. We have explained what Ansible is and what the building blocks of the configuration scripts are. We have introduced playbooks, roles, and the inventory. There are many more things Ansible can do for you. You can manage devices, filesystems, users, groups, permissions, networking, and so on. The list of all modules Ansible is shipped with out of the box is impressive. You can always check the list at https://docs.ansible.com/ansible/latest/module_plugin_guide/index.html. Remember to check the list for your version of Ansible.
So, now that we’ve covered installing, configuring, and using Ansible to manage your servers (installing software, creating configuration files, and managing services), we are ready to look into Ansible Galaxy: community-developed modules that increase Ansible’s usefulness.
Ansible Galaxy
Section titled “Ansible Galaxy”Ansible is a powerful automation tool that enables users to configure, deploy, and manage complex IT infrastructures with ease. However, creating and maintaining Ansible playbooks can be time-consuming, especially when working with large-scale environments. Fortunately, Ansible Galaxy exists to help streamline this process by providing a centralized repository of pre-built roles and playbooks that can be easily integrated into an existing Ansible project.
Ansible Galaxy is a community-driven platform that hosts an extensive collection of Ansible roles and playbooks. These roles and playbooks are submitted by users from around the world and are reviewed and curated by Ansible’s maintainers. Ansible Galaxy provides a simple, efficient way to find and use pre-built automation content that can save users time and effort while ensuring quality and consistency.
Using Ansible Galaxy, users can quickly find, download, and use pre-built roles and playbooks for popular applications, services, and infrastructure components. These pre-built components can help speed up deployment times, ensure best practices are followed, and reduce the likelihood of errors or inconsistencies. Ansible Galaxy can also help users learn from others’ experiences and gain insights into the best practices of their peers.
Let’s use one of the Galaxy roles to install the nginx web server on our webserver role. In order to do that, we will need to install the role from Ansible Galaxy. First, ensure that Ansible is installed on your system by running the following command:
admin@myhome:~$ ansible-galaxy install nginxinc.nginxThis command will download and install the nginx role from Ansible Galaxy. By default, all roles installed are placed in the ~/.ansible/roles directory. It’s possible to change that by creating a global Ansible configuration file in your home directory: ~/.ansible.cfg.
An example of a configuration file changing the roles_path directory looks like this:
[defaults]roles_path = /home/admin/myansiblerolesA good practice is to pin role version numbers and put this version in a YAML file saved in the same Git repository where you will keep your Ansible playbooks. To achieve this, let’s create an ansible_requirements.yml file:
---- src: nginxinc.nginx version: 0.24.0To install the role from Ansible Galaxy using that file, you would run the following command:
admin@myhome:~$ ansible-galaxy install -r ansible_requirements.ymlOnce the role is installed, it can be used in an Ansible playbook by adding the following line to the playbook:
roles: - nginxinc.nginxHere’s an example playbook that uses the nginx role from Ansible Galaxy to install and configure nginx on a remote server:
---- name: Install and configure Nginx hosts: webservers become: true roles: - nginxinc.nginx vars: nginx_sites: myapp: template: "{{ playbook_dir }}/templates/myapp.conf.j2"In this playbook, we specify the webservers group as the target hosts and use the nginxinc.nginx role to install and configure nginx. We also define a variable called nginx_sites that specifies the configuration for a nginx server block that will be created using a Jinja2 template located in the playbook’s templates directory.
By using Ansible Galaxy and pre-built roles such as nginxinc.nginx, users can automate complex tasks quickly and reliably, ensuring consistency and reducing the risk of errors.
Handling secrets
Section titled “Handling secrets”Protecting secrets such as passwords, tokens, and certificates is crucial in any IT infrastructure. These secrets are the keys to accessing sensitive information and services, and their exposure can lead to severe security breaches. Therefore, it is crucial to keep them safe and secure. Ansible provides several methods for managing secrets, such as Ansible Vault, which allows users to encrypt and decrypt sensitive data using a password or key file. This feature helps to protect secrets and ensures that only authorized users have access to them.
Saving secrets in a Git repository or any other public place is a significant security risk. Such repositories are often accessible to multiple users, some of whom may not have the necessary permissions to access sensitive data. Additionally, version control systems such as Git retain the history of changes made to files, making it possible for secrets to be exposed inadvertently. This could happen if a user inadvertently commits secrets to a repository or if a hacker gains access to a repository’s commit history. Therefore, it is vital to discourage saving secrets in public places to reduce the risk of unauthorized access. Instead, Ansible provides secure ways to manage secrets, ensuring that they are encrypted and only accessible to authorized users. By doing so, users can be confident that their secrets are safe and secure.
Ansible Vault
Section titled “Ansible Vault”Ansible Vault is a feature provided by Ansible that enables users to encrypt and decrypt sensitive data, such as passwords, keys, and certificates. The vault creates an encrypted file that can be decrypted only by authorized users, ensuring that sensitive data remains secure. The vault can be used to store secrets in files, variables, or other sources that are used by Ansible.
Ansible Vault uses a variety of encryption methods to secure the secrets stored within it. By default, Ansible Vault uses AES 256 encryption, a widely accepted and secure encryption algorithm. Additionally, Ansible Vault supports other encryption algorithms, such as AES 192 and AES 128, providing flexibility in the encryption strength used to secure secrets. When encrypting data with Ansible Vault, users can choose to encrypt it with a password or with a key file. This ensures that only authorized users who possess the password or key file can decrypt the secrets stored within the vault.
To create a new vault using Ansible Vault, you can use the following command:
admin@myhome:~$ ansible-vault create secrets.ymlNew Vault password:Confirm New Vault password:This will create a new encrypted vault file called secrets.yml. You will be prompted to enter a password to encrypt the file. Once you’ve entered the password, the vault file will be created and opened in your default editor. Here’s an example secrets.yml file:
somesecret: pleaseEncryptMesecret_pgsql_password: veryPasswordyPasswordTo edit this secret file, you’ll need to use the ansible-vault edit secrets.yml command and type the encryption password afterward.
To write an Ansible task that reads the pgsql_password secret from the vault, you can use the ansible.builtin.include_vars module with the vault_password_file parameter. Here is an example task:
- name: Read pgsql_password from Ansible Vault include_vars: file: secrets.yml vault_password_file: /path/to/vault/password/file vars: pgsql_password: "{{ secret_pgsql_password }}"In this task, we’re using the include_vars module to read the variables from the secrets.yml vault file. The vault_password_file parameter specifies the location of the file containing the password to decrypt the vault. We then assign the value of secret_pgsql_password to the pgsql_password variable, which can be used elsewhere in the playbook.
Note that the secret_pgsql_password variable should be defined in the vault. The secret_ prefix is to indicate that the password was retrieved from the vault. Ansible does not differentiate between regular variables or secret variables.
When you run this playbook with an increased Ansible debug level, you will notice that the PostgreSQL password is exposed in the debug output. To prevent that, every task dealing with sensitive information can be executed with the no_log: True option enabled.
Secrets OPerationS (SOPS) is an open source tool developed by Mozilla that allows users to securely store and manage their secrets within various configuration files, including Ansible playbooks. SOPS uses a hybrid encryption approach, which means that it combines symmetric and asymmetric encryption to ensure maximum security.
SOPS encrypts secrets using a master key, which can be either symmetric or asymmetric. Symmetric encryption uses a password or passphrase to encrypt and decrypt secrets, while asymmetric encryption uses a pair of keys, one public and one private, to encrypt and decrypt secrets. SOPS encrypts the master key using key wrapping, a technique that encrypts the key with another key. In SOPS, the key that is used for key wrapping is often an AWS Key Management Service (KMS) key, but it can also be a Pretty Good Privacy (PGP) key or a Google Cloud KMS key.
SOPS integrates seamlessly with Ansible and supports various file formats, including YAML, JSON, and INI. It also supports various cloud providers, including AWS, Google Cloud, and Azure, making it a versatile tool for managing secrets across different environments.
Here’s an example of how to create a SOPS-encrypted YAML file and load its contents into an Ansible playbook using community.sops.load_vars.
First, create a YAML file named secrets.yaml with the following content:
postgresql_password: So.VerySecretThen, use SOPS to encrypt the file using the following command:
admin@myhome:~$ sops secrets.yaml > secrets.sops.yamlThis will create an encrypted version of the secrets.yaml file named secrets.sops.yaml. It’s safe to remove the secrets.yaml plain text file now. The most common mistake is leaving these files behind and committing them to the Git repository.
Next, create a new Ansible playbook named database.yml with the following content:
---- hosts: dbserver become: true vars: postgresql_password: "{{ lookup('community.sops.load_vars', 'secrets.sops.yaml')['postgresql_password'] }}" tasks: - name: Install PostgreSQL apt: name: postgresql state: present - name: Create PostgreSQL user and database postgresql_user: db: mydatabase login_user: postgres login_password: "{{ postgresql_password }}" name: myuser password: "{{ postgresql_password }}" state: presentIn this example, we’re using community.sops.load_vars to load the postgresql_password variable from the encrypted secrets.sops.yaml file. The postgresql_password variable is then passed to the postgresql_user task using the {{ postgresql_password }} Jinja2 syntax.
When you run this playbook with Ansible, it will decrypt the secrets.sops.yaml file using SOPS and load the postgresql_password variable into the postgresql_password variable in the playbook. This ensures that the password is not stored in plain text in the playbook, providing an extra layer of security for your sensitive information.
More information about SOPS is available in its official GitHub repository: https://github.com/mozilla/sops.
Other solutions
Section titled “Other solutions”There are several alternatives to Ansible Vault and SOPS that you can use to manage your sensitive data securely, as follows:
- HashiCorp Vault: An open source tool for securely storing and accessing secrets. It provides a service for secure and centralized storage of secrets, access control, and auditing.
- Blackbox: A command-line utility that encrypts and decrypts files using GNU Privacy Guard (GPG). It works by creating a separate GPG key for each user or team that needs access to the encrypted data.
- Keywhiz: Another open source secrets management system that provides a simple way to store and distribute secrets securely. It includes a web interface for managing secrets and a command-line tool for accessing them.
- Azure Key Vault, AWS Secrets Manager, or Google Cloud Secret Manager: Solutions you might want to consider keeping secrets in when dealing with a cloud environment.
This is, of course, not a complete list of all available options.
In this section, we introduced Ansible Vault and SOPS as means to handle secrets such as passwords. In the next section, we will be introducing a graphical frontend (GUI) to Ansible.
Ansible Tower and alternatives
Section titled “Ansible Tower and alternatives”Ansible Tower provides a centralized platform for managing Ansible automation workflows, making it easier for IT teams to collaborate, share knowledge, and maintain their infrastructure. Some of its key features include a web-based interface for managing Ansible playbooks, inventories, and job runs, role-based access control (RBAC) for managing user permissions, a built-in dashboard for monitoring job status and results, and an API for integrating with other tools and platforms.
It was first released in 2013 by Ansible, Inc. (now part of Red Hat), and has since become one of the most popular tools for automating IT workflows.
Since its initial release, Ansible Tower has undergone numerous updates and enhancements, including support for more complex automation workflows, integration with cloud platforms such as AWS and Azure, and improved scalability and performance. Ansible Tower is a commercial product shipped by the Red Hat company. The closest alternative to Ansible Tower is Ansible WorX (AWX).
Ansible AWX is an open source alternative to Ansible Tower, offering many of the same features as Tower but with a greater degree of customization and flexibility. AWX was first released in 2017 and has since become a popular choice for organizations looking to implement Ansible automation at scale without the cost of a commercial license.
One of the primary differences between Ansible Tower and Ansible AWX is their licensing model. While Ansible Tower requires a commercial license and is sold as part of Red Hat Ansible Automation Platform, Ansible AWX is open source and freely available for download from the Ansible website. This means that organizations can deploy AWX on their own infrastructure and customize it to their specific needs, rather than relying on a pre-built commercial solution.
Feature-wise, Ansible Tower and Ansible AWX are quite similar, with both platforms offering a web-based interface for managing Ansible playbooks, inventories, and job runs, RBAC for managing user permissions, and a built-in dashboard for monitoring job status and results. However, Ansible Tower does offer some additional features not found in Ansible AWX, such as native integration with Red Hat Ansible Automation Platform, advanced analytics and reporting, and certified modules and collections.
Another open source alternative to Ansible Tower is Ansible Semaphore. Similar to Tower, it is a web-based application designed to simplify the management of Ansible playbooks and projects. It is an open source, free, and easy-to-use alternative to Ansible Tower that allows users to easily automate their infrastructure tasks without the need for extensive coding knowledge. The first release of Ansible Semaphore was in 2016, and since then it has become a popular choice for those who want a simple yet powerful web-based interface for managing their Ansible automation workflows.
You can read more about these alternatives on their respective websites:
- Ansible Tower: https://access.redhat.com/products/ansible-tower-red-hat
- Ansible AWX: https://github.com/ansible/awx
- Ansible Semaphore: https://www.ansible-semaphore.com/
Advanced topics
Section titled “Advanced topics”In this section, we will show you how to handle advanced Ansible features and techniques for debugging and automatically checking your playbooks for possible errors.
Debugging
Section titled “Debugging”In order to debug issues with your Ansible playbook runs, it is often useful to increase the verbosity level to get more detailed output about what Ansible is doing. Ansible has four verbosity levels: -v, -vv, -vvv, and -vvvv. The more v s you add, the more verbose the output becomes.
By default, Ansible runs with -v, which provides basic information about the tasks that are executed. However, if you are experiencing issues with your playbook, it may be helpful to increase the verbosity level to get more detailed output. For example, using - vv will provide additional information about the playbooks, roles, and tasks that are being executed, while using -vvv will also show the tasks that Ansible is skipping.
To increase the verbosity level of an Ansible playbook run, simply add one or more -v options to the ansible-playbook command. Here’s an example:
admin@myhome:~$ ansible-playbook playbook.yml -vvThis will run the playbook with verbose output at the -vvlevel. If you need even more verbose output, you can add extra -v options. You can see an example here:
admin@myhome:~$ ansible-playbook playbook.yml -vvvIn addition to using the -v options, you can also set the verbosity level in your ansible.cfg file by adding the following lines:
[defaults]verbosity = 2This will set the verbosity level to -vv for all ansible-playbook commands. You can change the value to 3 or 4 to increase the verbosity level even further.
If you need to add some custom communication (such as printing a variable) in a verbose mode, you can do it by using the debug task.
Here’s an example playbook that demonstrates how to print out a variable in -vv debug mode:
---- hosts: all gather_facts: true vars: my_variable: "Hello, World!" tasks: - name: Print variable in debug mode debug: msg: "{{ my_variable }}" verbosity: 2In this playbook, we define a my_variable variable that holds the "Hello, World!" string. Then, we use the debug module to print out the value of this variable using the msg parameter.
The verbosity: 2 line is what enables debug mode. This tells Ansible to increase the verbosity level to -vv, which will show us the output of the debug module.
Linting Ansible playbooks
Section titled “Linting Ansible playbooks”Ansible code linting is the process of analyzing and verifying the syntax and style of Ansible code to ensure that it conforms to best practices and standards. The purpose of linting is to catch potential errors or issues before the code is executed, which can save time and effort in the long run.
The most common tool used for Ansible code linting is ansible-lint. This is an open source command-line tool that analyzes Ansible playbooks and roles for potential problems and provides suggestions for improvement.
To run ansible-lint against a sample playbook, you can execute the following command in the terminal:
admin@myhome:~$ ansible-lint sample-playbook.ymlAssuming the sample playbook is saved as sample-playbook.yml in the current working directory, its content looks like this:
---- name: (Debian/Ubuntu) {{ (nginx_setup == 'uninstall') | ternary('Remove', 'Configure') }} NGINX repository ansible.builtin.apt_repository: filename: nginx repo: "{{ item }}" update_cache: true mode: "0644" state: "{{ (nginx_state == 'uninstall') | ternary('absent', 'present') }}" loop: "{{ nginx_repository | default(nginx_default_repository_debian) }}" when: nginx_manage_repo | bool- name: (Debian/Ubuntu) {{ (nginx_setup == 'uninstall') | ternary('Unpin', 'Pin') }} NGINX repository ansible.builtin.blockinfile: path: /etc/apt/preferences.d/99nginx create: true block: | Package: * Pin: origin nginx.org Pin: release o=nginx Pin-Priority: 900 mode: "0644" state: "{{ (nginx_state == 'uninstall') | ternary('absent', 'present') }}" when: nginx_repository is not defined- name: (Debian/Ubuntu) {{ nginx_setup | capitalize }} NGINX ansible.builtin.apt: name: nginx{{ nginx_version | default('') }} state: "{{ nginx_state }}" update_cache: true allow_downgrade: "{{ omit if ansible_version['full'] is version('2.12', '<') else true }}" ignore_errors: "{{ ansible_check_mode }}" notify: (Handler) Run NGINXNote that the output of ansible-lint may vary depending on the version of Ansible and ansible-lint being used, as well as the specific rules enabled.
As an example, the following is the output of running ansible-lint against the sample playbook provided:
[WARNING]: empty path for ansible.builtin.blockinfile, path set to ''[WARNING]: error loading version info from /usr/lib/python3.10/site-packages/ansible/modules/system/setup.py: __version__ = '2.10.7'[WARNING]: 3.0.0 includes an experimental document syntax parser that could result in parsing errors for documents that used the previous parser. Use \`--syntax-check\` to verify new documents before use or consider setting \`document_start_marker\` to avoid using the experimental parser.sample-playbook.yml:1:1: ELL0011: Trailing whitespacesample-playbook.yml:7:1: ELL0011: Trailing whitespacesample-playbook.yml:9:1: ELL0011: Trailing whitespacesample-playbook.yml:17:1: ELL0011: Trailing whitespacesample-playbook.yml:22:1: ELL0011: Trailing whitespacesample-playbook.yml:26:1: ELL0011: Trailing whitespacesample-playbook.yml:29:1: ELL0011: Trailing whitespacesample-playbook.yml:35:1: ELL0011: Trailing whitespacesample-playbook.yml:38:1: ELL0011: Trailing whitespacesample-playbook.yml:41:1: ELL0011: Trailing whitespacesample-playbook.yml:44:1: ELL0011: Trailing whitespacesample-playbook.yml:47:1: ELL0011: Trailing whitespacesample-playbook.yml:50:1: ELL0011: Trailing whitespacesample-playbook.yml:53:1: ELL0011: Trailing whitespaceThe output indicates that the playbook contains trailing whitespace on several lines, which violates the ELL0011 rule of ansible-lint. The warning messages regarding the empty path and version info loading are not critical issues and can be safely ignored.
To fix the trailing whitespace issue, simply remove the extra spaces at the end of each affected line. Once the issues are fixed, you can rerun ansible-lint to ensure that there are no further problems with the playbook.
Speeding up SSH connections
Section titled “Speeding up SSH connections”Ansible is an open source automation tool that is widely used to deploy and manage IT infrastructure. One of the key features of Ansible is its ability to use SSH for secure communication with remote servers. However, using SSH for every single task can be time-consuming and can impact performance, especially if you’re dealing with a lot of servers. To address this issue, Ansible supports SSH multiplexing, which allows multiple SSH connections to share a single TCP connection.
SSH multiplexing works by reusing the existing SSH connection rather than creating a new one for every task. When Ansible establishes an SSH connection to a remote server, it opens a TCP socket and creates a control socket for that connection. A control socket is a special socket that is used to manage the SSH connection. When another SSH connection is requested to the same host, Ansible checks whether a control socket already exists for that connection. If it does, Ansible reuses the existing control socket and creates a new channel within the same SSH connection for the new task.
The benefits of SSH multiplexing are that it saves time and resources by reducing the number of SSH connections that Ansible has to establish. Additionally, it can improve the performance of Ansible by reducing the overhead of creating and tearing down SSH connections for every task.
To enable SSH multiplexing in Ansible, you need to configure the ControlMaster and ControlPath options in the SSH client configuration file. The ControlMaster option enables the use of SSH multiplexing, while the ControlPath option specifies the location of the control socket. By default, Ansible uses the ~/.ansible/cp directory to store control sockets. You can also configure the maximum number of SSH connections that can be multiplexed simultaneously by setting the ControlPersist option.
To customize the SSH multiplexing configuration, you can put SSH options into your default Ansible configuration file placed in ~/.ansible.cfg by adding the [ssh_connection] section, as follows:
[ssh_connection]ssh_args = -o ControlMaster=auto -o ControlPersist=3600control_path = ~/.ssh/multiplexing/ansible-ssh-%%r@%%h:%%pMake sure to also create a ~/.ssh/multiplexing directory after adding this configuration.
The ControlMaster=auto option creates a master session automatically, and if there is a master session already available, subsequent sessions are automatically multiplexed. Setting ControlPersist=3600 will leave the master connection open in the background to accept new connections for 3600 seconds (1 hour).
Dynamic inventory
Section titled “Dynamic inventory”In cloud environments such as AWS, servers can be created and terminated dynamically based on demand. Managing these servers manually can be a daunting task, which is why automation tools such as Ansible are essential. One of the critical features of Ansible that makes it well suited for cloud environments is dynamic inventory.
Dynamic inventory is an Ansible feature that enables the automatic discovery of hosts (servers) and groups (tags) in a cloud environment. In AWS, Ansible can use the Elastic Compute Cloud (EC2) inventory plugin to query the AWS API to retrieve information about EC2 instances and groups.
To use dynamic inventory in AWS, you need to configure the EC2 inventory plugin in your Ansible configuration.
The amazon.aws.aws_ec2 inventory plugin is an official Ansible plugin that enables dynamic inventory for Amazon EC2 instances. To use this plugin, you need to follow the steps set out next.
Depending on which version of Ansible you’re using and whether you’ve installed full Ansible (not only Ansible Core), you might need to use Ansible Galaxy to install an AWS collection plugin, as follows:
admin@myhome:~$ ansible-galaxy collection install amazon.awsInstall the boto3 and botocore libraries on your Ansible control node. You can install them using the pip package manager, like so:
admin@myhome:~$ pip install boto3 botocoreCreate an Identity and Access Management (IAM) user with the necessary permissions to access EC2 instances and groups. You can create an IAM user using the AWS Management Console or the AWS CLI. Make sure to save the IAM user’s access key and secret access key.
Create an AWS credentials file (~/.aws/credentials) on your Ansible control node and add the IAM user’s access key and secret access key, as follows:
[default]aws_access_key_id = YOUR_ACCESS_KEYaws_secret_access_key = YOUR_SECRET_KEYCreate an Ansible inventory file (inventory.yml) in your project directory and configure it to use the amazon.aws.aws_ec2 plugin, like so:
plugin: amazon.aws.aws_ec2regions: - eu-central-1filters: tag:Environment: - webserver - frontendHere’s a brief explanation of the configuration options:
plugin: Specifies the inventory plugin to useregions: Specifies the AWS regions to search for instances infilters: Allows you to filter EC2 instances by tags
Test the inventory by running the following command:
admin@myhome:~$ ansible-inventory -i inventory.yml --listThis command should output a JSON object that lists all the EC2 instances in the specified regions, grouped by their Ansible tags.
This way, you won’t have to update your inventory file on every Ansible playbook run to make sure you have an up-to-date servers list.
Summary
Section titled “Summary”In this chapter, we have presented you with the Ansible CaC tool. We have explained and demonstrated how moving configuration, from tribal knowledge and documents (as well as describing steps required to get your system to a desired state) to tools that can implement said configuration based on a well-defined syntax brings benefits to your organization, such as repeatability, ability to run many configurations in parallel, automated tests, and execution.
In the next chapter, we are going to introduce you to Infrastructure as Code (IaC).
Further reading
Section titled “Further reading”- Mastering Ansible, Fourth Edition by James Freeman and Jesse Keating
- Ansible Playbook Essentials by Gourav Shah
- Ansible for Real-Life Automation by Gineesh Madapparambath