Skip to content

Instantly share code, notes, and snippets.

@tuksik
Forked from tknerr/README.md
Created September 28, 2017 13:15
Show Gist options
  • Save tuksik/0a4a1bdc93e3853a325175662ab8cf5d to your computer and use it in GitHub Desktop.
Save tuksik/0a4a1bdc93e3853a325175662ab8cf5d to your computer and use it in GitHub Desktop.
Vagrant with Ansible Provisioner on Windows

Vagrant with Ansible Provisioner on Windows

Long story short, ansible does not work on a Windows control machine, so you basically have to:

  • either run ansible --connection=local ... in the target vm
  • set up a separate control vm where ansible is installed via shell provisioner

Below are Vagrantfile examples for both approaches

Within the Target VM

This requires the vagrant-guest_ansible plugin to be installed (v0.0.2.dev, see this pull request).

Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "hansode/centos-6.5-x86_64"

  provisioner = Vagrant::Util::Platform.windows? ? :guest_ansible : :ansible

  config.vm.define "web" do |web|
    web.vm.network :private_network, ip: "192.168.33.13"
    web.vm.hostname = "web"
    web.vm.provision provisioner do |ansible|
      ansible.playbook = "provisioning/playbook.yml"
    end
  end

  config.vm.define "db" do |db|
    db.vm.network :private_network, ip: "192.168.33.14"
    db.vm.hostname = "db"
    db.vm.provision provisioner do |ansible|
      ansible.playbook = "provisioning/playbook.yml"
    end
  end

end

From a Separate Control VM

This does not require any additional plugins, but needs a shell script to install ansible in the control VM:

Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "hansode/centos-6.5-x86_64"

  config.vm.define "web" do |web|
    web.vm.network :private_network, ip: "192.168.33.13"
    web.vm.hostname = "web"
    web.vm.synced_folder ".", "/vagrant", disabled: true
  end

  config.vm.define "db" do |db|
    db.vm.network :private_network, ip: "192.168.33.14"
    db.vm.hostname = "db"
    db.vm.synced_folder ".", "/vagrant", disabled: true
  end

  #
  # this is our ansible controller VM which provisions the other VMs
  #
  config.vm.define "controller" do |controller|
    controller.vm.network :private_network, ip: "192.168.33.15"
    controller.vm.hostname = "controller"
    # install ansible
    controller.vm.provision "shell", privileged: false, path: "install_ansible.sh"
    # run ansible
    controller.vm.provision "shell", privileged: false, inline: <<-EOF
      if [ ! -f /home/vagrant/.ssh/id_rsa ]; then
        wget --no-check-certificate https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant -O /home/vagrant/.ssh/id_rsa
        wget --no-check-certificate https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/id_rsa.pub
        chmod 600 /home/vagrant/.ssh/id_*
      fi
      rm -rf /tmp/provisioning
      cp -r /vagrant/provisioning /tmp/provisioning
      cd /tmp/provisioning
      chmod -x hosts
      export ANSIBLE_HOST_KEY_CHECKING=False
      ansible-playbook playbook.yml --inventory-file=hosts
    EOF
  end

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