Reader small image

You're reading from  Mastering Ansible, 4th Edition - Fourth Edition

Product typeBook
Published inDec 2021
PublisherPackt
ISBN-139781801818780
Edition4th Edition
Right arrow
Authors (2):
James Freeman
James Freeman
author image
James Freeman

James Freeman is an accomplished IT professional with over 25 years' experience in the technology industry. He has more than a decade of first-hand experience in solving real-world enterprise problems in production environments using Ansible, open source, and AWS. As part of this work, he frequently introduces Ansible as a new technology to businesses and CTOs for the first time. In addition, he has co-authored five books and one video training course on Ansible, facilitated bespoke Ansible workshops and training sessions, and presented at both international conferences and meetups on Ansible.
Read more about James Freeman

Jesse Keating
Jesse Keating
author image
Jesse Keating

Jesse Keating is an accomplished Ansible user, contributor, and presenter. He has been an active member of the Linux and open source community for over 15 years. He has firsthand experience involving a variety of IT activities, software development, and large-scale system administration. He has presented at numerous conferences and meetups, and has written many articles on a variety of topics.
Read more about Jesse Keating

View More author details
Right arrow

Chapter 12: Infrastructure Provisioning

Almost everything in data centers is becoming software-defined, from networks to the server infrastructure on which our software runs. Infrastructure as a Service (IaaS) providers offer APIs for programmatically managing images, servers, networks, and storage components. These resources are often expected to be created just-in-time, in order to reduce costs and increase efficiency.

As a result, a great deal of effort has gone into the cloud provisioning aspect of Ansible over the years, with more than 30 infrastructure providers catered for in the official Ansible release. These range from open source solutions such as OpenStack and oVirt to proprietary providers such as VMware and cloud providers such as AWS, GCP, and Azure. 

There are more use cases than we can cover in this chapter, but nonetheless, we will explore the following ways in which Ansible can interact with a variety of these services:

    ...

Technical requirements

To follow the examples presented in this chapter, you will need a Linux machine running Ansible 4.3 or newer. Almost any flavor of Linux should do – for those interested in specifics, all the code presented in this chapter was tested on Ubuntu Server 20.04 LTS unless states otherwise, and on Ansible 4.3. The example code that accompanies this chapter can be downloaded from GitHub at this URL: https://github.com/PacktPublishing/Mastering-Ansible-Fourth-Edition/tree/main/Chapter12.

Check out the following video to see the Code in Action:https://bit.ly/3BU6My2

Managing an on-premise cloud infrastructure

The cloud is a popular but vague term, used to describe IaaS. There are many types of resources that can be provided by a cloud, although the most commonly discussed are compute and storage. Ansible is capable of interacting with numerous cloud providers in order to discover, create, or otherwise manage resources within them. Note that although we will focus on the compute and storage resources in this chapter, Ansible has a module for interacting with many more cloud resource types, such as load balancers, and even cloud role-based access controls.

One such cloud provider that Ansible can interact with is OpenStack (an open source cloud operating system), and this is a likely solution for those with a need for on-premise IaaS functionality. A suite of services provides interfaces to manage compute, storage, and networking services, plus many other supportive services. There is not a single provider of OpenStack; instead...

Managing a public cloud infrastructure

The management of public cloud infrastructures with Ansible is no more difficult than the management of OpenStack with it, as we covered earlier. In general, for any IaaS provider supported by Ansible, there is a three-step process to getting it working:

  1. Establish the Ansible collections, modules, and inventory plugins available to support the cloud provider.
  2. Install any prerequisite software or libraries on the Ansible host.
  3. Define the playbook and run it against the infrastructure provider.

There are dynamic inventory plugins readily available for most providers too, and we have already demonstrated two in this book:

  • amazon.aws.aws_ec2 was discussed in Chapter 1The System Architecture and Design of Ansible.
  • openstack.cloud.openstack was demonstrated earlier in this chapter.

Let's take a look at Amazon Web Services (AWS), and specifically, the EC2 offering. We can...

Interacting with Docker containers

