Rapid Ansible development with Docker and Vagrant
Lately im working a lot with Ansible, even if i would prefer an immutable infrastructure approach sometimes you want to test quickly…
Rapid Ansible development with Docker and Vagrant
Lately im working a lot with Ansible, even if i would prefer an immutable infrastructure approach sometimes you want to test quickly locally or the final environment where you plan to deploy doesn’t have the ‘artifact’ concept like an AMI.
Using Vagrant for testing has been the standard defacto for several years, Ansible is a config management tool where you can setup and configure hundreds of services and servers, so even if its possible to test locally you ansible setup with Vagrant and VMs sometimes this is costly and slow, specially when you can abuse a little bit containers to achieve the same thing quickly and with less resources.
We just need to create a Vagrantfile like the following one:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don’t change it unless you know what
# you’re doing.
Vagrant.configure(2) do |config|
config.ssh.username = "root"
config.ssh.private_key_path = "id_dsa"
config.vm.define "srv" do |v|
v.vm.provider "docker" do |d|
d.build_dir = "vagrant_docker"
d.build_args = ["-t", "srv"]
d.name = "srv"
d.remains_running = false
d.create_args = ["-p","2202:22","-it", "— privileged"]
# if you dont plan to use Ansible you can set d.has_ssh = true
# and it will expose a random port and you will be able
# to log in using vagrant ssh srv
end
# you can add several vms with docker provider here
# if you need multiple "vms".
end
We need a Dockerfile to build the image we are going to use, Vagrant will build the image that we specified on build_dir parameter. In my case, i’ve just needed a Debian vanilla image so my Dockerfile is quite simple.
FROM debian:8.6
MAINTAINER fsero
RUN apt-get -y install ssh
RUN mkdir -p /root/.ssh_keys/
RUN mkdir -p /root/.ssh/
COPY id_dsa.pub /root/.ssh_keys/
RUN chmod 0400 /root/.ssh_keys/*
RUN cat /root/.ssh_keys/* >> /root/.ssh/authorized_keys
RUN chmod 0400 /root/.ssh/*
CMD /etc/init.d/ssh start && bash -l
ENTRYPOINT /etc/init.d/ssh start && bash -l
Then we just need to set up our “vm” using vagrant, don’t forget provider parameter, although you can set up docker provider as default one.
vagrant up --provider docker --parallel
if you list your docker images you should see “srv” docker container running.
Then you just need to add this host to your Ansible inventory:
[servers]
srv01.docker.localhost ansible_user=root ansible_port=2202 ansible_become=true hostname=srv01
One of the advantages, besides speed, i’ve seen using this model is that because docker images are really lightweight you will discover hidden dependencies that you might want to pin or remove completely.
It is quite easy to run a full run or run integration tests of your Ansible playbooks in seconds to minutes.
Happy hacking!
메타데이터
- post_id
- 3fb87f0498da
- slug
- rapid-ansible-development-with-docker-and-vagrant-3fb87f0498da
- url
- https://medium.com/@fsero/rapid-ansible-development-with-docker-and-vagrant-3fb87f0498da
- canonical_url
- https://medium.com/@fsero/rapid-ansible-development-with-docker-and-vagrant-3fb87f0498da
- author_url
- https://medium.com/@fsero
- status
- ok
- fetched_at
- 2026-06-10 08:17:25