Tech · · 4 min read · Part 2 of 3

Ansible Dev Server using VS Code Dev Containers

A consistent Ansible development environment for the whole team, defined once in a file

Every Ansible team eventually loses an afternoon to version drift: one person’s ansible-lint flags what another’s doesn’t, or a playbook works on one laptop and fails on the next. A dev container fixes that by giving everyone the same environment, defined once in a file and rebuilt identically on every machine.

What is a dev container?

A dev container is a preconfigured, isolated development environment that runs inside a container, with all the tools, libraries and dependencies a project needs. Instead of setting up each machine by hand, you describe the environment once, in a devcontainer.json file, and anyone on the team can start an identical one.

In VS Code, the Dev Containers extension does the work. When you open a folder that contains a devcontainer.json file, VS Code builds or pulls the container, then attaches the editor to it. Your terminal, your extensions and your tools all run inside.

For Ansible, that means the Ansible Development Tools (ADT) come preinstalled:

  • ansible-core, ansible-builder, ansible-creator, ansible-dev-environment;
  • ansible-lint, ansible-navigator, ansible-sign;
  • molecule, pytest-ansible, tox-ansible.

The team can start writing, linting, testing and running playbooks straight away.

Why use it

  • Everything runs on your workstation. No remote server to set up or pay for.
  • The complete toolkit, at the same versions for everyone.
  • The full Ansible extension, including execution environments and Red Hat Ansible Lightspeed.

What you need in return:

  • Podman or Docker, plus WSL if you’re on Windows;
  • access to registry.redhat.io, for Red Hat’s container image;
  • access to your private automation hub, if you use custom execution environments.

Prerequisites

  • Podman, Podman Desktop, Docker or Docker Desktop.
  • A Red Hat login, and logged in to the Red Hat registry (registry.redhat.io). Also log in to your private automation hub, if you use one.
  • VS Code, with:
  • On Windows: launch VS Code and connect to your WSL machine first.

If you use Podman

Tell the Dev Containers extension to use Podman instead of Docker.

  1. In VS Code, open the Settings editor.
  2. Search for @ext:ms-vscode-remote.remote-containers. Or click the Extensions icon in the activity bar, then the gear icon on the Dev Containers extension.
  3. Set Dev › Containers: Docker Path to podman.
  4. Set Dev › Containers: Docker Compose Path to podman-compose.

Prepare Podman. On macOS and Windows, Podman runs containers inside a small virtual machine. Create it once and log in to the registry:

podman machine init
podman machine start
podman login registry.redhat.io

Podman Desktop can do the same from its interface: create a Podman machine, then add the registry.redhat.io registry.

Install the Ansible Development Tools in a dev container

  1. In VS Code, open your project folder.

  2. Click the Ansible icon in the activity bar to open the Ansible extension.

  3. In the Ansible Development Tools section, select Devcontainer.

  4. On the Create a devcontainer page, choose the Downstream container image, which is Red Hat’s supported image, then Create. The extension writes devcontainer.json files for both Podman and Docker into a .devcontainer folder in your project.

    The Create a devcontainer page in VS Code, with the destination directory, the Downstream container image from registry.redhat.io selected, and the Create button

  5. Click Open Devcontainer. When VS Code finds the devcontainer.json file, it offers to Reopen in Container. Click it. If the notification doesn’t appear, click the Remote indicator at the bottom left and choose Reopen in Container.

    VS Code’s notification offering to reopen the folder in a container

  6. Choose the dev container for Podman or for Docker, whichever you use. VS Code reads the configuration and pulls the image; the first time takes a few minutes.

    VS Code’s progress notification: Reading Dev Container Configuration

  7. Once the folder reopens inside the container, the Remote indicator at the bottom left shows Dev Container: ansible-dev-container-podman (or -docker).

    VS Code’s status bar showing Dev Container: ansible-dev-container-podman

  8. You can now develop Ansible content inside the dev container.

    VS Code with an Ansible project open inside the dev container: the window title and Explorer name the dev container, and the project’s .devcontainer folder is listed

  9. Check that the tools and some collections are already installed, from the terminal inside VS Code:

    ansible-galaxy collection list
    rpm -qa | grep ansible

    The terminal inside the dev container listing preinstalled collections such as ansible.netcommon and ansible.utils, and the installed Ansible packages

Share it with your team

Commit the .devcontainer folder to your repository. Anyone who clones it and opens it in VS Code gets the same offer to reopen in a container, with the same tools at the same versions. Update the image once, and the whole team moves together.

Reference: Installing Ansible development tools on a container inside VS Code, Red Hat Ansible Automation Platform 2.6 documentation.

Note I used AI assistance to draft or edit this post, working from my own notes and experience, and I reviewed every change. The opinions and any mistakes are mine.