Building Ansible Docker Image with Python 3

Bulding Ansible docker image.

  • To build docker image
docker build -t sfgroups/ansible-python3 .
  • To Run the Ansible docker image
docker run --rm --name ansible -it sfgroups/ansible-python3 ansible --version
  • Mount SSH keys and playbooks, this will give the shell
docker run --rm -it \
        --name ansible \
        -v ~/.ssh:/root/.ssh:ro \
        -v ~/.kube:/.kube \
        -v $(pwd):/playbooks \
        sfgroups/ansible-python3
  • Test the SSH connection to the hosts
ansible -i hosts all -m ping
  • Collect host information
ansible -i hosts all -m setup

Dockerfile use to build this image

FROM python:rc-alpine

RUN	apk --update add \
  bash \
		curl \
		ca-certificates \
		git \
		less \
		openssl \
		openssh-client \
		p7zip \
		py-lxml \
		py-pip \		
    	&& apk --update add --virtual \
		build-dependencies \
		libffi-dev \
		openssl-dev \
		build-base \
	&& pip install --upgrade \
		pip \		
	&& pip install \
		ansible>=2.8.0 \		
		docker-py \
		dopy  \
		pywinrm  \
		pyvmomi \
		pysphere \
		dictdiffer \
		jinja2 \
		python-string-utils \
		ruamel.yaml \
		six \
                ansible-cmdb \
	&& apk del build-dependencies \
	&& rm -rf /var/cache/apk/*

RUN	mkdir -p /etc/ansible \		
	&& echo 'localhost' > /etc/ansible/hosts \			
	&& mkdir -p ~/.ssh && touch ~/.ssh/known_hosts \
	&& mkdir -p /playbooks
COPY bashrc /root/.bashrc

WORKDIR /playbooks
RUN ansible -c local -m setup all > /dev/null
ENV PATH="/:${PATH}"
CMD ["bash"]

Copyright © 2004, Software Groups