This is the simplest Dockerfile
able to run Ansible playbooks.
I’m assuming that this Dockerfile
is on the same directory/repository as your ansible
files.
Dockerfile
FROM alpine:3.16.0
COPY . /ansible
WORKDIR /ansible
RUN apk add --update-cache \
python3=3.10.4-r0 \
py3-pip=22.1.1-r0 \
openssh-client==9.0_p1-r1 && \
rm -rf /var/cache/apk/*
RUN pip3 install -r requirements.txt
requirements.txt
ansible==6.1.0
ansible-core==2.13.1
appdirs==1.4.4
boto3==1.24.31
botocore==1.27.31
cffi==1.15.1
contextlib2==21.6.0
cryptography==37.0.4
Jinja2==3.1.2
jmespath==1.0.1
MarkupSafe==2.1.1
more-itertools==8.13.0
ordered-set==4.0.2
packaging==21.3
pep517==0.12.0
pip==22.1.1
pycparser==2.21
pyparsing==2.4.7
python-dateutil==2.8.2
PyYAML==6.0
resolvelib==0.8.1
retrying==1.3.3
s3transfer==0.6.0
setuptools==59.4.0
six==1.16.0
tomli==2.0.1
urllib3==1.26.10
Example of .dockerignore
. Docker will ignore these files during the image build and will not copy it to the container.
/.vscode/
Makefile
Dockerfile
.dockerignore
.gitlab-ci.yml
.git
.gitignore
As a plus, here you can find a Makefile
to help to build, run and few other tasks. During the run
, this Makefile
gets AWS credentials for your local configuration and export it to the container using environment variables. You can also change your private key before run make run
.
.ONESHELL :
CURRENT_DIR := $(shell pwd)
CONTAINER_NAME := "ansible"
CONTAINER_IMAGE := "ansible:6.1.0"
CONTAINER_APP_DIR := "/ansible"
AWS_ACCESS_KEY_ID := $(if $(AWS_ACCESS_KEY_ID),$(AWS_ACCESS_KEY_ID),$(shell aws --profile default configure get aws_access_key_id))
AWS_SECRET_ACCESS_KEY := $(if $(AWS_SECRET_ACCESS_KEY),$(AWS_SECRET_ACCESS_KEY),$(shell aws --profile default configure get aws_secret_access_key))
SSH_PRIVATE_KEY := $(HOME)/.ssh/id_rsa
# HELP
# This will output the help for each task
# thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help
help: ## This help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
# DOCKER TASKS
.PHONY : build
build: ## Build the container
@echo Building $(CONTAINER_IMAGE)
@docker build -t $(CONTAINER_IMAGE) .
.PHONY : run
run: ## Run the container
@echo Running $(CONTAINER_IMAGE)
@docker run --rm \
-e AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) \
-e AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) \
-v $(SSH_PRIVATE_KEY):/root/.ssh/id_rsa \
-w $(CONTAINER_APP_DIR) \
--name $(CONTAINER_NAME) \
-it $(CONTAINER_IMAGE) sh
.PHONY : stop
stop: ## Stops the container
@echo Stopping $(CONTAINER_IMAGE)
@docker stop $(CONTAINER_NAME)
.PHONY : rm
rm: ## Removes the container
@echo Removing $(CONTAINER_IMAGE)
@docker rm -f $(CONTAINER_NAME)