title: Multi-machine ansible setup with vagrant on windows
body: <h1>Purpose</h1>
  
  <p>I want ansible to provision virtual box vm's on my windows 8 machine [via Vagrant]. Everything needs to run locally and since Ansible doesn't run on Windows, I bootstrap a debian vm with ansible as the control machine. <a href="https://github.com/KSid/windows-vagrant-ansible" rel="nofollow">This code</a> served as an example.
  After struggling with the system I got it somewhat working, but not completely (although ansible doesn't tell me).</p>
  
  <h1>Question</h1>
  
  <p><strong>What configuration is required for a multi-machine setup using ansible [in a vm], vagrant and virtualbox [on windows host]</strong> if we want:</p>
  
  <ol>
  <li><p>ssh acces from the host machine to the ansible-vm as well as all the slaves</p></li>
  <li><p>ssh acces from the ansible-vm to all the slaves</p></li>
  <li><p>being able to shield the multi-machine network from the host's network, if possible</p></li>
  </ol>
  
  <h1>Problem</h1>
  
  <p>Running <code>ansible -m ping -all -i path-to-hosts</code> yields ssh errors. It seems ansible tries to reach the machines named web1 and db1, but can't find such hosts.</p>
  
  <blockquote>
    <p> ESTABLISH CONNECTION FOR USER: vagrant
     REMOTE_MODULE ping
     ESTABLISH CONNECTION FOR USER: vagrant
     REMOTE_MODULE ping
     EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'web1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762 &amp;&amp; chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762 &amp;&amp; echo $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762'"]
    EXEC previous known host file not found for web1
     EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'db1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922 &amp;&amp; chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922 &amp;&amp; echo $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922'"]
    EXEC previous known host file not found for db1
    web1 | FAILED => SSH encountered an unknown error. The output was:
    OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug1: auto-mux: Trying existing master
    debug1: Control socket "/home/vagrant/.ansible/cp/ansible-ssh-web1-22-vagrant" does not exist
    debug2: ssh_connect: needpriv 0
    ssh: Could not resolve hostname web1: Name or service not known</p>
    
    <p>db1 | FAILED => SSH encountered an unknown error. The output was:
    OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug1: auto-mux: Trying existing master
    debug1: Control socket "/home/vagrant/.ansible/cp/ansible-ssh-db1-22-vagrant" does not exist
    debug2: ssh_connect: needpriv 0
    ssh: Could not resolve hostname db1: Name or service not known</p>
  </blockquote>
  
  <h1>Code</h1>
  
  <p>The following code tries to provision 
  1. ansible-master: the control machine running ansible
  1. db1: a database server
  1. web1: a web server</p>
  
  <h2>Vagrantfile</h2>
  
  <pre class="lang-rb prettyprint-override"><code>Vagrant.configure("2") do |config|
    config.vm.box = "wheezy64"
    config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/debian-70rc1-x64-vbox4210.box"
    config.vm.synced_folder ".", "/vagrant", :mount_options =&gt; ['dmode=777','fmode=666']
  
    config.vm.network :public_network
  
    config.vm.provider "virtualbox" do |v|
        v.customize [
            "modifyvm", :id,
            "--groups", "/Vagrant/Ansible",
           # "--natdnshostresolver1", "on"
        ]
    end
  
    config.vm.define :ansiblemaster do |ansiblemaster|
     # ansiblemaster.vm.network :private_network, ip: "192.168.111.101"
     ansiblemaster.vm.hostname = "ansiblemaster"
     # ansiblemaster.vm.network :forwarded_port, guest: 80, host: 8080
      ansiblemaster.ssh.forward_agent = true
  
  
      ansiblemaster.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", 512]
        vb.customize ["modifyvm", :id, "--name", "ansible-master"]
        vb.name = "ansiblemaster"
      end
  
      ansiblemaster.vm.provision :shell, :inline =&gt;
        "if [[ ! -f /apt-get-run ]]; then sudo apt-get update &amp;&amp; sudo touch /apt-get-run; fi"
  
  
  
      ansiblemaster.vm.provision :shell do |sh|
        sh.path = "provision.sh"
        sh.args = "./ansible provisioning/site.yml provisioning/hosts/dev_hosts"
      end
    end
  
    config.vm.define :web1 do |slave|
     slave.vm.hostname = "web1"
     # slave.vm.network :private_network, ip: "192.168.111.201"
      slave.vm.synced_folder "./src", "/var/www/site", id: "proj-root"
      slave.vm.provider :virtualbox do |vb|
        vb.name = "web1"
        vb.customize ["modifyvm", :id, "--memory", "512"]
      end
    end
  
    config.vm.define :db1 do |slave|
      slave.vm.hostname = "db1"
      #slave.vm.network :private_network, ip: "192.168.111.202"
      slave.vm.provider :virtualbox do |vb|
        vb.name = "db1"
        vb.customize ["modifyvm", :id, "--memory", "512"]
      end
    end
  end
  </code></pre>
  
  <h2>Provision.sh</h2>
  
  <pre class="lang-bsh prettyprint-override"><code>#!/bin/bash
  
  ANSIBLE_DIR=$1
  ANSIBLE_PLAYBOOK=$2
  ANSIBLE_HOSTS=$3
  TEMP_HOSTS="/tmp/ansible_hosts"
  
  if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
    echo "Cannot find Ansible playbook"
    exit 1
  fi
  
  if [ ! -f /vagrant/$ANSIBLE_HOSTS ]; then
    echo "Cannot find Ansible hosts"
    exit 2
  fi
  
  if [ ! -d $ANSIBLE_DIR ]; then
    echo "Updating apt cache"
    apt-get update
    echo "Installing Ansible dependencies and Git"
    apt-get install -y git python-yaml python-paramiko python-jinja2
    echo "Cloning Ansible"
    git clone git://github.com/ansible/ansible.git ${ANSIBLE_DIR}
  fi
  
  cd ${ANSIBLE_DIR}
  cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} &amp;&amp; chmod -x ${TEMP_HOSTS}
  echo "Running Ansible"
  echo "dir is nu: " $(pwd)
  
  source hacking/env-setup
  echo "source ${ANSIBLE_DIR}/hacking/env-setup" &gt;&gt; /home/vagrant/.bashrc
  ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --connection=local
  
  rm ${TEMP_HOSTS}
  </code></pre>
  
  <h2>provsioning/hosts/dev_hosts</h2>
  
  <pre><code>[webservers]
  web1
  
  [dbservers]
  db1
  </code></pre>