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
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
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/hashicorp/vagrant/master/keys/vagrant -O /home/vagrant/.ssh/id_rsa
wget --no-check-certificate https://raw.githubusercontent.com/hashicorp/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
Wish I had looked at the "see also" first, since this functionality (run Ansible within guest VM) has been part of Vagrant officially since 1.8: https://www.vagrantup.com/docs/provisioning/ansible_local.html