Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Practical Ansible - Second Edition

You're reading from  Practical Ansible - Second Edition

Product type Book
Published in Sep 2023
Publisher Packt
ISBN-13 9781805129974
Pages 420 pages
Edition 2nd Edition
Languages
Authors (3):
James Freeman James Freeman
Profile icon James Freeman
Fabio Alessandro Locati Fabio Alessandro Locati
Profile icon Fabio Alessandro Locati
Daniel Oh Daniel Oh
Profile icon Daniel Oh
View More author details

Table of Contents (21) Chapters

Preface 1. Part 1:Learning the Fundamentals of Ansible
2. Chapter 1: Getting Started with Ansible 3. Chapter 2: Understanding the Fundamentals of Ansible 4. Chapter 3: Defining Your Inventory 5. Chapter 4: Playbooks and Roles 6. Part 2:Expanding the Capabilities of Ansible
7. Chapter 5: Creating and Consuming Modules 8. Chapter 6: Creating and Consuming Collections 9. Chapter 7: Creating and Consuming Plugins 10. Chapter 8: Coding Best Practices 11. Chapter 9: Advanced Ansible Topics 12. Part 3:Using Ansible in an Enterprise
13. Chapter 10: Network Automation with Ansible 14. Chapter 11: Container and Cloud Management 15. Chapter 12: Troubleshooting and Testing Strategies 16. Chapter 13: Getting Started with Ansible Automation Controller 17. Chapter 14: Execution Environments 18. Assessments 19. Index 20. Other Books You May Enjoy

Defining Your Inventory

As we discussed in the preceding two chapters, Ansible cannot do anything until you tell it which hosts it is responsible for. This is, of course, logical—you wouldn’t want any automation tool, regardless of how easy it is to use and set up, to simply go out and take control of every single device on your network. Hence, at the bare minimum, you must tell Ansible what hosts it is going to automate tasks on, and this, in the most fundamental terms, is what an inventory is.

However, there is so much more to inventories than just a list of automation targets. Ansible inventories can be provided in several formats; they can be either static or dynamic, and they can contain important variables that define how Ansible interacts with each host (or groups of hosts). Hence, they deserve a chapter to themselves, and in this chapter, we shall perform a practical exploration of inventories and how to use them to your best advantage as you automate your...

Technical requirements

This chapter assumes that you have set up your control host with Ansible, as detailed in Chapter 1, Getting Started with Ansible, and you are using the most recent version available—the examples in this chapter were tested with Ansible 8.0 and ansible-core 2.15.0. This chapter also assumes that you have at least one additional host to test against, and this should be Linux-based. Although we will give specific examples of hostnames in this chapter, you are free to substitute them with your own hostnames and/or IP addresses, and details of how to do this will be provided in the appropriate places.

The code bundle for this chapter is available here: https://github.com/PacktPublishing/Practical-Ansible-Second-Edition/tree/main/Chapter%203.

Creating an inventory file and adding hosts

Whenever you see a reference to “creating an inventory” in Ansible, you are normally quite safe to assume that it is a static inventory. Ansible supports two types of inventory—static and dynamic—and we will cover the latter of these two later in this chapter. Static inventories are by their very nature static; they are unchanging unless a human manually edits them. This is great when you are starting out and testing Ansible, as it provides you with a very quick and easy way to get up and running quickly. Even in small, closed environments, static inventories are a great way to manage your environment, especially when changes to the infrastructure are infrequent.

Most Ansible installations will look for a default inventory file in /etc/ansible/hosts (though this path is configurable in the Ansible configuration file, as discussed in Chapter 2, Understanding the Fundamentals of Ansible). You are welcome to populate...

Generating a dynamic inventory file

In these days of cloud computing and infrastructure-as-code, the hosts you may wish to automate could change on a daily, if not hourly, basis! Keeping a static Ansible inventory up to date could become a full-time job, and hence, in many large-scale scenarios, it becomes unrealistic to attempt to use a static inventory on an ongoing basis.

This is where Ansible’s dynamic inventory support comes in. In short, Ansible can gather its inventory data from just about any executable file (though you will find that most dynamic inventories are written in Python)—the only requirement is that the executable returns the inventory data in a specified JSON format. You are free to create your own inventory scripts if you wish, but thankfully, many have been created already for you to use that cover a multitude of potential inventory sources including Amazon EC2, Microsoft Azure, Red Hat Satellite, Lightweight Directory Access Protocol (LDAP) directories...

Special host management using patterns

We have already established that you will often want to run either an ad hoc command or a playbook against only a subsection of your inventory. So far, we have been quite precise in doing that, but let’s now expand upon this by looking at how Ansible can work with patterns to figure out which hosts a command (or playbook) should be run against.

As a starting point, let’s again consider an inventory that we defined earlier in this chapter for the purposes of exploring host groups and child groups. For your convenience, the inventory contents are provided again here:

loadbalancer.example.org
[frontends]
web01.example.org
web02.example.org
[apps]
app01.example.org
app02.example.org
[databases]
db01.example.org
db02.example.org
[fedora:children]
apps
databases
[ubuntu:children]
frontends

To demonstrate host/group selection by pattern, we shall use the --list-hosts switch with the ansible command to see which hosts Ansible would...

Summary

Creating and managing Ansible inventories is a crucial part of your work with Ansible, and hence we have covered this fundamental concept early in this book. They are vital as, without them, Ansible would have no knowledge of what hosts it is to run automation tasks against, yet they provide so much more than this. They provide an integration point with configuration management systems, they provide a sensible source for host-specific (or group-specific) variables to be stored, and they provide you with a flexible way of running this playbook.

In this chapter, you learned about creating simple static inventory files and adding hosts to them. We then extended this by learning how to add host groups and assign variables to hosts. We also looked at how to organize your inventories and variables when a single flat inventory file becomes too much to handle. We then learned how to make use of dynamic inventory files, before concluding with a look at useful tips and tricks such...

Questions

  1. How do you add the frontends group variables to your inventory?
    1. [frontends::]
    2. [frontends::values]
    3. [frontends:host:vars]
    4. [frontends::variables]
    5. [frontends:vars]
  2. What enables you to automate Linux tasks such as provisioning DNS, managing DHCP, updating packages, and configuration management?
    1. Playbook
    2. Yum
    3. Cobbler
    4. Bash
    5. Role
  3. Ansible allows you to specify an inventory file location by using the -i option on the command line:
    1. True
    2. False

Further reading

Details on using Ansible inventory plugins are available here: https://docs.ansible.com/ansible/latest/plugins/inventory.html#using-inventory-plugins

lock icon The rest of the chapter is locked
You have been reading a chapter from
Practical Ansible - Second Edition
Published in: Sep 2023 Publisher: Packt ISBN-13: 9781805129974
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.
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}