Table of Content
Ansible playbooks form a popular and useful DevOps tool that focuses on automation in remote systems. To work with an Ansible playbook, one needs to have the basic knowledge of DevOps. It is nothing but an amalgamation of practices, tools, and philosophies that enhance the efficiency of an organization to make its services and applications work with precision.
DevOps has changed how traditional software works, changing the nature of software development and infrastructure management processes. It is an automation process that reduces the delivery time of applications.
Many big companies have grown successfully over the years by using DevOps, which plays a significant role in increasing the efficiency of applications. Notably, DevOps engineers deal with the application management process through automation and unification with the help of coding.
Ansible playbook is a perfect example of DevOps that helps the software development process to take place automatically. The collaboration and interaction or communication among the operation professionals, project management, and software development take place through tools like Ansible playbooks.
Ansible is a reliable automation tool capable of handling intra-service orchestration, application management, and provisioning. It is an open-source automation tool that reduces time consumption and eliminates tasks that require repetition. Ansible playbooks help teams to concentrate on strategic work.
Ansible playbooks help in the deployment of applications and interservice orchestration. It is an integral part of the Ansible configuration. We can clearly understand the concept of Ansible playbooks once we look into their usage and variables with some examples.
An Ansible playbook is a handy DevOps tool that helps remote systems to work efficiently through automation. It is a file that enables users to write Ansible codes that play a significant role in server configuration. It is a collection of scripts that supports the IT processes in enforcing remote systems.
Playbooks in Ansible have one or multiple playbooks arranged in a specific order. These ordered sets of tasks function against hosts selected from one’s inventory. The most intriguing part is that it works constructively to set the work you need to do in a remote system.
We will be providing you with a few examples to mark using Ansible playbooks to create playbooks that you may need to run regularly. You will need to develop and run them using the control machine.
Example 1:
Create a file on the target machine/server, keeping the inventory file and the web server’s group in mind. Use this code on a .yml extension and then run the playbook.
- hosts: webservers become: true tasks: - name: Create a file file: path=/home/ansible/niranjan.txt state=touch
In this code, we have used a file module to create a file.
Example 2:
Let’s learn how to create and delete a user in the playbook.
--- - hosts: webservers become: true tasks: - name: Create User user: name=cynthia password=cynthia groups=ansible shell=/bin/bash
Ansible playbooks have a clear advantage of getting used in many cases. You can use them for the following tasks:
An Ansible playbook consists of a set of hosts that participate in the configuration. You will be amazed to know that it also has a list containing the tasks you need to carry out. However, it would help to consider that there is no scope for any play that is standardized, for every play has to be written by an administrator to make the automation happen.
To understand Ansible playbooks, you need to know the types and the ways of using Ansible playbooks. Ansible uses YAML syntax, which gives it the right to seek two types of file extensions, namely .yaml and .yml. YAML is short for another markup language.
Starting with prerequisites, we will be moving forward with installing and more. You can either use Ansible playbooks using a command-line interface or CLI. To run your first command and Ansible playbook, keep a few pointers in mind.
Before starting this journey, we need you to go through the prerequisites. To run your first command, you need to exercise the previously discussed concepts. You can move forward to actual programming by choosing a compatible Ansible. You can begin with Ansible 2.10 or higher versions installed.
After installing the version, you need to find one or more network devices compatible with Ansible. You must acquire basic Linux command-line knowledge and network switch and router configuration.
Ansible modules carry out tasks. One or more tasks can be combined to form a play. Two or more plays can be combined to create the Ansible Playbook. Ansible Playbooks are libraries of commands that automatically execute against hosts. The Ansible inventory is made up of host groups. Ansible modules in a Playbook perform a certain task. Each ansible module offers metadata that identifies the when, where, and who performs a task. Check out our Business Analytics and Data Science Course.
To launch complicated applications, Ansible Playbooks provide a repeatable, reusable, easy configuration management and multi-machine deployment mechanism. Learn What Is Data: Definition, Types & Data Management. Explore the Data Warehousing and Data Mining.
Once you are confident about your experience, you can start installing Ansible. Follow the steps from here.
ssh my_vyos_user@vyos.example.net show config exit
By establishing a manual connection for the authenticity of the network device, you will be able to move ahead. It works by adding its RSA key fingerprint to the loss of known hosts in your system. If you have already connected to the device in the past, you have successfully established its authenticity.
ansible all -i vyos.example.net, -c ansible.netcommon.network_cli -u my_vyos_user -k -m vyos.vyos.vyos_facts -e ansible_network_os=vyos.vyos.vyos
In this set of seven values, the flags in the command are:
Ansible will load them automatically if you have used the SSH agent with SSH keys. As a result, you can omit the (-k) flag. If you decide to run Ansible in an effective environment, you can add this variable:
ansible_python_interpreter=/path/to/venv/bin/python
--- - name: Network Getting Started First Playbook connection: ansible.netcommon.network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"
This playbook consists of seven values from the command line: the group, connection method, and module. Once you have the values set in the playbook, you can exclude the command line. You can also add a second task to showcase the dual output.
ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml At this point, your playbook should generate an output like this: $ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml PLAY [First Playbook] *************************************************************************************************************************** TASK [Get config for VyOS devices] *************************************************************************************************************************** ok: [vyos.example.net] TASK [Display the config] *************************************************************************************************************************** ok: [vyos.example.net] => { "msg": "The hostname is vyos, and the OS is VyOS 1.1.8" }
--- - name: Network Getting Started First Playbook Extended connection: ansible.netcommon.network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}" - name: Update the hostname vyos.vyos.vyos_config: backup: yes lines: - set system host-name vyos-changed - name: Get changed config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the changed config debug: msg: "The new hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}" This extension of the first playbook consists of four tasks in a single play. After you run it using the same command that we have previously used, the output will show this: $ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook_ext.yml PLAY [First Playbook] ************************************************************************************************************************************ TASK [Get config for VyOS devices] ********************************************************************************************************************************** ok: [vyos.example.net] TASK [Display the config] ************************************************************************************************************************************* ok: [vyos.example.net] => { "msg": "The hostname is vyos and the OS is VyOS 1.1.8" } TASK [Update the hostname] ************************************************************************************************************************************* changed: [vyos.example.net] TASK [Get changed config for VyOS devices] ************************************************************************************************************************************* ok: [vyos.example.net] TASK [Display the changed config] ************************************************************************************************************************************* ok: [vyos.example.net] => { "msg": "The new hostname is vyos-changed and the OS is VyOS 1.1.8" } PLAY RECAP ************************************************************************************************************************************ vyos.example.net : ok=5 changed=1 unreachable=0 failed=0
- hosts: arista gather_facts: True gather_subset: interfaces module_defaults: arista.eos.eos_facts: gather_network_resources: interfaces Congratulations! Your playbook will return the following interface facts: "network_resources": { "interfaces": [ { "description": "test-interface", "enabled": true, "mtu": "512", "name": "Ethernet1" }, { "enabled": true, "mtu": "3000", "name": "Ethernet2" }, { "enabled": true, "name": "Ethernet3" }, { "enabled": true, "name": "Ethernet4" }, { "enabled": true, "name": "Ethernet5" }, { "enabled": true, "name": "Ethernet6" }, ] }
Finally, you can store the above facts and use them for other tasks.
DevOps aims to make software operations easier by approaching a faster development method. Ansible playbooks are a handy bunch that can change the way you look at automation systems. These are a complex set of IT actions that, once executed, can be of great use with little human interaction.
Therefore, new products come to the fore along with the maintenance of the pre-existing deployments. Hence, you can opt for Ansible playbooks at any point in time without giving it a second thought.
You can learn about Ansible, and other key DevOps tools and technologies, with the Hero Vired Certificate Program in DevOps & Cloud Engineering, offered in partnership with edX and from AWS
Ansible Playbook makes DevOps simpler by automating the integration of internally created applications into your production programs. Ansible Playbook is one of the most well-liked DevOps tools for orchestration, automation, configuration, and management of IT infrastructure. In order to launch complicated applications, Ansible Playbook provides a repeatable, reusable, easy configuration management and multi-machine deployment mechanism.
You may also like
Carefully gathered content to add value to and expand your knowledge horizons