As part of my first steps with Ansible, I was looking for the most efficient way to install Ansible. And since I don’t have a purpose-built lab and wanted to run it on my MacBook, installing it in a docker container offered the smallest footprint option.
Install Docker
Installing Docker Dekstop interactively is a very straightforward process, especially on macOS or Linux but either way, they have comprehensive documentation available on Docker Docs about how to install it on macOS, Linux and Windows.
If you are using Windows, unlike me, there are a few quirks around the Windows Subsystem for Linux (WSL2) but that is all neatly explained on Docker Docs – I found that I had to update my work laptop to switch to WSL2 from 1 etc.
Create a Dockerfile
This file tells the docker builder what should be provisioned in a container and the snippet below covers the installation of Linux packages, ansible collections for Azure and NetApp as well as the needed python wheels.
FROM centos:7
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
RUN yum check-update; \
yum install -y gcc libffi-devel python3 epel-release; \
yum install -y python3-pip; \
yum install -y wget; \
yum clean all
RUN pip3 install --upgrade pip; \
pip3 install --upgrade virtualenv; \
pip3 install pywinrm[kerberos]; \
pip3 install pywinrm; \
pip3 install jmspath; \
pip3 install netapp-lib; \
pip3 install requests; \
pip3 install paramiko; \
python3 -m pip install ansible; \
ansible-galaxy collection install azure.azcollection; \
pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt; \
ansible-galaxy collection install netapp.ontap; \
pip3 install -r ~/.ansible/collections/ansible_collections/netapp/ontap/requirements.txt
The last two lines can be repeated and customized to install other collections if needed: the first one installs the collection itself and the second one addresses the python requirements within a collection which uses pip to install the python wheels required (when a collection is installed you can browse collections/ansible_collections path to discover the full path to the requirements file).
ansible-galaxy collection install netapp.ontap; \
pip3 install -r ~/.ansible/collections/ansible_collections/netapp/ontap/requirements.txt
Install Ansible by building the docker image
The following command “docker build . -t ansible” processes the docker file created above and will create the container (called “ansible”), update, and install the relevant packages via yum and pip as well as download and install the chosen ansible collection.
[+] Building 271.4s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.21kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 1.3s
=> [1/3] FROM docker.io/library/centos:7@sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407 0.0s
=> CACHED [2/3] RUN yum check-update; yum install -y gcc libffi-devel python3 epel-release; yum install -y python3-pip; yum insta 0.0s
=> [3/3] RUN pip3 install --upgrade pip; pip3 install --upgrade virtualenv; pip3 install pywinrm[kerberos]; pip3 install pywinr 252.0s
=> exporting to image 18.0s
=> => exporting layers 18.0s
=> => writing image sha256:e15538cddc3f668d54d203d5674d7f07dd9340e3b760b6b4882851eec48fd3d1 0.0s
=> => naming to docker.io/library/ansible 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Finally, start and use the container
By running “docker run -it ansible”, that container will start and we should be able to connect to it and interact with ansible. In fact the “-it” option included connects us to the shell directly, ready to run our playbooks.