Linux container technologies, especially Docker, have grown in popularity in recent years, and this has continued since the previous edition of this book was published. Containers provide a fast path to resource isolation while maintaining the consistency of the runtime environment. They can be launched quickly and are efficient to run, as there is very little overhead involved. Utilities such as Docker provide a lot of useful tooling for container management, such as a registry of images to use as the filesystem, tooling to build the images themselves, clustering orchestration, and so on. Through its ease of use, Docker has become one of the most popular ways to manage containers, though others, such as Podman and LXC, are becoming much more prevalent. For now, though, we will focus on Docker, given its broad appeal and wide install base.

Ansible can interact with Docker in numerous ways as well. Notably, Ansible can be used to build...

Building containers with Ansible

As we mentioned at the beginning of the previous section, the world of containers has moved on greatly since the previous edition of this book was published. Although Docker is still a massively popular container technology, new and improved technologies have become favored, and indeed natively integrated into Linux operating systems. Canonical (the publisher of Ubuntu) is championing the LXC container environment, while Red Hat (the owner of Ansible) is championing Buildah and Podman.

If you read the third edition of this book, you will know that we covered a technology called Ansible Container, which was used to directly integrate Ansible with Docker and remove the need for glue steps such as adding hosts to the in-memory inventory, having two separate plays for instantiating the container, and building the container image contents. Ansible Container has now been deprecated, and all development work has ceased (according to their GitHub page...

Summary

DevOps has pushed automation in many new directions, including the containerization of applications, and even the creation of infrastructure itself. Cloud computing services enable self-service management of fleets of servers for running services. Ansible can easily interact with these services to provide the automation and orchestration engine.

In this chapter, you learned how to manage on-premises cloud infrastructures, such as OpenStack, using Ansible. We then extended this with examples of public cloud infrastructure provision on both AWS and Microsoft Azure. Finally, you learned how to interact with Docker using Ansible, and how to neatly package Docker service definitions using Ansible Container.

Ansible can start just about any host, except for the one that it is running on, and with proper credentials, it can create the infrastructure that it wants to manage, either for one-off actions or to deploy a new version of an application into a production container management...

Questions

  1. When creating or deleting VM instances on OpenStack, which inventory host should you reference in your play?

    a) The OpenStack host

    b) localhost

    c) The VM Floating IP address

    d) None of the above

  2. How would you reference a newly created virtual machine in a second play without having to use a dynamic inventory script?

    a) Use ansible.builtin.raw commands.

    b) Use ansible.builtin.shell commands.

    c) Use ansible.builtin.add_host to add the new VM to the in-memory inventory.

    d) You need to use the dynamic inventory plugin.

  3. You can still run dynamic inventory scripts directly in Ansible 4.x and newer, just as you could in Ansible 2.x releases.

    a) True

    b) False

  4. To use a dynamic inventory script, and set its parameters, you would now (assuming the collection is already installed):

    a) Define a YAML inventory file with the plugin name and parameters.

    b) Reference the dynamic inventory script in the -i parameter of ansible/ansible-playbook.

    c) Put the plugin name in your play definition...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Ansible, 4th Edition - Fourth Edition
Published in: Dec 2021Publisher: PacktISBN-13: 9781801818780
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 €14.99/month. Cancel anytime

Authors (2)

author image
James Freeman

James Freeman is an accomplished IT professional with over 25 years' experience in the technology industry. He has more than a decade of first-hand experience in solving real-world enterprise problems in production environments using Ansible, open source, and AWS. As part of this work, he frequently introduces Ansible as a new technology to businesses and CTOs for the first time. In addition, he has co-authored five books and one video training course on Ansible, facilitated bespoke Ansible workshops and training sessions, and presented at both international conferences and meetups on Ansible.
Read more about James Freeman

author image
Jesse Keating

Jesse Keating is an accomplished Ansible user, contributor, and presenter. He has been an active member of the Linux and open source community for over 15 years. He has firsthand experience involving a variety of IT activities, software development, and large-scale system administration. He has presented at numerous conferences and meetups, and has written many articles on a variety of topics.
Read more about Jesse Keating