Ansible - Adding Arista and Multi-Vendor Playbooks

Ansible - Adding Arista and Multi-Vendor Playbooks
Table of Contents

This post is part of an ongoing series - see all the posts below.

Ansible Course

So far, every device in our lab has been a Cisco switch. But very few real networks are made up of a single vendor. You might have Cisco in the campus, Arista in the data centre, and a few firewalls from yet another vendor. One of the biggest benefits of Ansible is that you can manage all of them from the same project, using the same inventory, the same playbooks, and the same way of working.

In this part, we'll add Arista switches to our lab. Arista is a good first step into multi-vendor automation, because just like Cisco, Ansible connects to it over SSH using network_cli. So most of what we've learned so far applies straight away, and we can focus on the differences.

Lab Topology

For this part, I've rebuilt the lab from scratch, so all the config we added in the previous chapters, such as NTP, syslog, and interface descriptions, is gone. Every device starts with only the default config created by Netlab.

As always, your lab doesn’t need to match mine exactly. All you need is a mix of Cisco and Arista devices, and it doesn’t matter how many of each you have or how they’re connected. Even one Cisco switch and one Arista switch is enough to follow along with this chapter.

The topology is the same as before, with one change. access2 and access3 are now Arista cEOS switches, while core1, core2, and access1 are still Cisco IOL. Keeping the same topology means the only thing that's different is the vendor, which makes it easy to compare the two side by side.

If you want to follow along, you can get the cEOS image for free by creating an account on the Arista website. I've covered how to download and import it in this blog post.

https://www.packetswitch.co.uk/netlab-the-fastest-way-to-build-network-labs/

The Arista Collection

Just like Cisco has the cisco.ios collection, Arista has the arista.eos collection, which contains all the modules and plugins for managing Arista EOS devices. It comes with the ansible package, so there's nothing extra to install. You can confirm it's there by running the following command.

ansible-galaxy collection list arista.eos

If you look through the documentation for the collection, you'll notice that most of the module names are very similar to the Cisco ones. For example, ios_command becomes eos_command, ios_facts becomes eos_facts, and ios_ntp_global becomes eos_ntp_global.

Updating the Inventory

When we first created inventory groups, we added a cisco group to hold the Cisco connection settings, and I mentioned that you might add another vendor in the future. Now we have exactly that situation.

The interesting part is that access2 and access3 are still access switches, they're just made by a different vendor. So each device now needs to belong to two kinds of groups, one for its vendor and one for its role. A device can be a member of as many groups as you like, so this isn't a problem.

---
all:
  children:
    cisco:
      hosts:
        core1:
          ansible_host: 192.168.202.101
        core2:
          ansible_host: 192.168.202.102
        access1:
          ansible_host: 192.168.202.103
    arista:
      hosts:
        access2:
          ansible_host: 192.168.202.104
        access3:
          ansible_host: 192.168.202.105
    core:
      hosts:
        core1:
        core2:
    access:
      hosts:
        access1:
        access2:
        access3:

The cisco and arista groups list each device along with its IP address. The core and access groups list the same devices again, but without any variables. Since the IP addresses are already defined under the vendor groups, we only need the device names here. Let's check how the inventory looks now.

ansible-inventory --graph
@all:
  |--@ungrouped:
  |--@cisco:
  |  |--core1
  |  |--core2
  |  |--access1
  |--@arista:
  |  |--access2
  |  |--access3
  |--@core:
  |  |--core1
  |  |--core2
  |--@access:
  |  |--access1
  |  |--access2
  |  |--access3

Every device appears under its vendor group and its role group. You'll also see a group called ungrouped. This is another built-in group, which holds any devices that aren't part of any other group. Since all our devices belong to a group, it's empty.

With this setup, we can target devices by vendor, by role, or a mix of both. For example, hosts: arista runs against the two Arista switches, hosts: access runs against all three access switches regardless of the vendor.

Group Variables for Arista

Next, we need to tell Ansible how to connect to the Arista switches. Just like we did for Cisco, create a new directory called group_vars/arista/ with a file called vars.yml.

group_vars/
├── arista/
│   └── vars.yml
└── cisco/
    └── vars.yml
---
ansible_network_os: arista.eos.eos
ansible_connection: ansible.netcommon.network_cli
ansible_user: admin
ansible_password: admin
ansible_become: true
ansible_become_method: enable
ansible_become_password: admin

Most of this will look familiar. ansible_network_os is set to arista.eos.eos, which tells Ansible these are Arista EOS devices, and we're still using network_cli to connect over SSH.

The last three variables are new. When you log in to a network device, you're usually placed in one of two modes. In user EXEC mode, the prompt ends with >, and you can only run a limited set of commands. To configure anything, you first need to type enable to move into privileged EXEC mode, where the prompt ends with #. These three variables tell Ansible to do the same thing after it logs in.

  • ansible_become - Tells Ansible that it needs to elevate its privileges after logging in.
  • ansible_become_method - How to elevate the privileges. For network devices, this is enable.
  • ansible_become_password - The enable password, if one is configured.

Arista's own documentation recommends using become with enable when connecting with network_cli, which is why we've added it here. My lab doesn't have an enable password configured on the Arista switches, so ansible_become_password isn't strictly needed. I've included it anyway, in case your lab does have one set.

You might be wondering why we didn't need these for the Cisco switches. In my lab, the admin user on the Cisco switches logs straight into privileged EXEC mode, so there's no need to run enable. As we mentioned earlier in the book, if your Cisco devices log you into user EXEC mode instead, you'd add the same three variables to group_vars/cisco/vars.yml. Let's check the variables for access2.

