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

Starting with Simple Automation

When you start your automation journey, start with simple use cases instead of automating complex workflows. Find three small use cases that you can use to learn automation faster and implement it in your environment. Ansible has a smooth learning curve but it is also important to choose the right use cases for your first automation project. Three great examples for initial use cases for automation are simple tasks such as application deployment, asset information collection, and simple file manipulation such as copy operations.

In this chapter, we are going to cover the following topics:

  • Identifying manual tasks to be automated
  • Finding the Ansible modules to use
  • Configuring your text editor for Ansible
  • Connecting to remote nodes

You will start by creating basic automation tasks by finding suitable modules before learning how to use credentials and other parameters.

Technical requirements

You will need the following technical requirements for this chapter:

  • A Linux machine for the Ansible control node
  • One or more Linux machines with Red Hat repositories configured (if you are using other Linux operating systems instead of Red Hat Enterprise Linux (RHEL), then make sure you have the appropriate repositories configured to get packages and updates)

All the Ansible code and the Ansible playbooks, 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-02.

Identifying manual tasks to be automated

In the previous chapter, you learned how to use Ansible ad hoc commands to manually execute tasks on remotely managed nodes using Ansible modules. Now, you will learn how to start with simple Ansible playbooks and tasks. Remember, you need to add your managed node details to your inventory file before you can execute any Ansible tasks.

We will start with a simple automation job to understand the basics of the Ansible playbook. For this example, we are assuming you have installed and configured the chronyd application. The chrony application is an implementation of the Network Time Protocol (NTP). chronyd is the default NTP client and server in Red Hat Enterprise Linux 8 and SUSE Linux Enterprise Server 15 and is available in many Linux distributions.

For our example Ansible playbook, we will do the following:

  1. Install the chrony package on all nodes.
  2. Adjust the chrony configurations.
  3. Start the chronyd service and enable...

Finding the Ansible modules to use

In this section, you will learn how to find suitable modules and documentation to use inside the Ansible playbook.

Find the available modules and details using the ansible-doc command:

Figure 2.12 – Ansible module list

It will be a long or short list, depending on your type of Ansible installation. (Recall the difference between ansible, ansible-base, and ansible-core, which was explained in the previous chapter.) You can check the total module count that’s available as follows:

[ansible@ansible Chapter-02]$ ansible-doc -l |wc -l
6108

Check the module details by calling the module name with the -s (--snippet) argument, as follows:

Figure 2.13 – Ansible module snippet for the dnf module

Alternatively, Check the full details of the module as follows:

Figure 2.14 – Ansible module details for the dnf module

The preceding output shows the example...

Configuring your text editor for Ansible

Since YAML is highly sensitive to indentation, you need to take extra care while developing and editing playbooks using your text editor. You can use any text editor of your choice to edit Ansible playbooks and configure the editor as needed.

If you can use a GUI editor such as Visual Studio Code or Atom, skip this section as GUI editors can easily be configured with multiple plugins to perform Ansible content development more efficiently. Please refer to https://docs.ansible.com/ansible/latest/community/other_tools_and_programs.html to find details about tools and programs for Ansible content development. Now, let’s learn how to configure the Vim editor for Ansible YAML files. Use Vim variables to enable or disable the features in the Vim editor:

[ansible@ansible Chapter-02]$ vim install-package.yaml 

Now, press Esc followed by : and type set nu to enable line numbers, as shown in the following screenshot:

...

Connecting to remote nodes

It is the best practice to use dynamic inventories to avoid frequent changes in static inventory files. However, this depends on your environment. It is also a best practice to separate inventory files based on environment, criticality, or other parameters. The following screenshot shows sample inventory files based on the workload environment. As you can see, there are different directories and files for production, development, and staging devices:

Figure 2.22 – Ansible inventory separation based on environment

You need to ensure that the Ansible control node to managed nodes connection is safe and secure. For Linux/Unix managed nodes, use the ssh connection (which is the default connection method) with key-based authentication, as explained in Chapter 1's, Configuring Your Managed Nodes section. There might be cases where you cannot use SSH keys. In that case, you can use encrypted passwords with a username for authentication...

Summary

In this chapter, you learned how to develop a simple playbook to automate the chrony package’s deployment and service management. You learned how to use the ansible-doc command to find the modules and details, including module examples and arguments to use. You also explored how to use the ansible-doc command to find the connection plugins and become plugins. After that, you learned how to configure your Vim editor to edit Ansible YAML files easily. Finally, you learned how to configure managed node connection methods.

In the next chapter, you will learn how to automate your daily tasks using Ansible, develop real use cases, and apply them to your workplace.

Further reading

For more information on the topics covered in this chapter, please visit the following links:

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 €14.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