Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@tknerr

This comment has been minimized.

Owner

tknerr commented Feb 18, 2015

@metamaker

This comment has been minimized.

metamaker commented Aug 12, 2017

@tknerr Thanks for described solutions. Strange that it got so few starts ;/

@michaellihs

This comment has been minimized.

michaellihs commented Jun 21, 2018

Hi @tknerr - just stumbled over this and thought I'd say "Hello!" :)

@lakshmiNdevulapalli

This comment has been minimized.

lakshmiNdevulapalli commented Oct 2, 2018

Thanks for the solution @tkneer. It worked for me! I was literally banging my head and tried 100 different workarounds, but finally your solution worked for me.

Cheers,
Bala

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