Reader small image

You're reading from  Ansible for Real-Life Automation

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803235417
Edition1st Edition
Concepts
Right arrow
Author (1)
Gineesh Madapparambath
Gineesh Madapparambath
author image
Gineesh Madapparambath

Gineesh Madapparambath has over 15 years of experience in IT service management and consultancy with experience in planning, deploying, and supporting Linux-based projects. He has designed, developed, and deployed automation solutions based on Ansible and Ansible Automation Platform (formerly Ansible Tower) for bare metal and virtual server building, patching, container management, network operations, and custom monitoring. Gineesh has coordinated, designed, and deployed servers in data centers globally and has cross-cultural experience in classic, private cloud (OpenStack and VM ware), and public cloud environments (AWS, Azure, and Google Cloud Platform). Gineesh has handled multiple roles such as systems engineer, automation specialist, infrastructure designer, and content author. His primary focus is on IT and application automation using Ansible, containerization using OpenShift (and Kubernetes), and infrastructure automation using Terraform.
Read more about Gineesh Madapparambath

Right arrow

Automating Non-Standard Platforms and Operations

So far, you have learned about the different systems and platforms (managed nodes) such as Linux systems, Microsoft Windows systems, network devices, and cloud platforms, all of which can be automated using Ansible. You have used different methods and protocols to connect to the target system or platform to execute the automated operations. You do not need to understand how complex operations are running inside these systems because Ansible will understand and take appropriate actions on the target systems to reach the desired state.

What if there are no standard Ansible modules or connection methods available for a specific operation on the managed node? Or what if the module is missing some feature you are looking for? For example, let’s say you want to automate a machine that doesn’t have the supported Python version installed, and you want to use Ansible to automate this Python installation or run raw commands on...

Technical requirements

You will need the following technical requirements for this chapter:

  • A Linux machine for the Ansible control node
  • Basic knowledge of commands for network devices (for example, FortiOS and Cisco ASA)
  • Basic knowledge of REST API endpoints and their usages
  • Basic knowledge of bash scripting and the Python programming language

All the Ansible artifacts, commands, and snippets for this chapter can be found in this book’s GitHub repository at https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/tree/main/Chapter-15.

Executing low-down raw commands

So far, you have learned about different Ansible modules and collections for developing automation content. Most of these modules expect a supported Python version on the target node (or PowerShell for Microsoft Windows) to execute the automation scripts in the backend (refer to Ansible’s managed node requirements documentation for more details: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#managed-node-requirements). When there is no required Python version or packages available, then you need to install it manually (or using some scripts) on every node before Ansible can automate the tasks on that nodes. When several managed nodes need to onboard to Ansible, then it will be a tedious task to log into each node and install these dependencies manually. In such situations, it is possible to execute the raw commands (such as the ones you use inside a bash script or PowerShell script) to install and configure...

Using raw commands for network operations

Ansible has a large collection of modules and plugins to support most of the well-known network and firewall devices. The following figure shows some of the supported network devices and brands for network automation:

Figure 15.3 – Supported brands for Ansible network automation (source: https://www.ansible.com/integrations/networks)

However, there will be situations where you must overcome the following challenges:

  • No supported Ansible module is available to execute a specific network or firewall operation.
  • There is a module available for operation but the specific feature or parameter you are looking for doesn’t exist in the current module version.
  • The supported module for the operation is using a slow method compared to the CLI command on the device.

Network Collections in the Ansible Collection Index

Refer to the documentation page at https://docs.ansible.com/ansible/latest...

Using API calls for automation

In Chapter 6, Automating Microsoft Windows and Network Devices, you learned about the different ways Ansible can talk to managed nodes, platforms, or applications. So as long as there is a supported method to access the target system, it is possible use Ansible to automate these tasks. In the previous chapters, you learned about the integration between Ansible and other tools such as Jira, ServiceNow, Kubernetes, public or private cloud platforms, and so on. For such platforms, most of those modules use HTTP/HTTPS API calls to execute operations. This means that if there are no modules available to automate your operations but there is an API method, you can use the same raw API calls from your Ansible playbook.

Python SDK and API Calls

Please remember that not all modules use direct or native API calls to execute the operations; some modules use Python libraries and software development kits (SDKs) to implement these tasks. For example, the FortiOS...

Creating custom modules for Ansible

In the previous sections, you learned how to automate operations if the standard modules are not available for specific tasks. But modules are the standard way of implementing automation and help you develop Ansible playbooks without worrying about the complex operations in the backend. If you know the backend operations and how to execute the tasks in the backend, then create a module for Ansible to execute a specific operation. Finally, contribute it back to the community via Ansible collections. That is the way the open source community grows.

Facts to check before creating a custom Ansible module

You can use any programming language (which can be called by the Ansible API, the ansible command, or the ansible-playbook command), libraries, and methods for your new Ansible module. Most of the Ansible modules that you are using now are written in the Python programming language. Before developing a new module, check yourself on the following...

Summary

In this chapter, you learned how to use Ansible to automate non-supported and non-standard operations using the Ansible raw module. You explored the raw command’s execution on servers, network devices, firewall devices, and more.

Then, you learned how to interact with the devices that provide API-based operations. The Ansible uri module was used to interact with a ToDo application; you explored the options for fetching and adding items to the application via APIs. You also learned about the API-based operations for devices and explored some sample usage using the Akamai DNS API.

In addition to the raw command and API-based operations, you learned about Ansible custom modules and how to create custom modules using bash and Python. In the end, you distributed the custom modules to Ansible Galaxy as an Ansible content collection.

With that, you have explored a common production use case where you can use Ansible as a perfect automation tool and Red Hat Ansible...

Further reading

To learn more about the topics that were covered in this chapter, take a look at the following resources:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Ansible for Real-Life Automation
Published in: Sep 2022Publisher: PacktISBN-13: 9781803235417
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

Author (1)

author image
Gineesh Madapparambath

Gineesh Madapparambath has over 15 years of experience in IT service management and consultancy with experience in planning, deploying, and supporting Linux-based projects. He has designed, developed, and deployed automation solutions based on Ansible and Ansible Automation Platform (formerly Ansible Tower) for bare metal and virtual server building, patching, container management, network operations, and custom monitoring. Gineesh has coordinated, designed, and deployed servers in data centers globally and has cross-cultural experience in classic, private cloud (OpenStack and VM ware), and public cloud environments (AWS, Azure, and Google Cloud Platform). Gineesh has handled multiple roles such as systems engineer, automation specialist, infrastructure designer, and content author. His primary focus is on IT and application automation using Ansible, containerization using OpenShift (and Kubernetes), and infrastructure automation using Terraform.
Read more about Gineesh Madapparambath