Ansible Network Automation Recipes: Multivendor Guide For 2025

ST
Written by
Integrated MA, Certified Relationship Coach

Modern network infrastructures demand efficiency, reliability, and scalability. Manual configurations no longer suffice in environments spanning multiple vendors and hybrid setups. Enter network automation tools like Ansible, which streamline operations through code-driven approaches. This article explores practical recipes inspired by leading resources in the field, focusing on real-world applications for configuring, validating, and managing diverse networks. We’ll delve into core concepts, step-by-step implementations, and advanced techniques to elevate your automation game.

Why Ansible Stands Out for Network Tasks

Ansible has revolutionized network engineering by providing an agentless, declarative automation framework. Unlike traditional scripting, it uses YAML-based playbooks that are human-readable and idempotent—meaning repeated runs yield consistent results without unintended changes. For networks, Ansible connects via SSH or API, supporting devices from Cisco, Juniper, Arista, and more.

Key advantages include:

  • Simplicity: No need for custom agents; leverage existing protocols.
  • Scalability: Handle hundreds of devices in parallel.
  • Integration: Pairs seamlessly with tools like NAPALM for vendor-agnostic operations.

In multivendor setups, Ansible abstracts hardware differences, allowing uniform playbooks across ecosystems. This reduces errors and accelerates deployments.

Foundational Recipes for Device Discovery and Inventory

Start with building a dynamic inventory. Static lists are brittle; instead, use Ansible’s inventory plugins to pull data from sources like NetBox or CSV files. Here’s a basic recipe for inventory management:

  1. Install Ansible and the ansible.netcommon collection: ansible-galaxy collection install ansible.netcommon.
  2. Create an inventory file (hosts.yml):
all:  children:    routers:      hosts:        router1.example.com:          ansible_host: 192.168.1.1          ansible_network_os: cisco.ios.ios        router2.example.com:          ansible_host: 192.168.1.2          ansible_network_os: junipernetworks.junos.junos

Test connectivity: ansible routers -m ansible.netcommon.net_ping. This verifies reachability across vendors.

Dynamic inventories shine in large networks. Integrate with Nautobot or NetBox via plugins, auto-populating devices based on IPAM data. Recipe tip: Use ansible-inventory --graph to visualize hierarchies.

Streamlining Configurations with Playbooks

Configuration management is a cornerstone. A sample playbook backs up and applies configs:

---- name: Manage Router Configs  hosts: routers  gather_facts: no  tasks:    - name: Backup config      cisco.ios.ios_config:        backup: yes    - name: Apply NTP config      cisco.ios.ios_config:        lines:          - ntp server 1.1.1.1

For multivendor support, standardize tasks. Juniper uses junipernetworks.junos.junos_config, but abstraction layers simplify this.

VendorModuleExample Task
Cisco IOSios_configBackup and apply lines
Juniper Junosjunos_configLoad and commit sets
Arista EOSarista.eos.eos_configReplace or merge configs

Idempotency ensures safe reruns. Always validate with check_mode: yes for dry runs.

Integrating NAPALM for Unified Operations

NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) complements Ansible by providing a unified API. The ansible.netcommon.net_napalm_* modules enable tasks like config retrieval and compliance checks.

Recipe: Validate configurations across vendors.

- name: NAPALM validation  napalm_config:    hostname: '{{ ansible_host }}'    username: admin    dev_os: ios    config_file: intended_config.txt    test: true    replace: false  register: napalm_output

NAPALM diff outputs highlight discrepancies, crucial for compliance. Extend to get facts: napalm_get_facts fetches interfaces, BGP neighbors, etc., in a normalized JSON format.

Pro tip: Combine with Ansible’s template module to generate vendor-specific configs from Jinja2 templates, ensuring consistency.

Advanced Workflows: Validation and Compliance

Beyond basics, enforce policies with Batfish or pyATS. A recipe for pre-deployment validation uses Batfish for topology analysis.

  1. Export configs to Batfish workspace.
  2. Run queries: Reachability, BGP best paths.
  3. Integrate via Ansible: Parse results and fail playbooks on violations.

Compliance checking: Use napalm_validate against golden configs. Store baselines in Git, pull via git module.

Scaling tip: Leverage Ansible Tower/AWX for orchestration, role-based access, and scheduling.

Real-World Case Studies in Hybrid Environments

Consider a hybrid cloud setup with on-prem Cisco/Juniper and AWS Transit Gateways. Recipe: Automate VPC peering and BGP setup.

  • Use aws_ec2 modules alongside network playbooks.
  • Ensure idempotency across realms.

In data centers, automate EVPN fabrics. Playbooks deploy symmetric IRB, validate with NAPALM, and monitor via Prometheus exporters.

Challenges: Handling stateful changes like interface flaps. Mitigate with serial: 1 for sequential execution or retries.

Best Practices for Production Automation

Succeed with these guidelines:

  • Version Control: Git all playbooks; use branches for testing.
  • Testing: Molecule for role testing; ansible-lint for quality.
  • Security: Vault for secrets; limit sudo scopes.
  • Monitoring: Callback plugins for logging; integrate with ELK stack.

Start small: Automate one task, like SNMP config, then expand. Measure ROI via reduced MTTR.

Tools Ecosystem Expansion

Beyond Ansible/NAPALM:

  • Nautobot: Source-of-truth for inventories.
  • NetBox: IPAM and DCIM integration.
  • Ansible Navigator: CLI for execution insights.

Future-proof with Nornir for Python devs, bridging to Ansible.

Common Pitfalls and Troubleshooting

Avoid these:

  • Ignoring platform quirks: Test per-vendor.
  • Overlooking facts caching: Use facts_cache: jsonfile.
  • Debugging: Enable -vvv; check register vars.

For NAPALM errors, verify library versions match device OS.

FAQs

What is the best way to start with network automation?

Install Ansible, build a simple inventory, and run ping modules on lab devices.

Does Ansible support all network vendors?

Yes, via collections for major platforms; NAPALM fills gaps.

How do I handle config drifts?

Regular backups and NAPALM diffs detect changes; schedule via cron/AWX.

Is coding experience required?

No, YAML playbooks are declarative and beginner-friendly.

Conclusion

Network automation transforms chaos into order. By mastering Ansible recipes for inventory, configs, and validation—bolstered by NAPALM—you achieve multivendor mastery. Experiment in labs, iterate, and scale. Resources like dedicated cookbooks provide hundreds of copy-paste-ready examples, accelerating your journey to automation proficiency. Embrace code-defined networks for tomorrow’s demands.

References

  1. Ansible for Network Automation Documentation — Red Hat Ansible. 2025-03-15. https://docs.ansible.com/ansible/latest/network/index.html
  2. NAPALM User Guide — NAPALM Project. 2024-11-20. https://napalm.readthedocs.io/en/latest/
  3. Network Automation Cookbook, 2nd Edition — Packt Publishing / Christian Adell et al. 2024-06-01. https://www.packtpub.com/en-us/product/network-automation-cookbook-9781836209988
  4. Ansible Network Collections — Ansible Galaxy. 2025-04-10. https://galaxy.ansible.com/ui/repo/published/ansible/network/
  5. Cisco DevNet: Ansible Automation — Cisco Systems. 2025-01-22. https://developer.cisco.com/docs/ansible/
‹›