Ansible 在 Vagrant 多机配置中未执行特定于主机的剧本

Ansible 在 Vagrant 多机配置中未执行特定于主机的剧本

在 Vagrant 中使用 ansible 进行配置时,我无法运行特定于服务器/角色的剧本。有一个适用于所有主机的通用剧本,并且该剧本可以正常工作,但是当尝试针对特定角色时,我遗漏了一些东西。有什么想法缺少什么吗?

ansible 输出是:

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

具有以下目录结构:

- 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 }

hosts 文件包含

[zookeeper_servers]
zookeeper1 zoo_id=1

[storm_servers]
storm1

[kafka_servers]
kafka1

Vagrant 文件包含

# -*- 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

答案1

Vagrant 会忽略你的库存文件,它会创建自己的(来源)。

您需要在中指定组Vagrant文​​件

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

答案2

我发现的一个问题是 ansible.inventory_path 被设置为一个目录,并且需要位于 hosts 文件中

  ansible.inventory_path = "./vagrant/hosts"

而不是它指向的目录

  ansible.inventory_path = "./vagrant/roles"

现在,我让特定角色的剧本在我的 Mac 上正常运行。Windows 端仍然不知道如何让它工作。

相关内容