Network Automation Cookbook
local graph
Network Automation Cookbook
Section titled “Network Automation Cookbook”chapter_01
Section titled “chapter_01”node-book_network-automation-cookbook_chapter-01-why-ansible-for-network-automation — Why Ansible for Network Automation?
chapter_02
Section titled “chapter_02”node-book_network-automation-cookbook_chapter-02-building-blocks-of-ansible — Building Blocks of Ansible
chapter_03
Section titled “chapter_03”node-book_network-automation-cookbook_chapter-03-advanced-ansible-concepts — Advanced Ansible Concepts
chapter_04
Section titled “chapter_04”node-book_network-automation-cookbook_chapter-04-building-data-center-networks-with-arista — Building Data Center Networks with Arista
chapter_05
Section titled “chapter_05”node-book_network-automation-cookbook_chapter-05-administering-a-multi-vendor-network-with-napalm-and-ansible — Administering a Multi-Vendor Network with NAPALM and Ansible
chapter_06
Section titled “chapter_06”node-book_network-automation-cookbook_chapter-06-deploying-and-operating-aws-networking — Deploying and Operating AWS Networking
chapter_07
Section titled “chapter_07”node-book_network-automation-cookbook_chapter-07-deploying-and-operating-azure-networking — Deploying and Operating Azure Networking
chapter_08
Section titled “chapter_08”node-book_network-automation-cookbook_chapter-08-deploying-and-operating-gcp-networking — Deploying and Operating GCP Networking
chapter_09
Section titled “chapter_09”node-book_network-automation-cookbook_chapter-09-network-validation-with-batfish — Network Validation with Batfish
chapter_10
Section titled “chapter_10”node-book_network-automation-cookbook_chapter-10-building-a-network-inventory-with-nautobot — Building a Network Inventory with Nautobot
chapter_11
Section titled “chapter_11”node-book_network-automation-cookbook_chapter-11-simplifying-automation-with-awx — Simplifying Automation with AWX
chapter_12
Section titled “chapter_12”node-book_network-automation-cookbook_chapter-12-event-driven-automation-with-ansible — Event-Driven Automation with Ansible
chapter_13
Section titled “chapter_13”node-book_network-automation-cookbook_chapter-13-terraform-and-ansible-dancing-together — Terraform and Ansible Dancing Together
Preface
Section titled “Preface”When the first edition of Network Automation Cookbook was published, the networking industry was at a turning point. Teams around the world were beginning to shift from manual, error-prone configurations toward automation-driven operations. Ansible, with its simple and human-readable approach, stood out as a tool that could bridge the gap between traditional network engineers and modern infrastructure-as-code practices.
Since then, the landscape has evolved dramatically.
In just a few years, Ansible has matured into a cornerstone of network automation. Vendors have expanded support, the community has grown, and new tools have emerged to extend its reach from dynamic inventory management and multi-vendor abstraction layers to event-driven automation and cloud-native networking. At the same time, automation practices have moved beyond one-off tasks and scripts to integrated, production-grade workflows that span physical, virtual, and cloud infrastructure.
This second edition is a reflection of that evolution.
We’ve completely refreshed the book to go beyond just automating CLI commands or pushing configurations. This edition is designed to guide you through building scalable, maintainable, and vendor-agnostic automation solutions using Ansible as the foundation. You’ll find practical recipes across on-premises data center automation, public cloud networking, source-of-truth integration, network validation, event-driven workflows, and the increasingly common need to combine Ansible with Terraform for full stack infrastructure automation.
Each chapter is hands-on, actionable, and focused on solving real-world problems. Whether you’re rendering configurations for Arista, automating a Juniper core, managing Nokia SR Linux devices, or deploying networking in AWS, Azure, or GCP, this book provides a clear and tested approach.
New additions such as Ansible EDA to explore event-driven Ansible for reactive workflows, Nautobot (similar to what you could do with NetBox or Infrahub) for dynamic inventory and source-of-truth-driven automation, and how Terraform and Ansible can coexist to deliver end-to-end automation in heterogeneous environments—take this edition far beyond the basics.
If you’re a network engineer looking to adopt automation, an infrastructure architect aiming to scale network operations, or a developer integrating network changes into CI/CD pipelines, this cookbook is for you.
We hope this second edition helps you move confidently from manual operations to modern, automated network engineering. The recipes in this book are based on real-world experience, field-tested tools, and a passion for making networking more reliable, repeatable, and programmable.
Let’s build the future of networking—one playbook at a time.
Who this book is for
Section titled “Who this book is for”This book is for network and DevOps engineers seeking to enhance their network operations via network automation, leveraging Ansible and other tools that complement it. Basic experience with YAML, Python, and network automation concepts is recommended to get the most out of this book.
What this book covers
Section titled “What this book covers”Chapter 1, Why Ansible for Network Automation?, details the different ways of installing Terraform manually, with scripts, or by using a Docker container, and it also details the Terraform migration configuration process.
Chapter 2, Building Blocks of Ansible, concerns the writing of Terraform configurations for a provider, variables, outputs, built-in functions, condition expressions, Yaml file manipulation, and pre and post conditions.
Chapter 3, Advanced Ansible Concepts, shows you how to build dynamic environments by going further with Terraform configuration writing using loops, maps, and collections.
Chapter 4, Building Data Center Networks with Arista, explores how to use Terraform with external data and local files, and how to execute local programs and scripts with Terraform.
Chapter 5, Automating a Juniper Network Core Using Ansible, explains Terraform State management, including reading, moving, deleting, and importing resources into the Terraform State.
Chapter 6, Managing Nokia SR Devices Using Ansible, explains the use of Terraform’s CLI to validate the configuration, use outputs, destroy resources provisioned by Terraform, use workspaces, generate dependency graphs, and debug the execution of Terraform.
Chapter 7, Administering a Multi-Vendor Network with NAPALM and Ansible, covers the creation, use, and sharing of Terraform modules, and shows testing module practices.
Chapter 8, Deploying and Operating AWS Networking, illustrates the use of Terraform in a practical scenario with the cloud provider Azure. It covers topics such as authentication, remote backends, ARM templates, Azure CLI execution, and Terraform configuration generation for an existing infrastructure.
Chapter 9, Deploying and Operating Azure Networking, provides a starting point for provisioning AWS and GCP infrastructure using Terraform, and includes details on these providers, authentication, and remote backend storage.
Chapter 10, Deploying and Operating GCP Networking, explains how to use Terraform to create Docker containers and deploy Kubernetes resources.
Chapter 11, Network Validation with Batfish, details Terraform configuration testing practices using several tools like Tfsec, OPA, terraform-compliance, and Pester.
Chapter 12, Building a Network Inventory with Nautobot, discusses topics that go further with Terraform, such as the execution of Terraform configuration tests, zero-downtime deployment, Terraform wrappers with Terragrunt, checking configuration using Git-Hook, and using the Terraform CDK as a developer.
Chapter 13, Simplifying Automation with AWX, explores local Terraform automation processes, and implementing a CI/CD pipeline to apply Terraform configuration automatically.
Chapter 14, Event-Driven Automation with Ansible, explains how to use Terraform Cloud to run Terraform in a team with the sharing of Terraform modules in a private registry, the use of remote backends for Terraform State, migrating Terraform State, running Terraform remotely, and integrating cost estimation.
Chapter 15, Terraform and Ansible Dancing Together, lists several Terraform errors and explains how to resolve them.
Chapter 16, Appendix - The Lab, contains a Terraform Cheat Sheet command line and my Terraform resources list.
To get the most out of this book
Section titled “To get the most out of this book”Start setting up your lab environment as described in Chapter 16; all the recipes can be reproduced from it. It is still possible to follow the recipes without using the lab.
Download the example code files
Section titled “Download the example code files”The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Network-Automation-Cookbook_2nd_Edition.
Conventions used
Section titled “Conventions used”There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Run the pb_arista_facts.yml playbook to validate the operational state of our fabric.”
A block of code is set as follows:
---- name: “CREATE OUTPUT CONFIGS DIRECTORY” ansible.builtin.file: path: “{{ config_output_root_dir }}” state: “directory” run_once: yesAny command-line input or output is written as follows:
ansible-playbook pb_validate_facts.ymlPLAY [Collect and Validate Arista DC Fabric Facts] **************TASK [Collect Arista Device Facts] ******************************ok: [ceos-01]ok: [ceos-02]ok: [ceos-03]Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Create a new organization, Network, for all network teams—as shown in the following screenshot—by selecting Access Management and Organizations from the left panel and clicking the Create Organization button.”
Warnings or important notes appear like this.
Tips and tricks appear like this.
Step 1
Section titled “Step 1”Keep your purchase invoice for this book ready, as you’ll need it in Step 3. If you received a physical invoice, scan it on your phone and have it ready as either a PDF, JPG, or PNG.
For more help on finding your invoice, visit
Note: Did you buy this book directly from Packt? You don’t need an invoice. After completing Step 2, you can jump straight to your exclusive content.
Step 3
Section titled “Step 3”Sign in to your Packt account or create a new one for free. Once you’re logged in, upload your invoice. It can be in PDF, PNG, or JPG format and must be no larger than 10 MB. Follow the rest of the instructions on the screen to complete the process.
| ## Need help? If you get stuck and need help, visit for a detailed FAQ on how to find your invoices and more. The following QR code will take you to the help page directly: |
|---|