SysAdmin 101
  • About Me
  • SSH
    • quick notes
    • socks tunneling
  • Tmux
    • quick notes
  • Kubernetes
    • quick notes
    • Setting up a K8S cluster
      • kubespray
      • metalLB
      • Nginx-Ingress
    • Storage
      • Persistent Storage
      • Persistent Storage Claim
  • Ansible
    • quick notes
    • ansible in a container
    • setup Ansible AWX
  • Docker
    • quick notes
    • docker compose
Powered by GitBook
On this page
  1. Ansible

ansible in a container

Using docker container to execute ansible instead of installing it on the hosts.

Note. This assumes you have Docker installed. If you don't, install that first.

First create a Dockerfile with the code below. We will use alpine but any other base is fine.

FROM alpine:3.18

ENV BUILD_PACKAGES \
  bash \
  curl \
  tar \
  openssh-client \
  sshpass \
  git \
  python3 \
  py-boto3 \
  py-dateutil \
  py-httplib2 \
  py3-jinja2 \
  py-paramiko \
  py-pip \
  py-yaml \
  py3-wheel \
  ca-certificates

COPY requirements.txt requirements.txt

RUN set -x && \
    echo "==> Adding build-dependencies..."  && \
    apk --update add --virtual build-dependencies \
    gcc \
    musl-dev \
    libffi-dev \
    openssl-dev \
    python3-dev && \
    \
    echo "==> Upgrading apk and system..."  && \
    apk update && apk upgrade && \
    \
    echo "==> Adding Python runtime..."  && \
    apk add --no-cache ${BUILD_PACKAGES} && \
    pip install --upgrade pip && \
    pip install python-keyczar docker-py && \
    \
    echo "==> Installing ansible..."  && \
    pip install ansible && \
    pip install -r requirements.txt && \
    \
    echo "==> Cleaning up..."  && \
    apk del build-dependencies && \
    rm -rf /var/cache/apk/* && \
    \
    echo "==> Adding hosts for convenience..."  && \
    mkdir -p /etc/ansible /ansible && \
    echo "[local]" >> /etc/ansible/hosts && \
    echo "localhost" >> /etc/ansible/hosts

ENV ANSIBLE_GATHERING smart
ENV ANSIBLE_HOST_KEY_CHECKING false
ENV ANSIBLE_RETRY_FILES_ENABLED false
ENV ANSIBLE_ROLES_PATH /ansible/playbooks/roles
ENV ANSIBLE_SSH_PIPELINING True
ENV PYTHONPATH /ansible/lib
ENV PATH /ansible/bin:$PATH
ENV ANSIBLE_LIBRARY /ansible/library

WORKDIR /ansible/playbooks

ENTRYPOINT ["ansible-playbook"]

Second create a requirements.txt file with the packages we need

ansible==7.6.0
ansible-core==2.14.6
cryptography==41.0.1
jinja2==3.1.2
jmespath==1.0.1
MarkupSafe==2.1.3
netaddr==0.8.0
pbr==5.11.1
ruamel.yaml==0.17.31
ruamel.yaml.clib==0.2.7

Optional: create aliases in your .bashrc file or your shell's equivalent

run docker build -t ansible .

alias ansible='docker run --rm -it -v $(pwd):/ansible/playbooks -v ~/.ssh:/root/.ssh
--entrypoint=ansible ansible'
alias ansible-playbook='docker run --rm -it -v $(pwd):/ansible/playbooks -v ~/.ssh:/r
oot/.ssh  ansible'

With the alias added and sourced, you can run ansible from the container as if it was installed locally on the host.

Previousquick notesNextsetup Ansible AWX

Last updated 1 year ago