Testing Ansible Playbook in a Docker Container

├── ansible
│ ├── env
│ │ └── local_docker
│ ├── roles
│ │ └── role1
│ │ └── tasks
│ │ └── main.yml
│ ├── myplaybook.yml
├── container-start-and-playbook-run.sh
└── docker
├── Dockerfile
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server sudo python unzip
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
[local]
ansible-test ansible_connection=docker
---
- hosts: local
become: true
roles:
- role1
#!/bin/bash

DOCKER_CONTAINER_NAME="ansible-test"

cd docker && docker build -t myubuntu .

docker run -ti --privileged --name $DOCKER_CONTAINER_NAME -d -p 5000:22 myubuntu

cd ../ansible && ansible-playbook -i env/local_docker myplaybook.yml -vvv
docker stop $DOCKER_CONTAINER_NAME

docker rm $DOCKER_CONTAINER_NAME

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store