online posts 16
ansible 101 for networking

task 8.1 · chapter 8 of 8: run it like production · 25 min

Execution environments

objective

Build an execution environment with the lab’s collections, and run site.yml inside it with ansible-navigator.

you edit, in the labexecution-environment.ymlansible-navigator.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Imageans101-ee:1, built by make ee
Switches' networkans101-mgmt
1 explain

What is an Ansible execution environment?

Your playbooks work in this codespace because it has the right Ansible, the right cisco.ios collection and the right Python libraries, like the SSH library network_cli uses. A colleague’s laptop, a CI runner or AAP might have different versions, and “works on my machine” is the result.

An execution environment (EE) is a container image with Ansible and everything your playbooks need, pinned. You build it once and run your playbooks inside it, anywhere. AAP runs every job in one.

  • ansible-builder builds the image from execution-environment.yml.
  • ansible-navigator runs a playbook inside it, instead of with the Ansible on your machine.
2 show

What goes in execution-environment.yml?

version: 3
images:
  base_image:
    name: quay.io/fedora/fedora-minimal:43     # the Linux it starts from
dependencies:
  ansible_core:
    package_pip: ansible-core==2.20.1          # which Ansible, pinned
  ansible_runner:
    package_pip: ansible-runner==2.4.3         # what navigator uses to run playbooks
  python:
    - ansible-pylibssh==1.4.0                  # the SSH library network_cli uses
  galaxy: requirements.yml                     # which collections: the same file the codespace uses

ansible-navigator.yml tells navigator which image to use, and how to run it. The EE is a container, with its own network, so it can’t see the switches unless you connect it to theirs.

3 follow along

The collections, and the build

  1. Point the EE at the same pinned collections the codespace uses: add the galaxy: line at the end of dependencies:, after the python: list that’s already there:

    editexecution-environment.ymlCtrl+P → execution-environment.yml
      python:
        - ansible-pylibssh==1.4.0
      galaxy: requirements.yml
    
  2. Build it. The first build takes a few minutes:

    run in the terminal
    make ee
    
  3. Look inside: docker run --rm ans101-ee:1 ansible-galaxy collection list cisco.ios. Same version as ansible-galaxy collection list cisco.ios in the codespace. That’s the point.

  4. Try running the fabric in it: ansible-navigator run playbooks/site.yml. It fails to reach the switches: the container can’t see them. That’s the challenge.

4 challenge

Reach the switches from inside the EE

Connect the EE to the switches’ management network, ans101-mgmt, so ansible-navigator run playbooks/site.yml works.

hint

ansible-navigator passes extra options to the container engine with container-options:, a list, under execution-environment:.

hint

The Docker option is --network=<name>. If navigator says it can’t resolve spine1, the option is missing or misspelled.

show the solutionansible-navigator.yml

or pull it into your lab: make solve T=8.1, then git diff --staged shows what changed

ansible-navigator:
  mode: stdout
  execution-environment:
    image: ans101-ee:1
    pull:
      policy: never
    container-options: ["--network=ans101-mgmt"]

5 check: run it, then prove it

$ ansible-navigator run playbooks/site.yml
$ make check T=8.1

when it's right, you'll see

╭─ task 8.1 · execution environments
│  ✓ execution environment ans101-ee:1 is built (make ee)
│  ✓ ansible-navigator runs playbooks/site.yml in the EE
╰─ 2/2 checks passed  verified