Skip to content

Instantly share code, notes, and snippets.

@yikaus
Last active January 16, 2022 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yikaus/c39ec878ae4735db867f to your computer and use it in GitHub Desktop.
Save yikaus/c39ec878ae4735db867f to your computer and use it in GitHub Desktop.
Setup Ansible dev environment with docker on Mac

Example setup on Mac

Prepare docker vm

People can use boot2docker or docker machine to provison a docker vm , I would like use vagrant myself to better understand the architecture.

install vagrant & vbox (5.0.3) https://www.virtualbox.org/download/testcase/VirtualBox-5.0.3-102165-OSX.dmg

Use attached Vagrantfile to provision jessie64

Put it into your vm dir , eg. ~/dev/vm/jessie and then

cd ~/dev/vm/jessie && vagrant up

~vm$ sudo echo 'deb http://http.debian.net/debian-backports squeeze-backports(-sloppy) main' > /etc/apt/sources.list.d/backports.list

~vm$ sudo apt-get update && sudo apt-get install docker.io && sudo gpasswd -a vagrant docker && sudo service docker restart

create dockervm.command as attached and drag it into OSX dock

Prepare your docker images with tutum sshd images

Build debian image

git clone https://github.com/tutumcloud/tutum-debian

vi wheezy/Dockerfile

add line "apt-get install -y python" and save

docker build -t local/debian wheezy/

Build centos image

git clone https://github.com/tutumcloud/tutum-centos

docker build -t local/centos centos6

Prepare ssh key

ssh-keygen -t rsa -b 4096 -C "ansible@local"

Specify the file name as ~/.ssh/docker.pem

Start sshd linux images

docker run --name dev -d -p 2222:22 -e AUTHORIZED_KEYS="cat ~/.ssh/docker.pem.pub" local/debian

docker run --name dev2 -d -p 2224:22 -e AUTHORIZED_KEYS="cat ~/.ssh/docker.pem.pub" local/centos

docker ps

Prepare your ansible setting

mkdir ~/dev/work

Use ansible.cfg and inventory file as attached

test setting with

ansible -m ping all

Use the attached test.yaml to verify docker instance configuration

[defaults]
host_key_checking=False
gathering=explicit
inventory=./inventory
private_key_file=~/.ssh/docker.pem
#!/bin/bash
cd ~/dev/vm/jessie
if ! nc -z 127.0.0.1 2222
then
/usr/local/bin/vagrant up
fi
/usr/local/bin/vagrant ssh
[test]
test1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user=root
test2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2224 ansible_ssh_user=root
---
- hosts: test
sudo: no
gather_facts: yes
tasks:
- apt: name='wget' state=present
when: ansible_os_family == "Debian"
- yum: name='wget' state=present
when: ansible_os_family == "RedHat"
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "debian/jessie64"
config.vm.box_check_update = false
config.vm.hostname = "docekrdev"
config.vm.network "private_network", ip: "192.168.0.3"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.synced_folder ".", "/vagrant", mount_options: ["dmode=775","fmode=666"]
config.vm.synced_folder "~/dev", "/home/vagrant/dev", mount_options: ["dmode=775","fmode=666"]
#config.vm.network "public_network"
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
#vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
end
@intfrr
Copy link

intfrr commented Nov 25, 2015

Hi there. I can't understand this line. How does this works? Where do I have to put the inventory file? The hosts file is omitted, why?

@yikaus
Copy link
Author

yikaus commented Dec 1, 2015

As my ansible.cfg defined , inventory=./inventory , and example inventory also included in this gist .
You should able to run this example with below file structure
./ansible.cfg
./test.yml
./inventory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment