Reader small image

You're reading from  Mastering Linux Administration - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837630691
Edition2nd Edition
Right arrow
Authors (2):
Alexandru Calcatinge
Alexandru Calcatinge
author image
Alexandru Calcatinge

Alexandru Calcatinge is an open-minded architect with a background in computer science and mathematics. He is a senior university lecturer with a PhD in urban planning from Ion Mincu University of Architecture and Urban Planning and a postgraduate degree in DevOps from Caltech's Center for Technology and Management Education (CTME). He teaches students about architectural programming and development and open source technologies. He has authored five books on architecture and urban planning and numerous scientific articles on urban and rural development. Alex was certified as a Linux trainer in 2017. He loves the DevOps philosophy and the possibilities that cloud technologies bring for the future. He is also a certified programming analyst, computer network administrator, trainer, designer, and life coach.
Read more about Alexandru Calcatinge

Julian Balog
Julian Balog
author image
Julian Balog

Julian Balog is a senior software engineer with more than 15 years of experience in the industry. Currently, his work primarily focuses on application delivery controllers, containerized workflows, networking, and security. With a never-ending passion for Linux and open-source technologies, Julian is always in pursuit of learning new things while solving problems and making things work through simple, efficient, and practical engineering. He lives with his wife, two children, and an Aussie-doodle in the greater Seattle area, Washington.
Read more about Julian Balog

View More author details
Right arrow

Infrastructure and Automation with Ansible

If your day-to-day system administration or development work involves tedious and repetitive operations, Ansible could help you run your tasks while saving you precious time. Ansible is a tool for automating software provisioning, configuration management, and application deployment workflows. Initially developed by Michael DeHaan in 2012, Ansible was acquired by Red Hat in 2015 and is now maintained as an open source project.

In this chapter, you’ll learn about the fundamental concepts of Ansible, along with a variety of hands-on examples. In particular, we’ll explore the following topics:

  • Introducing Ansible architecture and configuration management
  • Installing Ansible
  • Working with Ansible

Technical requirements

First, you should be familiar with the Linux command-line Terminal in general. Intermediate knowledge of Linux will help you understand some of the intricacies of the practical illustrations used throughout this chapter. You should also be proficient in using a Linux-based text editor.

For the hands-on examples, we recommend setting up a lab environment similar to the one we’re using. To replicate this environment, your CPU will need to have at least 6 physical cores and 6 virtual cores (12 in total). A quad-core CPU with hyper-threading is not enough. Also, OpenSSH should be installed on all the hosts. You’ll find related instructions in the Setting up the lab environment section of this chapter. If you don’t configure a lab environment, you will still benefit from the detailed explanations associated with the practical examples in this chapter.

Now, let’s start our journey by covering introductory concepts surrounding Ansible...

Introducing Ansible architecture and configuration management

In the introduction to this chapter, we captured one of the essential aspects of Ansible – it’s a tool for automating workflows. Almost any Linux system administration task can be automated using Ansible. Using the Ansible CLI, we can invoke simple commands to change the desired state of a system. Usually, with Ansible, we execute tasks on a remote host or a group of hosts.

Let’s use the classic illustration of package management. Suppose you’re managing an infrastructure that includes a group of web servers, and you plan to install the latest version of a web server application (Nginx or Apache) on all of them. One way to accomplish this task is to SSH into each host and run the related shell commands to install the latest web server package. If you have a lot of machines, this will be a big task. You could argue that you can write a script to automate this job. This is possible, but then...

Installing Ansible

In this section, we’ll show you how to install Ansible on a control node. On Linux, we can install Ansible in a couple of ways:

  • Using the platform-specific package manager (for example, apt on Ubuntu/Debian)
  • Using pip, the Python package manager

The Ansible community recommends pip for installing Ansible since it provides the most recent stable version of Ansible. In this section, we’ll use Ubuntu as our distribution of choice. For a complete Ansible installation guide for all major OS platforms, please follow the online documentation at https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html.

On the control node, Ansible requires Python, so before installing Ansible, we need to make sure we have Python installed on our system.

Important note

Python 2 is no longer supported as of January 1, 2020. Please use Python 3.8 (or newer) instead.

Let’s start by installing Ansible on Ubuntu.

...

Working with Ansible

In this section, we’ll use the Ansible CLI tools extensively to perform various configuration management tasks. To showcase our practical examples, we’ll work with a custom lab environment, and we highly encourage you to reproduce it for a complete configuration management experience.

Here’s a high-level outline of this section:

  • Setting up the lab environment
  • Configuring Ansible
  • Using Ansible ad hoc commands
  • Using Ansible playbooks
  • Using templates with Jinja2
  • Using Ansible roles

Let’s start with an overview of the lab environment.

Setting up the lab environment

Our lab uses Kernel-based Virtual Machine (KVM) as a hypervisor for the virtual environment, but any other hypervisor will do. Chapter 11, Working with Virtual Machines, describes the process of creating Linux virtual machines (VMs) in detail. We deployed the following VMs using Ubuntu Server LTS to mimic a real-world configuration management...

Summary

In this chapter, we covered significant ground in terms of Ansible. Due to this chapter’s limited scope, we couldn’t capture all of Ansible’s vast number of features. However, we tried to provide an overarching view of the platform, from Ansible’s architectural principles to configuring and working with ad hoc commands and playbooks. You learned how to set up an Ansible environment, with several managed hosts and a control node, thereby emulating a real-world deployment at a high level. You also became familiar with writing Ansible commands and scripts for typical configuration management tasks. Most of the commands and playbooks presented throughout this chapter closely resemble everyday administrative operations.

Whether you are a systems administrator or a DevOps engineer, a seasoned professional, or on the way to becoming one, we hope this chapter brought new insights to your everyday Linux administration tasks and automation workflows. The...

Questions

Let’s try to wrap up some of the essential concepts we learned about in this chapter by completing the following quiz:

  1. What are idempotent operations or commands in Ansible?
  2. You want to set up passwordless authentication with your managed hosts. What steps should you follow?
  3. What is the ad hoc command for checking communication with all your managed hosts?
  4. Enumerate a few Ansible modules. Try to think of a configuration management scenario where you could use each module.
  5. Think of a simple playbook that monitors the memory that’s available on your hosts and will notify you if that memory is above a given threshold.

Further reading

Here are a few resources we found helpful for learning more about Ansible internals:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Linux Administration - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837630691
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Alexandru Calcatinge

Alexandru Calcatinge is an open-minded architect with a background in computer science and mathematics. He is a senior university lecturer with a PhD in urban planning from Ion Mincu University of Architecture and Urban Planning and a postgraduate degree in DevOps from Caltech's Center for Technology and Management Education (CTME). He teaches students about architectural programming and development and open source technologies. He has authored five books on architecture and urban planning and numerous scientific articles on urban and rural development. Alex was certified as a Linux trainer in 2017. He loves the DevOps philosophy and the possibilities that cloud technologies bring for the future. He is also a certified programming analyst, computer network administrator, trainer, designer, and life coach.
Read more about Alexandru Calcatinge

author image
Julian Balog

Julian Balog is a senior software engineer with more than 15 years of experience in the industry. Currently, his work primarily focuses on application delivery controllers, containerized workflows, networking, and security. With a never-ending passion for Linux and open-source technologies, Julian is always in pursuit of learning new things while solving problems and making things work through simple, efficient, and practical engineering. He lives with his wife, two children, and an Aussie-doodle in the greater Seattle area, Washington.
Read more about Julian Balog