Search code examples
vagrantvirtualboxansiblevagrant-windows

Vagrant->Ansible: Host file not found, provided hosts list is empty, no hosts matched


Background: I frequently end up working on different laptops running on different operating systems. This means I waste a lot of time re-installing the same programs and applications. I've decided to try and automate this using Vagrant and Ansible.

Problem: As I want this build to be deployable on a range of operating systems I want Vagrant to spin up a simple ubuntu/trusty64 box, and Ansible to be installed and execute on the Ubuntu box, however I'm having trouble with the Ansible hosts. I've read the Ansible docs and have read about inventory however haven't found it very clear how these work or where this should be defined in my setup. For reference I'm new to both Vagrant and Ansible but have experience with VirtualBox. Any help would be much appreciated

Stack trace here:stacktrace

Vagrantfile:

# -*- mode: ruby -*-"
# vi: set ft=ruby :

# vagrant plugin install vagrant-ansible-local

VAGRANTFILE_API_VERSION = "2"

$ansible_install_script = <<SCRIPT
if ! which ansible >/dev/null; then
  apt-get update -y
  apt-get install -y software-properties-common
  apt-add-repository -y ppa:ansible/ansible
  apt-get update -y
  apt-get install -y ansible
fi
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "dev-machine", primary: true do |machine|
    machine.vm.box = "ubuntu/trusty64"
    machine.vm.hostname = 'local.dev-machine.box'
    machine.vm.network :private_network, :ip => '10.20.1.2'

    machine.vm.provider "virtualbox" do |vb|
      vb.gui = true
      vb.memory = "8192"
    end # vb

    machine.vm.provision "shell", inline: $ansible_install_script

    machine.vm.provision "ansibleLocal" do |ansible|
      ansible.guest_folder = "/vagrant-ansible"
      ansible.raw_arguments = "--inventory=/vagrant-ansbile/ansible_hosts"
      ansible.playbook = "playbook.yml"
      ansible.limit = "local.dev-machine.box"
    end # ansible
  end # machine
end # config

playbook.yml:

---
- hosts: all
  become: yes
  become_method: sudo
  tasks:
    - name: Check Ubuntu 14.04 running
      assert:
        that:
          - ansible_distribution == 'Ubuntu'
          - ansible_distribution_release == 'trusty'

    - name: update apt cache
      apt: update_cache=yes

    - name: install git
      apt: name=git-core state=latest

    - name: Install Python 3.4
      apt: name={{items}} state=latest
      with_items:
        - python
        - python-dev
        - python-virtualenv
        - python-setuptools
        - python-pip

Solution

  • First, you have a syntax error in the name of the provisioner - it should be ansible_local not ansibleLocal.

    Second, you seem to just want to run the playbook against a single machine which is the default case. The following definition:

    machine.vm.provision "ansible_local" do |ansible|
      ansible.playbook = "playbook.yml"
    end # ansible
    

    will run the playbook.yml (stored in the Vagrant project directory on the host) using the Ansible executable on the Vagrant box. You don't need to specify any other options for that, Vagrant will automatically provide the inventory file pointing to the local machine as a target.


    As a side note:

    Do not use the Ansible from APT. It's a few generations old (v 1.7.2).

    Instead configure pip and use the current official release from PyPI.