Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Ansible for Real-Life Automation

You're reading from  Ansible for Real-Life Automation

Product type Book
Published in Sep 2022
Publisher Packt
ISBN-13 9781803235417
Pages 480 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Gineesh Madapparambath Gineesh Madapparambath
Profile icon Gineesh Madapparambath

Table of Contents (22) Chapters

Preface 1. Part 1: Using Ansible as Your Automation Tool
2. Chapter 1: Ansible Automation – Introduction 3. Chapter 2: Starting with Simple Automation 4. Chapter 3: Automating Your Daily Jobs 5. Chapter 4: Exploring Collaboration in Automation Development 6. Part 2: Finding Use Cases and Integrations
7. Chapter 5: Expanding Your Automation Landscape 8. Chapter 6: Automating Microsoft Windows and Network Devices 9. Chapter 7: Managing Your Virtualization and Cloud Platforms 10. Chapter 8: Helping the Database Team with Automation 11. Chapter 9: Implementing Automation in a DevOps Workflow 12. Chapter 10: Managing Containers Using Ansible 13. Chapter 11: Managing Kubernetes Using Ansible 14. Chapter 12: Integrating Ansible with Your Tools 15. Chapter 13: Using Ansible for Secret Management 16. Part 3: Managing Your Automation Development Flow with Best Practices
17. Chapter 14: Keeping Automation Simple and Efficient 18. Chapter 15: Automating Non-Standard Platforms and Operations 19. Chapter 16: Ansible Automation Best Practices for Production 20. Index 21. Other Books You May Enjoy

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 2022 Publisher: Packt ISBN-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.
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}