├── .github ├── FUNDING.yml └── workflows │ └── build.yml ├── Dockerfile ├── LICENSE ├── README.md └── initctl_faker /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | --- 3 | github: geerlingguy 4 | patreon: geerlingguy 5 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Build 3 | 4 | on: 5 | pull_request: 6 | push: 7 | branches: 8 | - master 9 | schedule: 10 | - cron: "10 1 * * 0" 11 | 12 | jobs: 13 | # Test the image builds and works correctly. 14 | test: 15 | name: Test 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | 21 | - name: Test building image. 22 | run: docker build -t docker-ansible . 23 | 24 | - name: Run the built image. 25 | run: >- 26 | docker run --name test-container -d --privileged 27 | -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host 28 | docker-ansible 29 | 30 | - name: Verify Ansible is accessible in the built image. 31 | run: docker exec --tty test-container env TERM=xterm ansible --version 32 | 33 | # If on master branch, build and release image. 34 | release: 35 | name: Release 36 | runs-on: ubuntu-latest 37 | needs: test 38 | if: github.ref == 'refs/heads/master' 39 | 40 | steps: 41 | - uses: actions/checkout@v2 42 | - uses: docker/setup-qemu-action@v1 43 | - uses: docker/setup-buildx-action@v1 44 | 45 | - name: Login to DockerHub 46 | uses: docker/login-action@v1 47 | with: 48 | username: ${{ secrets.DOCKERHUB_USERNAME }} 49 | password: ${{ secrets.DOCKERHUB_TOKEN }} 50 | 51 | - name: Build and push image. 52 | uses: docker/build-push-action@v2 53 | with: 54 | context: ./ 55 | file: Dockerfile 56 | platforms: linux/amd64,linux/arm64 57 | push: true 58 | tags: ${{ github.repository }}:latest 59 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:20.04 2 | LABEL maintainer="Jeff Geerling" 3 | 4 | ARG DEBIAN_FRONTEND=noninteractive 5 | 6 | ENV pip_packages "ansible" 7 | 8 | # Install dependencies. 9 | RUN apt-get update \ 10 | && apt-get install -y --no-install-recommends \ 11 | apt-utils \ 12 | build-essential \ 13 | locales \ 14 | libffi-dev \ 15 | libssl-dev \ 16 | libyaml-dev \ 17 | python3-dev \ 18 | python3-setuptools \ 19 | python3-pip \ 20 | python3-yaml \ 21 | software-properties-common \ 22 | rsyslog systemd systemd-cron sudo iproute2 \ 23 | && apt-get clean \ 24 | && rm -Rf /var/lib/apt/lists/* \ 25 | && rm -Rf /usr/share/doc && rm -Rf /usr/share/man 26 | RUN sed -i 's/^\($ModLoad imklog\)/#\1/' /etc/rsyslog.conf 27 | 28 | # Fix potential UTF-8 errors with ansible-test. 29 | RUN locale-gen en_US.UTF-8 30 | 31 | # Install Ansible via Pip. 32 | RUN pip3 install $pip_packages 33 | 34 | COPY initctl_faker . 35 | RUN chmod +x initctl_faker && rm -fr /sbin/initctl && ln -s /initctl_faker /sbin/initctl 36 | 37 | # Install Ansible inventory file. 38 | RUN mkdir -p /etc/ansible 39 | RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts 40 | 41 | # Remove unnecessary getty and udev targets that result in high CPU usage when using 42 | # multiple containers with Molecule (https://github.com/ansible/molecule/issues/1104) 43 | RUN rm -f /lib/systemd/system/systemd*udev* \ 44 | && rm -f /lib/systemd/system/getty.target 45 | 46 | VOLUME ["/sys/fs/cgroup", "/tmp", "/run"] 47 | CMD ["/lib/systemd/systemd"] 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ubuntu 20.04 LTS (Focal Fossa) Ansible Test Image 2 | 3 | [![Build](https://github.com/geerlingguy/docker-ubuntu2004-ansible/actions/workflows/build.yml/badge.svg)](https://github.com/geerlingguy/docker-ubuntu2004-ansible/actions/workflows/build.yml) [![Docker pulls](https://img.shields.io/docker/pulls/geerlingguy/docker-ubuntu2004-ansible)](https://hub.docker.com/r/geerlingguy/docker-ubuntu2004-ansible/) 4 | 5 | Ubuntu 20.04 LTS (Focal Fossa) Docker container for Ansible playbook and role testing. 6 | 7 | ## Tags 8 | 9 | - `latest`: Latest stable version of Ansible. 10 | 11 | The latest tag is a lightweight image for basic validation of Ansible playbooks. 12 | 13 | ## How to Build 14 | 15 | This image is built on Docker Hub automatically any time the upstream OS container is rebuilt, and any time a commit is made or merged to the `master` branch. But if you need to build the image on your own locally, do the following: 16 | 17 | 1. [Install Docker](https://docs.docker.com/install/). 18 | 2. `cd` into this directory. 19 | 3. Run `docker build -t ubuntu2004-ansible .` 20 | 21 | ## How to Use 22 | 23 | 1. [Install Docker](https://docs.docker.com/engine/installation/). 24 | 2. Pull this image from Docker Hub: `docker pull geerlingguy/docker-ubuntu2004-ansible:latest` (or use the image you built earlier, e.g. `ubuntu2004-ansible:latest`). 25 | 3. Run a container from the image: `docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host geerlingguy/docker-ubuntu2004-ansible:latest` (to test my Ansible roles, I add in a volume mounted from the current working directory with ``--volume=`pwd`:/etc/ansible/roles/role_under_test:ro``). 26 | 4. Use Ansible inside the container: 27 | a. `docker exec --tty [container_id] env TERM=xterm ansible --version` 28 | b. `docker exec --tty [container_id] env TERM=xterm ansible-playbook /path/to/ansible/playbook.yml --syntax-check` 29 | 30 | ## Notes 31 | 32 | I use Docker to test my Ansible roles and playbooks on multiple OSes using CI tools like Jenkins and Travis. This container allows me to test roles and playbooks using Ansible running locally inside the container. 33 | 34 | > **Important Note**: I use this image for testing in an isolated environment—not for production—and the settings and configuration used may not be suitable for a secure and performant production environment. Use on production servers/in the wild at your own risk! 35 | 36 | ## Author 37 | 38 | Created in 2020 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 39 | -------------------------------------------------------------------------------- /initctl_faker: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ALIAS_CMD="$(echo ""$0"" | sed -e 's?/sbin/??')" 3 | 4 | case "$ALIAS_CMD" in 5 | start|stop|restart|reload|status) 6 | exec service $1 $ALIAS_CMD 7 | ;; 8 | esac 9 | 10 | case "$1" in 11 | list ) 12 | exec service --status-all 13 | ;; 14 | reload-configuration ) 15 | exec service $2 restart 16 | ;; 17 | start|stop|restart|reload|status) 18 | exec service $2 $1 19 | ;; 20 | \?) 21 | exit 0 22 | ;; 23 | esac 24 | --------------------------------------------------------------------------------