Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Ansible is an open-source IT engine that automates the deployment of IT technologies, including intra-service orchestration, cloud provisioning, and application deployment. Ansible helps you with the easy installation of software.
In this blog, we will discuss Ansible Network Roles in detail. So without any further ado, let's get started!
What is Ansible Role?
Ansible roles are playbooks divided into a predefined file structure. When we switch from playbooks to roles, it is simpler to share, read, and update our Ansible workflow. Users can create custom roles. We don't have to create our DNS playbook. Instead, we designate a DNS server and a role that will handle its configuration.
The Ansible Network team has created several roles for frequent network use cases to streamline our workflow further. We can avoid having to create everything from scratch by using these roles. We can focus on creating and maintaining the parser templates that describe our network topologies and inventory instead of writing and maintaining our create_vlan playbooks or roles and let Ansible's network roles handle the rest.
DNS Playbook Sample
The example playbook.yml file is a single YAML file that contains a two-task playbook to help illustrate what a role is.
This Ansible Playbook first configures the hostname on a Cisco IOS XE device before configuring the DNS servers.
Ansible roles can be conceptualised as disassembled playbooks. They are reusable, efficient, and simple. Now, a different user can just add the system-demo role without having to develop a unique, "hard-coded," playbook.
Variable Precedence
We are not required to modify the role structure's vars/main.yml file. There are numerous places in Ansible where we can specify variables for a particular play.
The number of places where variables can be placed is 21. The vast majority of use cases only require understanding where to put the variables with the lowest precedence and how to pass the variables with the highest precedence.
Lowest Precedence📗⬇
The default directory inside a role has the lowest precedence. All 20 additional locations where we could potentially specify the variable will take precedence over defaults. Rename the vars directory to defaults to immediately give the system-demo role's variables the lowest priority.
[user@ansible system-demo]$ mv vars defaults
[user@ansible system-demo]$ tree
.
├── defaults
│ └── main.yml
├── tasks
│ └── main.yml
In order to alter the playbook's default behaviour, add a new vars section. Set dns to 1.1.1.1 for this demonstration so that playbook.yml becomes:
The rtr2 Cisco router will be configured as follows:
rtr2#sh run | i name-server
ip name-server 1.1.1.1
The default directory is now subordinate to the variable set up in the playbook. In fact, the values in the defaults directory would prevail in any other place where we configure variables.
Highest Precedence📗⬆
Variables specified as extra vars with the -e or —extra-vars = will always take precedence over those specified in the defaults directory within a role, regardless of the situation. The newly created vars within the playbook that contains the 1.1.1.1 DNS server are overridden when the playbook is restarted with the -e option.
Only the highest precedence setting of 192.168.1.1 will appear on the Cisco IOS XE router as a result:
rtr3#sh run | i name-server
ip name-server 192.168.1.1
Network administrators frequently use extra variables to override defaults. The Job Template Survey feature on AWX or the Red Hat Ansible Automation Platform are two effective examples of this. A network operator can be asked to complete a Web form with the necessary information through the web UI.
For non-technical playbook authors, using a Web browser to execute a playbook can be incredibly simple!
Update an Installed Role👩💻
All of the versions are listed on a role's Ansible Galaxy page. Use the ansible-galaxy install command with the version and force options to update a locally installed role to a new or different version. We might need to update any dependent roles manually to support this version.
Ansible is an open-source IT engine that automates the deployment of IT technologies, including intra-service orchestration, cloud provisioning, and application deployment.
What is Ansible Playbook?
Playbooks are the files in which the Ansible code is written. Playbooks are written in YAML format. YAML stands for "Yet Another Markup Language"; thus, there isn't much syntax required.
Which language is used in Ansible?
Ansible uses Python, Powershell, and R programming languages.
What is Ansible's main function in a network?
Ansible roles are playbooks divided into a predefined file structure. When you switch from playbooks to roles, it is simpler to share, read, and update your Ansible workflow. Users can create custom roles. You don't have to create your DNS playbook.
What is Ansible Unarchive?
The Ansible unarchive module is used to unpack or uncompress files from archive files like zip, tar, and tar.gz. Before uncompressing the files, it might optionally transfer the files to a remote server before uncompressing them.
Conclusion
In this article, we have extensively discussed Ansible Network Role and variable precedence.
If you want to learn more about Ansible, check out our articles on