Skip to content

Instantly share code, notes, and snippets.

@intfrr
Forked from yikaus/Ansible-Docker.md
Last active July 26, 2023 10:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intfrr/f67d09aba946cb7d6f6f to your computer and use it in GitHub Desktop.
Save intfrr/f67d09aba946cb7d6f6f to your computer and use it in GitHub Desktop.
Setup Ansible dev environment with docker on Mac

Forked from https://gist.github.com/yikaus/c39ec878ae4735db867f

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 If some errors on ip change it to any different network class

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 centos image

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

docker build -t tutum/centos:centos7 centos7

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 tutum/debian:jessie jessie/

Prepare ssh key

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

Specify the file name as /home/vagrant/.ssh/docker.pem

Start sshd linux images

docker run --name devcentos7 -d -p 2222:22 -e AUTHORIZED_KEYS="cat /home/vagrant/.ssh/docker.pem.pub" tutum/centos:centos7 docker ps -a docker inspect --format '{{ .NetworkSettings.IPAddress }}' [CONTAINER-ID] 172.17.0.11

docker run --name devdebian8 -d -p 2224:22 -e AUTHORIZED_KEYS="cat /home/vagrant/.ssh/docker.pem.pub" tutum/debian:jessie docker ps -a docker inspect --format '{{ .NetworkSettings.IPAddress }}' [CONTAINER-ID] 172.17.0.12

Prepare your ansible setting

Install ansible on vagrant

sudo apt-get install -y ansible

Change the settings from ansible.cfg and inventory file as attached in /etc/ansible/ansible.cfg

Change the /etc/ansible/hosts file as in the inventory file

mkdir ~/dev/work

Test setting with

ansible -m ping all

(If some errors, install python on the guest container)

Use the attached test.yaml to verify docker instance configuration Paste the inventory file here too

vim inventory ansible-playbook -i inventory test.yml --user root

Wait for the results

[defaults]
host_key_checking=False
gathering=explicit
inventory=./inventory
private_key_file=/home/vagrant/.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]
devcentos7 ansible_ssh_host=[CHANGE-IP] ansible_ssh_port=2222 ansible_ssh_user=root
devdebian8 ansible_ssh_host=[CHANGE-IP] 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 = "dockerdev"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment