Devcontainers for ansible collection development

I am the maintainer of netscaler.adc ansible collection. I am thinking to add a devcontainer support for the collection.

Any heads up on the devcontainer with respect to ansible collection development please?

Directory Structure:
Ensure your collection has the following structure:
ansible_collections/
└── netscaler
└── adc
β”œβ”€β”€ meta
β”œβ”€β”€ plugins
β”‚ β”œβ”€β”€ inventory
β”‚ β”œβ”€β”€ modules
β”‚ └── module_utils
β”œβ”€β”€ roles
β”œβ”€β”€ tests
β”œβ”€β”€ .devcontainer
β”‚ β”œβ”€β”€ devcontainer.json
β”‚ └── Dockerfile
└── galaxy.yml
Create .devcontainer Directory:
Inside your collection’s root directory, create a .devcontainer folder.
Dockerfile:
Here’s a Dockerfile example specifically for Ansible development:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container to /home/vscode
WORKDIR /home/vscode

# Install system dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    git \
    ssh \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Ansible and related tools
RUN pip install --no-cache-dir \
    ansible \
    ansible-lint \
    molecule \
    docker \
    molecule[docker] \
    testinfra

# Install netscaler.adc collection
RUN ansible-galaxy collection install netscaler.adc -p /usr/share/ansible/collections

# Set up non-root user for VSCode
RUN groupadd --gid 1000 vscode \
    && useradd --uid 1000 --gid 1000 -m vscode \
    && mkdir -p /home/vscode/.vscode-server/extensions \
    && chown -R vscode:vscode /home/vscode

# Make sure the non-root user has the rights to use sudo without password
RUN echo "vscode ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Switch to non-root user
USER vscode

# Set environment variables for non-root user
ENV PATH="/home/vscode/.local/bin:${PATH}"

# Default command when container starts
CMD ["/bin/bash"]

devcontainer.json:
Here’s a devcontainer.json to configure the development environment:

{
    "name": "Ansible Collection Development for netscaler.adc",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "settings": {
        "terminal.integrated.defaultProfile.linux": "bash",
        "python.linting.enabled": true,
        "python.linting.pylintEnabled": true,
        "python.formatting.provider": "black",
        "python.formatting.blackArgs": [
            "--line-length=120"
        ],
        "editor.formatOnSave": true,
        "ansible.python.interpreter": "/usr/local/bin/python"
    },
    "extensions": [
        "ms-python.python",
        "vscoss.vscode-ansible",
        "redhat.ansible",
        "ms-vscode-remote.remote-containers",
        "esbenp.prettier-vscode"
    ],
    "forwardPorts": [],
    "postCreateCommand": "ansible-galaxy collection install --force -p /usr/share/ansible/collections netscaler.adc",
    "remoteUser": "vscode"
}
1 Like