Reader small image

You're reading from  Practical Ansible - Second Edition

Product typeBook
Published inSep 2023
PublisherPackt
ISBN-139781805129974
Edition2nd Edition
Right arrow
Authors (3):
James Freeman
James Freeman
author image
James Freeman

James Freeman is an accomplished IT professional with over 25 years' experience in the technology industry. He has more than a decade of first-hand experience in solving real-world enterprise problems in production environments using Ansible, open source, and AWS. As part of this work, he frequently introduces Ansible as a new technology to businesses and CTOs for the first time. In addition, he has co-authored five books and one video training course on Ansible, facilitated bespoke Ansible workshops and training sessions, and presented at both international conferences and meetups on Ansible.
Read more about James Freeman

Fabio Alessandro Locati
Fabio Alessandro Locati
author image
Fabio Alessandro Locati

Fabio Alessandro Locati – commonly known as Fale – is an EMEA associate principal solutions architect at Red Hat, a public speaker, an author, and an open source contributor. His primary areas of expertise are Linux, automation, security, and cloud technologies. Fale has more than 15 years of working experience in IT, with many of them spent consulting for various organizations, including dozens of Fortune 500 companies. Fale has written Learning Ansible 2.7, Learning Ansible 2, and OpenStack Cloud Security, and has been part of the review process of multiple books.
Read more about Fabio Alessandro Locati

Daniel Oh
Daniel Oh
author image
Daniel Oh

Daniel Oh is a principal technical marketing manager at Red Hat. He provides runtimes, frameworks, fast data access, and high-performance messaging in flexible, easy-to-use, cost-effective, open, and collaborative ways. He's also a CNCF ambassador and DevOps Institute ambassador who evangelizes how to design and develop cloud-native serverless microservices and deploy them to multi/hybrid cloud-native platforms based on CNCF projects. Daniel loves to share his developer experiences with DevOps folks in terms of how to evolve traditional microservices to cloud-native, event-driven, and serverless applications via technical workshops, brown bag sessions, hackathons, and hands-on labs across regions at many international conferences.
Read more about Daniel Oh

View More author details
Right arrow

Network Automation with Ansible

Years ago, the standard practice was to configure every single network device by hand. This way of managing devices was possible mainly because the routers and switches were routing the traffic of physical servers, so only a little configuration was needed on each networking device, and changes were slow-paced. In addition, humans were the only ones with enough information on machines to set up networking. Everything was very manual in terms of both planning and execution.

Virtualization changed this paradigm, as it has resulted in thousands of machines being connected to the same switch or router, each with potentially different networking requirements. Changes are fast paced and expected frequently, and with virtual infrastructures defined in code, it becomes a full-time job for a human administrator just to keep up with the changes to the infrastructure. Virtualization orchestration platforms have far better knowledge of the machine’s location...

Technical requirements

This chapter assumes that you have set up your control host with Ansible, as detailed in Chapter 1, Getting Started with Ansible, and are using the most recent version available – the examples in this chapter were tested with Ansible 2.15. This chapter also assumes that you have at least one additional host to test against; ideally, this should be Linux-based. Since this chapter is network device-centric, we understand that not everyone will have access to specific networking equipment to test on (for example, Cisco switches).

Where examples are given, and you have access to such devices, please feel free to explore the examples. However, if you do not have access to any network hardware, we will give an example using the freely available Cumulus VX, which offers a fully-featured demo of Cumulus Networks’ switching environment. Although we will give specific examples of hostnames in this chapter, you are free to substitute them with your hostname...

Why automate network management?

How we design data centers has radically changed in the last 30 years. In the 90s, a typical data center was full of physical machines, each with a particular purpose. In many companies, the servers were bought from different vendors based on the purpose of the machine. This meant that every time there was a need for new servers, network devices, and storage devices, those devices were bought, provisioned, configured, and delivered.

The big drawback here was the significant lag between identifying the need for the server and its delivery. At the time, this was acceptable since most companies had very few systems, and they tended to change very rarely. Also, this approach was costly as a lot of devices were underutilized.

With the progress of society and companies in the world of technology, it has become necessary for companies to get more efficiency from their infrastructure and cut their infrastructure deployment time and costs. These new requirements...

How Ansible manages networking devices

Ansible allows you to manage many different networking devices, including Arista EOS, Cisco ASA, Cisco IOS, Cisco IOS XR, Cisco NX-OS, Dell OS 6, Dell OS 9, Dell OS 10, Extreme EXOS, Extreme IronWare, Extreme NOS, Extreme SLX-OS, Extreme VOSS, F5 BIG-IP, F5 BIG-IQ, Junos OS, Lenovo CNOS, Lenovo ENOS, MikroTik RouterOS, Nokia SR OS, Pluribus Netvisor, and VyOS, as well as all OSs that support NETCONF. As you can imagine, we can make Ansible communicate with them in various ways.

