我正在创建一个名为Ansible剧本的Vagrantfile。哪个运行良好。
我的问题是,由于我在流浪汉上使用“每个”循环进行部署,因此每次创建计算机时都会运行Ansible剧本。预期的行为是,在启动所有VM之后才运行Ansible提供程序。
我的Vagrantfile编码为波纹管:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Define Vagrant API version, the virtualization provider and the location of the param file and its name:
VAGRANTFILE_API_VERSION = "2"
VIRT_PROVIDER="virtualbox"
VM_PARAM_FILE = 'params.yml'
ANSIBLE_PLAYBOOK = "/Users/dzarpelon/Documents/DevOps/Projects/Confluence_Lab/ansible/playbook.yml"
#define secure ssh key location
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/devops_key.pub").first.strip
# Require Yaml module, this is needed to read the 'params.yml' file.
require 'yaml'
# Read 'params.yml' file
servers = YAML.load_file(File.join(File.dirname(__FILE__), VM_PARAM_FILE))
# Create boxes as needed
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provision "ansible" do |ansible|
ansible.playbook = ANSIBLE_PLAYBOOK
ansible.force_remote_user = TRUE
end #end ansible provisioning
# ensure that all Vagrant machines will use the same SSH key pair.
config.ssh.insert_key = false
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box
end #end of cache plugin configuration
if Vagrant.has_plugin?("vagrant-hostmanager")
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
end # end of hostmanager plugin
#execute for each entry on the params file
servers.each do |servers|
config.vm.define servers["name"] do |srv|
srv.vm.box = servers["box"]
srv.vm.network servers["network_type"], ip: servers["ip"]
srv.vm.hostname = servers["name"]
srv.hostsupdater.aliases = [servers["name"]]
srv.vm.provider VIRT_PROVIDER do |v|
v.name = servers["name"]
v.memory = servers["ram"]
end #end provider
end # end config loop
end # end servers loop
end #end of Vagrant.Configure
您可以看到,Ansible设置步骤在“每个”循环之外完成,并且仍然在创建每个VM之后运行。
关于通过该循环创建所有VM之后如何运行Ansible的任何想法?
非常感谢您的帮助!
The fine manual似乎解决了您的非常关注的问题,他们的建议是保护.provision
块仅在最后一台计算机上运行,从而根据他们的文档,确保预配将在最后一台计算机之后运行起来。