ansible-inventory --host access2
{
  "ansible_become": true,
  "ansible_become_method": "enable",
  "ansible_become_password": "admin",
  "ansible_connection": "ansible.netcommon.network_cli",
  "ansible_host": "192.168.202.104",
  "ansible_network_os": "arista.eos.eos",
  "ansible_password": "admin",
  "ansible_user": "admin"
}

access2 gets its IP address from the inventory and everything else from group_vars/arista/vars.yml. None of the Cisco settings apply to it, since it's not part of the cisco group.

Running Commands on Arista

Let's make sure Ansible can connect to the Arista switches by running show version as an ad-hoc command. This time, we use the eos_command module instead of ios_command.

ansible arista -m arista.eos.eos_command -a "commands='show version'"
access2 | SUCCESS => {
  "changed": false,
  "stdout": [
    "..."
  ],
  "stdout_lines": [
    [
      "Arista cEOSLab",
      "Hardware version: ",
      "Serial number: A4A5B2490CA0B061B6F71595E36C769B",
      "Hardware MAC address: 001c.73eb.1913",
      "System MAC address: 001c.73eb.1913",
      "",
      "Software image version: 4.34.2F-43232954.4342F.1 (engineering build)",
      "Architecture: i686",
      ...
    ]
  ]
}
access3 | SUCCESS => {
  ...
}

Both Arista switches respond, and the output looks exactly like what we've seen with Cisco, with stdout and stdout_lines. Only the module name and the device output are different.

Since we changed the inventory, it's also worth making sure the Cisco switches still work.

ansible cisco -m cisco.ios.ios_command -a "commands='show version | incl RELEASE'"
access1 | SUCCESS => {
  "changed": false,
  "stdout": [
    "..."
  ],
  "stdout_lines": [
    [
      "Cisco IOS Software [Dublin], Linux Software (X86_64BI_LINUX_L2-ADVENTERPRISEK9-M), Version 17.12.1, RELEASE SOFTWARE (fc5)"
    ]
  ]
}
core2 | SUCCESS => {
  ...
}
core1 | SUCCESS => {
  ...
}

Gathering Facts on Arista

Gathering facts works the same way too, using the eos_facts module.

ansible access2 -m arista.eos.eos_facts
access2 | SUCCESS => {
  "ansible_facts": {
    "ansible_net_api": "cliconf",
    "ansible_net_fqdn": "access2",
    "ansible_net_gather_network_resources": [],
    "ansible_net_gather_subset": [
      "default"
    ],
    "ansible_net_hostname": "access2",
    "ansible_net_model": "cEOSLab",
    "ansible_net_python_version": "3.12.10",
    "ansible_net_serialnum": "A4A5B2490CA0B061B6F71595E36C769B",
    "ansible_net_system": "eos",
    "ansible_net_version": "4.34.2F-43232954.4342F.1 (engineering build)",
    "ansible_network_resources": {}
  },
  "changed": false
}

Many of the facts have the same names as the Cisco ones, such as ansible_net_hostname, ansible_net_version, and ansible_net_serialnum. There are also a few differences. Arista gives us the model, cEOSLab, and the FQDN, but not the image name. This is a good reminder to always check the documentation for each vendor's facts module, rather than assuming the facts will be identical.

Configuring NTP and Syslog on Arista

Now let's configure NTP and syslog on the Arista switches, just like we did for Cisco in the previous chapter. Arista has its own resource modules for both, eos_ntp_global and eos_logging_global. Create a new playbook called arista_ntp_syslog.yml.

---
- name: Configure NTP and syslog on Arista
  hosts: arista
  gather_facts: false

  tasks:
    - name: Configure NTP servers
      arista.eos.eos_ntp_global:
        config:
          servers:
            - server: 10.10.10.13
            - server: 10.10.10.14
      notify: Save config

    - name: Configure syslog servers
      arista.eos.eos_logging_global:
        config:
          hosts:
            - name: 10.10.10.50
            - name: 10.10.10.51
      notify: Save config

  handlers:
    - name: Save config
      arista.eos.eos_config:
        save_when: always

If you compare this with the Cisco playbook from the previous chapter, it's almost identical. The module names start with arista.eos.eos_ instead of cisco.ios.ios_, and the handler uses eos_config to save the config.

But if you look closely, there's one important difference. For the syslog servers, the Cisco module uses host as the key, while the Arista module uses name. The NTP servers, on the other hand, use server for both vendors. I only found this by checking the documentation for each module with ansible-doc. Resource modules from different vendors often look very similar, but the data they expect isn't always identical, so it's always worth checking.

Let's run the playbook.

ansible-playbook arista_ntp_syslog.yml
PLAY [Configure NTP and syslog on Arista] *************

TASK [Configure NTP servers] **************************
changed: [access3]
changed: [access2]

TASK [Configure syslog servers] ***********************
changed: [access2]
changed: [access3]

RUNNING HANDLER [Save config] *************************
changed: [access3]
changed: [access2]

PLAY RECAP ********************************************
access2  : ok=3  changed=3  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
access3  : ok=3  changed=3  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0

Both tasks made changes on both switches, so the handler saved the config. Let's check the config on access2.

access2#show running-config | section ntp
ntp server 10.10.10.13
ntp server 10.10.10.14

access2#show running-config | section logging
logging monitor debugging
logging host 10.10.10.50
logging host 10.10.10.51

The NTP and syslog servers have been added. If we run the playbook again, everything shows ok, and the handler doesn't run, just like it did with Cisco.

PLAY [Configure NTP and syslog on Arista] *************

TASK [Configure NTP servers] **************************
ok: [access3]
ok: [access2]

TASK [Configure syslog servers] ***********************
ok: [access3]
ok: [access2]

PLAY RECAP ********************************************
access2  : ok=2  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
access3  : ok=2  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.