The goal here is to easily set up an environment to test out ansible commands, playbooks or roles. The various hosts are setup as Docker containers running Debian Jessie, SSH and Python.
You need to have Docker, Docker-compose and Python installed. You may choose to have Ansible installed globally, or you can use a virtual environment. You also need to have the port 2222 and a few more after that open for local connections.
git clone https://github.com/kbairak/ansible-playground
cd ansible-playground
makemake makes sure that the containers will use your public key to authenticate
SSH connections and also builds the image for the containers (based on
krlmlr/debian-ssh).
If you want to install Ansible in a virtual environment:
mkvirtualenv ansible
pip install ansible./bin/add_host webserverThis command edits your docker-compose.yml, ssh.config and inventory
files so that you can target a host named 'webserver' in your ansible commands
and playbooks. Feel free to add as many hosts as you like. Also feel free to
check the modified files in order to understand what went on there.
make up # Runs `docker-compose up -d`
ansible webserver -m pingIf everything went smoothly, you should see this:
webserver | SUCCESS => {
"changed": false,
"ping": "pong"
}
-
As long as you don't remove anything from your
docker-compose.ymlfile, you should be able to remove all your containers with:make down # Runs `docker-compose down` -
If you want to stop the containers so that you don't lose any changes you might have made to them, run
make stop # Runs `docker-compose stop`instead. You can restart them later with:
make restart # Runs `docker-compose restart` -
If you remove some services from
docker-compose.ymlwhile their respective containers are running, thendocker-composewon't be able to find them. In this case, look them up with:docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9fcc0e9a172 debian-ssh-python "/run.sh" 4 minutes ago Up 4 minutes 0.0.0.0:2222->22/tcp ansibleplayground_webserver_1
The names added by
docker-composeshould make it easy to identify and stop/remove them.docker stop ansibleplayground_webserver_1 docker rm -f ansibleplayground_webserver_1