Ansible not executing host specific playbook in Vagrant multi-machine provisioning

ansiblevagrant

When provisioning with ansible in Vagrant, I am unable to get server/role specific playbooks to run. There is a common playbook run for all hosts, and that is working, but when trying to target a specific role, I'm missing something. Any ideas what piece is missing?

The ansible output is:

Running Ansible playbook...

PLAY [all] ********************************************************************


GATHERING FACTS ***************************************************************

ok: [127.0.0.1]

TASK: [common | ensure apt-get is up to date] *********************************

ok: [127.0.0.1]

TASK: [common | ensure build-essential installed] *****************************

changed: [127.0.0.1]

TASK: [common | ensure git is installed] **************************************

changed: [127.0.0.1]

TASK: [common | ensure curl is installed] *************************************

changed: [127.0.0.1]

PLAY [zookeeper_servers] ******************************************************

skipping: no hosts matched

PLAY [kafka_servers] **********************************************************

skipping: no hosts matched

PLAY [storm_servers] **********************************************************

skipping: no hosts matched

PLAY RECAP ********************************************************************

127.0.0.1                  : ok=5    changed=3    unreachable=0    failed=0

With the following directory structure:

- Vagrantfile
  - vagrant
    - playbook.yml
    - hosts
    - roles
      - common
        - tasks
          - main.yml
      - kafka_servers
        - tasks
          - main.yml
      - storm_servers
        - tasks
          - main.yml
      - zookeeper_servers
        - tasks
          - main.yml

# The main playbook

- hosts: all
  roles:
   - common

- hosts: zookeeper_servers
  roles:
  - { role: zookeeper_servers }

- hosts: kafka_servers
  roles:
  - { role: kafka_servers }

- hosts: storm_servers
  roles:
  - { role: storm_servers }

The hosts file contains

[zookeeper_servers]
zookeeper1 zoo_id=1

[storm_servers]
storm1

[kafka_servers]
kafka1

And the Vagrant file contains

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

CUSTOM_CONFIG = {
                  "BOX_NAME"  =>  "precise64",
                  "BOX_URL"   =>  "http://files.vagrantup.com/precise64.box",
                  "HEADLESS"  =>  false
                }

Vagrant.configure("2") do |config|
  config.vm.define "kafka" do |kafka|
    kafka.vm.box = CUSTOM_CONFIG['BOX_NAME']
  end

  config.vm.define "zookeeper" do |zookeeper|
    zookeeper.vm.box = CUSTOM_CONFIG['BOX_NAME']
  end

  config.vm.define "storm" do |storm|
    storm.vm.box = CUSTOM_CONFIG['BOX_NAME']
  end

  if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM)
    # provisioning with ansible on windows
    config.vm.provision "shell", path: "./vagrant/ansible-windows.sh"
  else
    # provisioning with ansible
    config.vm.provision :ansible do |ansible|
      ansible.playbook = "./vagrant/playbook.yml"
      ansible.inventory_path = "./vagrant/roles"
    end
  end
end

Best Answer

Vagrant ignores your inventory file, it creates it's own (source).

You need to specify groups in Vagrantfile:

ansible.groups = {
 "group1" => ["machine1"],
 "group2" => ["machine2", "machine3"],
 "all_groups:children" => ["group1", "group2", "group3"]
}
Related Topic