title | layout | nav_order | grand_parent | parent | permalink | last_modified_date |
---|---|---|---|---|---|---|
Ansible Playbooks on Docker |
home |
1 |
Docker |
Dockerfiles |
docs/docker/dockerfiles/ansible |
2024-01-13 |
To test Ansible playbooks on Docker, you can use a Dockerfile to create a Docker image with Ansible installed. Then, you can run a container based on that image and execute your playbooks inside the container. Here's a basic example of a Dockerfile that installs Ansible:
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install --no-install-recommends -y python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN pip3 install pip --upgrade
RUN pip3 install ansible
RUN apt-get update -y && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
sshpass
WORKDIR /work
docker build -t ansible:ubuntu-latest .
docker run -v "${PWD}":/work:ro -v ~/.ansible/roles:/root/.ansible/roles -v ~/.ssh:/root/.ssh:ro --rm ansible ansible-playbook playbook.yaml ;
Here is a step-by-step breakdown of the Dockerfile:
- It starts with a base image ubuntu:latest.
- It updates the package list and installs python3-pip without recommended packages.
- It upgrades pip and installs ansible.
- It updates the package list again, and installs sshpass without recommended packages.
- Finally, it sets the working directory to /work.
This Dockerfile sets up an environment with Ubuntu, Python, Pip, Ansible, and SSHpass.