Also, we have to remember that Ansible networking modules run on the controller host (the one where you issued the ansible command), while usually, the Ansible modules run on the target host. This difference is crucial because it allows Ansible to use different connection mechanisms based on the target device type. Remember that even when you have a host with SSH management capabilities (that many switches have), Ansible needs Python to be present on the target host...

Connecting to network devices

As we have seen, there are some peculiarities in Ansible networking, so specific configurations are required.

To manage network devices with Ansible, you need to have at least one to test on. Let’s assume we have a Cisco IOS system available to us. It is accepted that not everyone will have such a device to test on, so the following is offered as a hypothetical example only.

Going by the https://docs.ansible.com/ansible/latest/network/user_guide/platform_index.html page, we can see that the correct ansible_network_os for this device is cisco.ios.ios and that we can connect to it using both network_cli and local. Since local is deprecated, we are going to use network_cli. Follow these steps to configure Ansible so that you can manage IOS devices:

  1. First, let’s create the inventory file with our devices in the routers group:
    [routers]
    n1.example.org
    n2.example.org
    [cumulusvx]
    vx01.example.org
  2. To know which connection parameters...

Custom conditional statements for networking devices

Although there are no networking-specific Ansible conditionals, conditionals frequently come into play in networking-related Ansible usage.

In networking, it’s common to enable and disable ports. To have data be able to pass through the cable, both ports at the ends of the cable should be enabled, resulting in a connected state (some vendors will use different names for this, but the idea is the same).

Let’s suppose we have two Arista Networks EOS devices, and we issued the ON status on the ports and need to wait for the connection to be up before proceeding.

To wait for the Ethernet4 interface to be enabled, we will need to add the following task to our playbook:

- name: Wait for interface to be enabled
  arista.eos.eos_command:
    commands:
    - show interface Ethernet4 | json
    wait_for:
    - "result[0...

Summary

Modern large-scale infrastructures that change rapidly necessitate the automation of network tasks. Fortunately, Ansible supports a wide array of network devices, from proprietary hardware such as Cisco IOS-based devices, to open standards such as white box switches that run operating systems such as Cumulus Linux. Ansible is a powerful and supportive tool to manage your network configuration and allows you to implement changes quickly and safely. You can even replace entire devices in your network and be confident in your ability to put the correct configuration on the new device, thanks to your Ansible playbooks.

In this chapter, you learned about the reasons for automating network management. You then looked at how Ansible manages network devices, how to enable network automation in Ansible, and how to locate the Ansible modules necessary to perform the automation tasks you wish to complete. Then, through practical examples, you learned how to connect to network devices...

Questions

  1. Which of these is NOT one of the four major connection types that Ansible uses for connecting to network devices?
    1. netconf
    2. network_cli
    3. local
    4. netstat
    5. httpapi
  2. True or false: The ansible_network_os variable is used by Ansible to understand the device family of the device we are going to use.
    1. True
    2. False
  3. True or false: To connect to an IOS router in a separate network, you need to specify the special connection variables for the host, possibly as inventory group variables.
    1. True
    2. False

Further reading

The official documentation about Ansible networking is available here: https://docs.ansible.com/ansible/latest/network/index.html.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Practical Ansible - Second Edition
Published in: Sep 2023Publisher: PacktISBN-13: 9781805129974
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

Authors (3)

author image
James Freeman

James Freeman is an accomplished IT professional with over 25 years' experience in the technology industry. He has more than a decade of first-hand experience in solving real-world enterprise problems in production environments using Ansible, open source, and AWS. As part of this work, he frequently introduces Ansible as a new technology to businesses and CTOs for the first time. In addition, he has co-authored five books and one video training course on Ansible, facilitated bespoke Ansible workshops and training sessions, and presented at both international conferences and meetups on Ansible.
Read more about James Freeman

author image
Fabio Alessandro Locati

Fabio Alessandro Locati – commonly known as Fale – is an EMEA associate principal solutions architect at Red Hat, a public speaker, an author, and an open source contributor. His primary areas of expertise are Linux, automation, security, and cloud technologies. Fale has more than 15 years of working experience in IT, with many of them spent consulting for various organizations, including dozens of Fortune 500 companies. Fale has written Learning Ansible 2.7, Learning Ansible 2, and OpenStack Cloud Security, and has been part of the review process of multiple books.
Read more about Fabio Alessandro Locati

author image
Daniel Oh

Daniel Oh is a principal technical marketing manager at Red Hat. He provides runtimes, frameworks, fast data access, and high-performance messaging in flexible, easy-to-use, cost-effective, open, and collaborative ways. He's also a CNCF ambassador and DevOps Institute ambassador who evangelizes how to design and develop cloud-native serverless microservices and deploy them to multi/hybrid cloud-native platforms based on CNCF projects. Daniel loves to share his developer experiences with DevOps folks in terms of how to evolve traditional microservices to cloud-native, event-driven, and serverless applications via technical workshops, brown bag sessions, hackathons, and hands-on labs across regions at many international conferences.
Read more about Daniel Oh