Extending Ansible - Sample Chapter
Extending Ansible - Sample Chapter
C o m m u n i t y
$ 29.99 US
19.99 UK
"Community
Experience
Distilled"
E x p e r i e n c e
D i s t i l l e d
Extending Ansible
Rishabh Das
Extending Ansible
Rishabh Das
Preface
With most companies moving to the cloud, infrastructure needs are growing
exponentially. The growing data and massive computing power required to store,
analyze, and process this data adds to the infrastructure needs. With the endlessly
increasing number of Internet service users and the enormous inflow of data
accompanied by a race for data mining, big data and cloud services have opened up
new data centers and expanded upon the existing ones. Also, with constantly scaling
infrastructure and increasing demands, with the 99.9% uptime promises to keep,
automated management of infrastructure became the need of the hour. DevOps soon
became a necessity and the market has flooded with DevOps tools. Ansible is one
such open source solution that combines orchestration, configuration management,
and application deployment capabilities in one.
Ansible is an IT automation tool that lets you manage your Infrastructure as Code.
It helps you deploy your applications and manage configurations, thus making life
easier. It is an open source project built on Python and has great community support.
Ansible, in most ways, is sufficient to address most of your requirements. With a
number of modules and plugins available, Ansible makes everything look so easy.
Writing and understanding playbooks is smooth.
This book is aimed at advanced users who already have a working knowledge of
Ansible, we will discuss various extension points that are exposed by Ansible and
how they can be exploited to fit our requirements. This book covers in detail the
Ansible Python API, Ansible modules, and Ansible plugins. In this bookby means
of real-life scenariosdemonstrates how Ansible can be extended to meet your
requirements. This will take you through a step-by-step process of how you can fill
in the gaps and become a master of Ansible.
Preface
[1]
Why Ansible?
Extending Ansible
Why Ansible?
Out of the many available tools in the market, how do you choose which tool best fits
your need? What factors should you consider while choosing a tool to satisfy your
requirements? Questions may come to mind such as:
What is the return on investment (ROI) in terms of money, time, and effort?
If these are the questions that come to mind, I'll try answering them in favor
of Ansible.
Ansible is free. The only investment you need is some time and effort.
Ansible playbooks are YAML-based and hence are very easy to read,
understand, and maintain, thus involving a very small learning curve.
Modules hide the complexity underneath.
Ansible is open source. Hence, there is an entire community to back you up.
You can file in issues or even fix them yourself, since you will always have
access to the code.
Unlike other solutions, which are mostly agent-based, Ansible works purely
on SSH. There is no agent required. Therefore, you can sit back and relax,
as there is no extra package lying on your production system.
Ansible provides a very good API, which you can use to build your own
Ansible modules that suit your needs and can then be plugged into your
infrastructure.
Ansible provides 90% of user requirements out of the box. The remaining
10% has a well-documented API and community support to build your own
modules, hence increasing the coverage.
If you are satisfied by the above arguments and willing to give Ansible a try, read on.
[2]
Chapter 1
[3]
Company-wide abstraction
Treating your infrastructure as code offers many advantages, but it comes with a cost.
Not all members of your team will be willing to climb the learning curve. As a result,
only a few people will become powerful users of any configuration management tool
such as Ansible, and they will become the bottleneck for the whole team.
A good IAC implementation should make it easy for everyone to interact with the
infrastructure, deploy new software, provision resources, and weave components
together. Details should be abstracted away as much as possible, behavior should
be clear, and definitions should be navigable. There should also exist an easy way
to trace any problems back to a high-level configuration.
To achieve this, one can develop plugins and modules that can abstract the details
and provide interfaces that people can directly use and get results from. This will
help everyone get up to speed and interact with the infrastructure.
You can create modules and plugins that can make your routine tasks easy. You
can share these as utilities that can be used by anyone in the company to carry out
similar tasks. This would require some developer efforts, but would enable even the
not so powerful users to get the most out of their infrastructure.
Chapter 1
Contributing to Ansible
Ansible is an open source project hosted on GitHub. If you have a GitHub account,
you can easily fork the Ansible repository and start contributing to the project
(Ansible code: https://github.com/ansible/ansible).
You can fork the project in your own account, clone it, and then make changes
and send out pull requests to the project owner. This applies to all the open
source projects.
If you don't know where to start contributing, you can also look at the Issues section
in the repository. The Issues section contains bug reports and feature requests from
people using the tool. You might choose to verify and fix the issues and then send in
your patch to the project owner as a pull request against an issue.
The patches go through a review process, and only after the project maintainer's
approval, the patch will be merged. Once merged, the feature will then be available
to the users.
Ansible architecture
Even though we assume our readers have a working knowledge of Ansible, it is
useful to run through a brief overview of the Ansible architecture, so as to have a
better understanding of the various extension points.
Ansible is an agentless configuration management system, meaning no special
software has to run on the managed hosts. Ansible connects to its targets usually via
plain SSH, copies all the necessary code, and runs it on the target machine. Being
agentless is one of the main advantages of Ansible over other solutions. This reduces
the overhead of the setup of agents required on the target machines, also reducing
security risks, as no extra packages or agents need to be installed.
The core Ansible components include:
Inventory: Target
Ansible runner
At the heart of Ansible is the runner. The runner allows you to execute actions on
one or more hosts and gather results.
The runner uses an inventory to choose which hosts to connect to. An inventory may
also associate a set of variables with each host. These variables can then be accessed
through the playbook and by other Ansible components like the connection plugin.
Connection plugins
Connection plugins (with a default SSH connection) can use specific host variables to
figure out how to connect to the remote host. Variables may include information like
a username to be used to connect to the remote host, a non-default port number, and
so on.
[6]
Chapter 1
Playbook
Moving on to another component, the playbook is one of the most important, as all
the recipes are written in the form of Ansible playbooks. Playbooks are modeled as a
collection of plays, each of which defines a set of tasks to be executed on a group of
remote hosts. A play also defines the environment where the tasks will be executed.
Roles
Playbook can be broken down into roles for better organization. Roles help in
modularizing the playbook tasks. These roles can later be included in the play
against specific host groups. For instance, if your infrastructure involves web servers
and proxy servers, each requiring a common set of tasks (preparing the systems)
and then type-specific tasks (setting up and configuring web/proxy servers), these
can be simply broken down into roles, which can later be run against specific hosts.
Common tasks can be defined against all hosts, at which time webserver and proxy
server roles can then be executed against respective host groups.
Variables
Another important component in Ansible architecture is variables. Variables can be
used to extract common values and parameterize shared playbook fragments. They
can also be used to categorize hosts based on some quality they share.
Facts
Since every host can give out a lot of information about itself, managing them
manually is not a recommended practice. Hence, Ansible included a special variable
called facts in its software.
The facts variable is provided by the setup module and gets implicitly executed on
every host (unless explicitly disabled). This variable collects information about the
remote host before the runner starts the execution of the playbook on the remote hosts.
Runner
Now that we have the Ansible playbook in place and all facts about the remote
host group have been collected, the runner kicks in. The runner variable executes
the specific actions (as specified in the Ansible playbook) on the remote hosts by
copying the action code to the target machine and preparing the environment before
executing the action code.
[7]
Once the runner evaluates and executes the tasks, it cleans up the copied code from
the remote host, finally reporting the status through callbacks.
Playbook expressiveness
The expressiveness of the playbook language is limited in order to promote a
somewhat declarative and descriptive structure of your configuration. However,
Ansible does not go overboard in trying to model a strictly declarative configuration.
Ansible plays are modeled as a sequential execution of tasks, affected only by
variables.
There are several tricks that allow you to insert complex logic within the playbooks,
as well as some extension points, which we will see later, that allow you to achieve
what you desire.
Extending Ansible
Ansible provides various extension points that can be used to extend Ansible and
fit it to customize your needs. It has four main entry points where you can put in
your code:
Python API: inverts the control and exploits parts of Ansible from your
custom tools
[8]
Chapter 1
Modules
Modules define the primitive operations that can be performed on your infrastructure.
They allow you to exactly describe what to do right from the playbook. They can
encapsulate a complex high-level task, such as interacting with some external
infrastructure component, and deploy a virtual machine or whole environment.
Modules are the key to Ansible customization. Modules can be written in any
programming language, and if suitable, they can use Ansible itself to perform the
nitty-gritty details of their operation.
A substantial part of this book is devoted to building Ansible modules.
Plugins
The term plugin groups a number of extension points that hook deeply in the
Ansible core and extend its behavior in powerful ways.
The currently available plugins for Ansible are as follows:
Action plugins
Loopback plugins
Callback plugins
Connection plugins
Filter plugins
Vars plugins
Python API
The Ansible Python API allows you to use Ansible as a library, thus making
use of the things that Ansible is good for right from your custom configuration
management solution (whatever it is). You can run Ansible playbooks
programmatically.
The Python API can also be used from within other Ansible extensions; we'll highlight
the important parts throughout this book.
[9]
Summary
After going through this chapter, you might be tempted to use Ansible as a
configuration management and orchestration tool. Perhaps we have also given you a
reason to choose Ansible as an IAC solution. This chapter provided you with a brief
introduction to Ansible and its capabilities and use cases. It familiarized you with the
Ansible architecture, the different components of Ansible, and the various extension
points provided by Ansible. This chapter also took you through the process of
contributing to an Ansible project.
In the next chapter, you will be learning about Ansible modules. The chapter will
take you through what you need to know before you start writing an Ansible
module and guide you through writing your first one. The chapter will also teach
you about some best practices that should be followed while developing an Ansible
module. Additionally, the chapter will create a base for the more advanced topics
that will be covered later in the book, which includes real-life scenarios of where and
how you can exploit the power of Ansible.
[ 10 ]
www.PacktPub.com
Stay Connected: