Looking at the present state of affairs, automation is the new trend in every sector. Automation finds prominent applications across different sectors such as advertising, marketing, sales, cloud, social media, and database management. Addressing the trouble of repetitive tasks, automation is here to stay! In this brief ansible tutorial, we shall take a look at one of the popular IT automation engines, i.e. Ansible.
The primary task of ansible is configuration management. Ansible is an ideal alternative for obtaining the desired state in a particular infrastructure with the least human effort. In this article, we shall take a look at the ansible architecture, how it works, and various ansible advantages. Most important of all, the discussion would highlight the role of playbooks in Ansible. So, let’s learn more about ansible.
Ansible Tutorial: Starting with the Basics
The first point of interest in this discussion should be on what is ansible. However, let us take a look at some facts about Ansible. According to the Ansible community review for 2017, the Ansible user base got increased by 50%. Ansible also has its top-level topic among GitHub searches that leads to more than 5000 databases of Ansible content. Ansible also made to the top 10 of all GitHub projects in different categories according to the 2017 GitHub Octoverse Report.
The ever-increasing community of Ansible and the new features such as Ansible meetups and community working groups indicate Ansible’s popularity. Considering these facts about Ansible, an Ansible tutorial can help you get a superficial idea about this configuration management tool. So, now we shall get to the definition of ansible, shall we?
When you find a question asking about what is ansible, the first thing on your mind should be ‘automation’! Ansible is an open-source IT automation engine ideal for application deployment, cloud provisioning, and intra-service orchestration. It is also helpful for automaton of many other IT tools.
Ansible provides exceptional ease of deployment without the need for custom security infrastructure or any agents. It depends on playbooks for describing automation jobs which is easy to understand. We shall reflect on playbooks in Ansible later on in this brief Ansible tutorial. Another interesting fact about ansible is its design for multi-tier deployment. So, with Ansible, you do not have to manage one system at a time.
On the contrary, Ansible prepares the IT infrastructure with a description of interrelation between all the systems. We shall also reflect on how ansible works in this discussion. However, before that, let us find out more about reasons to use Ansible. And, we are not talking about just the advantages here.
Also Read: Top 10 DevOps Tools
Reasons to Go for Ansible
The foremost agenda in this Ansible tutorial while discussing reasons to use ansible shall start with configuration management. If you have a background in development or operations or you are a DevOps professional, then you must have gone through a series of processes. It may have been logging into a server for changing configuration option, restarting a service or installing a package.
Must have been quite easy logging in through SSH and making changes to the application before logging out, wasn’t it? However, imagine that you face the same issue after some weeks or months! By now, you must have forgotten about how you achieved the fix in the first place. Therefore, you can use Ansible for managing servers with higher speed, consistency, and iteration.
The prominent highlight you can find in almost any Ansible tutorial is Ansible’s simplicity. Ansible is easy to understand as compared to other configuration management tools and also provides credible efficiency and functionality. You can also find other prominent reasons to use Ansible as we move ahead gradually in our discussion.
Preparing for an Ansible interview? Go through these top Ansible interview questions and get ready to crack the Ansible interview.
The Architecture of Ansible
The next important element in almost every ansible tutorial refers to the architecture of ansible. An understanding of the different elements in the architecture of Ansible can also support our understanding of how ansible work. The six prominent components of Ansible include modules, module utilities, inventory, playbooks, plugins, APIs, and the Ansible search path. Let us discuss each of them briefly as a promising addition to this brief introductory guide on Ansible.
The first element in ansible architecture refers to modules. Ansible works through connecting to different nodes and pushing out scripts known as “ansible modules” on them. The modules accept parameters describing the desired state of a system. Subsequently, ansible ensures execution of the modules followed by removing them upon finishing. The default method for executing modules is over SSH.
The library of modules can exist on any machine without the need for servers, databases, or daemons. Ansible gives much-needed flexibility for scripting your modules. The text editor terminal program and the version control system in Ansible can help in tracking changes in content. Furthermore, you can write specialized modules in languages such as Ruby, bash, or Python, which return JSON.
The next important addition to the architecture of Ansible is module utilities. Module utilities are one of the major reasons for simple ansible usage. In cases of multiple modules using the same code, Ansible ensures storage of the functions as module utilities. The module utilities help in reducing duplication and maintenance. The example of a code that parses URLsis an ideal reference for module utilities. The code can be “lib/ansible/module_utils/url.py.” Also, you have the flexibility to write your module utilities. Python or PowerShell is ideal for writing module utilities.
Plugins are also one of the common entries in the architecture of ansible found commonly in every ansible documentation. They are known for improving the core functionality of Ansible. Plugins execute on control node in the “/usr/bin/ansible” process rather than executing on a remote system like modules. They provide extensions and options for core functionalities in Ansible.
The core functionalities include data transformation, connecting to inventory, logging output, and others. Although you can find a fair number of usable plugins at your disposal when you ship in Ansible, you could also write some plugins on your own. One instance can involve writing an inventory plugin for connecting to any data source returning JSON. Python is the sole language for writing ansible plugins.
The next important element in the architecture of ansible refers to Inventory. Ansible provides a representation of machines that it manages in a file which is responsible for putting all the managed machines. You can assign managed machines to groups of own choosing. Every ansible documentation would outline the role of inventory in ansible.
You don’t need any additional SSL signing server for adding new machines. Therefore, there are no issues in deciding on reasons for which specific machines do not get linked for DNS issues or obscure NTP. Ansible could derive inventory, variable, and group information from different sources such as Rackspace, EC2, OpenStack, and others. The example of a plain text inventory is shown as follows.
--- [webservers] www1.example.com www2.example.com [dbservers] db0.example.com db1.example.com
After listing inventory hosts, you can assign variables to them in simple text files. You can assign variables directly in the inventory file or the subdirectory known as ‘group_vars/’ or ‘host_vars/.’
Ansible APIs are used as transport for Cloud services whether used public or private. As Ansible API documentation is under construction, there is no more information available about APIs.
Certification helps the professionals to validate their skills and get global recognition for them. Check out the list of Top DevOps Certifications and be determined to get one!
Playbooks in Ansible
Now, we shall shed some light on the backbone of ansible, i.e., playbooks. As we know until now, playbooks help in the description of automation jobs, and it uses the very simple YAML. YAML stands for Yet Another Markup Language, and its unique highlight is that it is in human-readable form. Any ansible-playbook tutorial would show that YAML is a data serialization language ideal for configuring files. Let us reflect further on Ansible playbooks to clarify our understanding of Ansible’s working.
Simply put, playbooks are the files in which you write Ansible code. Playbooks are core features in Ansible and provide information regarding the execution tasks to Ansible. Think of playbooks as a to-do list for Ansible!
You can observe details on playbook structure in any ansible-playbook tutorial. The structure of playbook involves many plays. Each play has to serve the function of mapping a set of instructions defined concerning a specific host. Another important concern in playbooks refers to the strict-typed nature of YAML. Even if YAML is in human-readable format, you need to take extra care when writing YAML files.
You can use a simple editor such as notepad++ as a YAML editor. All you have to do is open notepad++, copy and paste “(Language → YAML)” YAML for changing the language to YAML. Keep in mind that a YAML always starts with three hyphens (—). Now, let us take a look at an example of creating a playbook. The following example shows a sample YAML file.
--- name: install and configure DB hosts: testServer become: yes vars: oracle_db_port_value : 1521 tasks: -name: Install the Oracle DB yum: <code to install the DB> -name: Ensure the installed service is enabled and running service: name: <your service name>
The Basic Syntax of Ansible Playbook
The sample playbook shows the basic syntax for a playbook. You have to save the sample playbook with the filename ‘test.yml.’ A YAML syntax should always follow the appropriate indentation, and you should be additionally careful in writing the syntax. Now, let us describe the important components noted in the above sample playbook. The following elements in this ansible tutorial are also known as YAML tags. The different YAML tags in the example mentioned above are ‘name,’ ‘hosts,’ ‘vars,’ and ‘tasks.’ A description of the tags can help in improving your knowledge of ansible usage.
The ‘name’ YAML tag provides a specification for the name of the Ansible playbook. The ‘name’ YAML tag describes the work of the playbook.
The ‘hosts’ YAML tag is also an important requirement playbook description in every ansible tutorial. The ‘hosts’ tag provides the specification of the lists of hosts or host group which you have to run a task. This is a mandatory field in the playbook as it informs Ansible about the hosts on which it should run listed tasks. You can run tasks on the same machine or a remote machine. You can also run tasks on multiple machines, thereby implying the possibility of a group of hosts entry.
The ‘vars’ YAML tag in ansible is also an important concern in almost any Ansible tutorial. This YAML tag helps in the definition of the variable for use in a playbook. The use of ‘vars’ in Ansible resembles variables in the case of any programming language.
The final element in an Ansible playbook refers to ‘tasks’ YAML tag. This YAML tag is a list of actions that Ansible should perform. The major use of ‘tasks’ YAML tag is in debugging the playbook.
On a concluding note to this Ansible tutorial, let us reflect on the future that Ansible holds for IT automation. For one mention, Ansible is a definite improvement over other configuration management systems. We can note that Ansible is agentless as you can install ansible only on the control machine. SSH can take care of connecting remote machines for you.
The thorough and simple documentation of Ansible is a formidable mention among ansible advantages. This can promote Ansible adoption, won’t it? An opensource instrument with a human-readable language, i.e., YAML can also appeal to many users in the automation landscape. So, if you have developed an interest in ansible recently, then you have a long way to go! This discussion can serve as a helpful guide for your ansible journey.
In DevOps, the task of development and operations is integrated, which is very important for designing test-driven applications. Ansible provides a stable environment for development and operation for integration and thus, results in smooth orchestration. So, if you are a DevOps professional, start learning Ansible now! You can also check out our DevOps certification training courses if you are preparing for any DevOps certification.
- Understanding the Role of Chatbots in DevOps - October 26, 2020
- Top 20 Git Commands with Examples - October 19, 2020
- Top Applications of Chatbots (with Use Cases) - October 13, 2020
- How to Prepare for Certified Kubernetes Application Developer (CKAD) Exam? - September 30, 2020
- Top 12 Docker Tips and Tricks [Latest] - September 26, 2020