Labs / Automation / Network as code

Lab 4 of 5 · Network as code

Ansible: every router from one set of files

Push routes to three routers with Ansible, run it twice, and catch a hand-made change.

Start this lab 6 steps

The idea

Amara runs three ferry-harbour routers. Write down once what they should have, dry-run it, apply it twice, give one harbour a route of its own, and catch the change somebody typed by hand.

Ansible's ping is a login and a Python program
Ansible's ping is a login and a Python program
Variables fill the template, once per router
Variables fill the template, once per router
The handler runs only after a change
The handler runs only after a change
The group's routes, plus one router's own
The group's routes, plus one router's own
The file matches, the router doesn't
The file matches, the router doesn't
Compare what each router runs with its file
Compare what each router runs with its file

Steps

  1. Three routers, one inventory

    Amara looks after the network at Saltmarsh Ferries. Each of its three harbours has a router: kelby, rosk and talham. Head office has put its bookings server on a new network, 10.0.50.0/24, behind its own router core at 10.0.1.5, and every harbour needs a route to it.

    By hand, that's three logins and a line each. But two more harbours open in spring, and typing the same thing into many routers is how routers that should match end up different.

    So Amara uses Ansible: she writes down what the routers should have, and Ansible logs in to each over SSH and makes it so. That's configuration management. Her project lives in /root/harbours on control, and it starts with the inventory, inventory.ini: the routers Ansible manages, in a group called harbours.

    [harbours]
    kelby  ansible_host=10.0.1.2
    rosk   ansible_host=10.0.1.3
    talham ansible_host=10.0.1.4
    

    /etc/ansible/ansible.cfg points Ansible at this file. Ansible does its work through modules, and ansible harbours -m ping runs the one called ping on every router in the group.

    control:~# ansible harbours -m ping
  2. Write it down, then dry-run it

    On these routers FRR does the routing, and it reads its settings from /etc/frr/frr.conf when it starts. Today each router's frr.conf is the one FRR saved when the router was set up by hand.

    Three files say what it should be. group_vars/harbours.yml holds variables for every router in the group:

    routes:
      - prefix: 10.0.50.0/24   # head office: the bookings server
        via: 10.0.1.5
    extra_routes: []   # empty for now
    

    templates/frr.conf.j2 is FRR's file with gaps in it, written in Jinja, Ansible's template language. {{ inventory_hostname }} becomes the router's name, and a loop writes one line per route:

    hostname {{ inventory_hostname }}
    ...
    {% for route in routes + extra_routes %}
    ip route {{ route.prefix }} {{ route.via }}
    {% endfor %}
    

    site.yml, the playbook, lists the tasks. Its first fills in the template for each router and writes the result to that router's frr.conf.

    Before it touches three routers, try it: with --check, each task works out what it would change and stops there, and --diff prints the lines each file would gain or lose.

    control:~# ansible-playbook /root/harbours/site.yml --check --diff
    control:~# ansible talham -a "vtysh -c 'show ip route'"
  3. Run it, then run it again

    Writing the file isn't enough: FRR read frr.conf when it started, and doesn't read it again by itself. So the template task ends with notify: Restart FRR, and the playbook has a handler by that name:

    handlers:
      - name: Restart FRR
        ansible.builtin.shell: |
          /usr/lib/frr/frrinit.sh restart
    

    A handler is a task that waits to be told. It runs once, at the end of the play, and only on a router where a task that notifies it reported a change. (The real one also waits for the routes to come back.)

    A restart isn't free: the router drops its routes for a moment, and a router running OSPF or BGP drops its neighbours too. That's why it should happen only when something changed.

    Run the playbook for real, then run it again straight away.

    control:~# ansible-playbook /root/harbours/site.yml
    control:~# ansible-playbook /root/harbours/site.yml
    talham:~# curl -sS -m 3 http://10.0.50.10/
  4. One harbour is different

    Talham also ships freight, and the freight office's network, 10.0.60.0/24, sits behind core too. Only talham needs a route to it.

    Ansible's answer isn't a second playbook or a copy of the template. A file in host_vars/ named after a router holds variables for that router alone, and a value set there wins over the group's.

    It replaces the group's value whole, lists included: a routes: list there would drop talham's route to bookings. That's why the template has a second list:

    extra_routes: []
    

    host_vars/talham.yml already holds it, empty. The template loops over routes + extra_routes: the group's routes, then the router's own.

    Give talham the freight route, written the way the route in group_vars/harbours.yml is. YAML is fussy about indents: a - starts each route, with its keys lined up under it.

  5. A route typed by hand

    On Thursday night the freight office couldn't reach Rosk. Jonas, on call, logged in to rosk and typed a route by hand: freight's network through talham. It worked, he went back to bed, and he didn't save it or write it down.

    The project is supposed to say what every router runs. Run the playbook and see what it makes of rosk. Then print rosk's routing table with a one-off ansible -a command.

    control:~# ansible-playbook /root/harbours/site.yml
    control:~# ansible rosk -a "vtysh -c 'show ip route'"
  6. Compare the router, not only the file

    The playbook has a second task, off until now:

    - name: Compare the running routes with the file
      ansible.builtin.shell: |
        vtysh -c 'show running-config' | grep '^ip route' | sort > /tmp/running-routes
        grep '^ip route' /etc/frr/frr.conf | sort | diff -U0 - /tmp/running-routes ...
      register: routes_diff
      changed_when: routes_diff.rc == 1
      check_mode: false
      when: compare_running | bool
      notify: Restart FRR
    

    It sorts the routes the router runs and the routes in its file, and diff compares the two lists: - for a route only the file has, + for one only the router runs.

    register keeps the result as routes_diff, and diff exits with 1 when the lists differ, which changed_when turns into a change, which notifies the restart. check_mode: false lets it run even in a dry run, since it only reads. A third task prints the diff.

    when skips all this while the group's compare_running says false. Set it to true in group_vars/harbours.yml.

    control:~# ansible rosk -a "vtysh -c 'show ip route'"

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab