问题描述
我有一个Vagrantfile,可以创建3个服务器.我有两本有趣的剧本.playbook1应该首先在每个服务器上执行.第二本playbook2应该仅在server1上执行,而不应该在server2和server3上执行.
I have a Vagrantfile which creates 3 servers. I have two ansible playbooks. playbook1 should be executed on every server first. The second playbook2 should only be executed on server1 and not on server2 and server3.
如何使用我的Vagrantfile进行管理?
How can I manage this with my Vagrantfile?
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.define "server1" do |server1|
//
end
config.vm.define "server2" do |server2|
//
end
config.vm.define "server3" do |server3|
//
end
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook1.yml"
end
end
上面的代码在所有服务器上执行playbook1.如何为playbook2.yml添加配置,使其仅在server1和之后的playbook1上执行?
The above executes playbook1 on all servers. How can I add config for playbook2.yml to be executed only on server1 and AFTER playbook1?
推荐答案
给出您的示例 Vagrantfile
和理论上的 playbook2.yml
仅在 server2上执行在
server1
上的 playbook1.yml
之后,我们将得出以下解决方案:
Given your example Vagrantfile
and your theoretical playbook2.yml
executing only on server2
after playbook1.yml
on server1
, we would arrive at the following solution:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.define "server1" do |server1|
//
# restrict scope of ansible provisioner to server1 by invoking on its class method off the constructor
server1.vm.provision :ansible do |ansible|
ansible.playbook = 'playbook1.yml'
end
end
config.vm.define "server2" do |server2|
//
# perform similarly for server2, which executes after server1 provisioning due to the imperative ruby dsl
server2.vm.provision :ansible do |ansible|
ansible.playbook = 'playbook2.yml'
end
end
config.vm.define "server3" do |server3|
//
end
end
还值得注意的是,如果您想精确订购,可以先 vagrant up server1
,然后再 vagrant up server2
,而不是多合一无所事事
.
It is also worth noting that if you want to be precise about ordering, you can vagrant up server1
and then vagrant up server2
instead of an all-in-one vagrant up
.
基本上,在 Vagrant.configure
中,有一个范围会影响 config.vm
中的所有VM.您可以像上面一样使用 config.vm.define
实例化,将其范围限制为特定的VM.用 config.vm.define
实例化的对象VM具有与基本 config
相同的成员/属性.
Basically, within Vagrant.configure
there is a scope affecting all VMs within config.vm
. You can restrict its scope to specific VMs by instantiating with config.vm.define
like you do above. Object VMs instantiated with config.vm.define
have the same members/attributes as the base config
.
请注意,您还可以根据需要执行以下操作:
Note you can also do something like this if you want:
Vagrant.configure('2') do |config|
...
(1..3).each do |i|
config.vm.define "server#{i}" do |server|
//
server.vm.provision :ansible do |ansible|
ansible.playbook = "playbook#{i}.yml"
end
end
end
end
针对每个服务器的特定剧本.这取决于每个虚拟机特定于//
的确切内容,以及是否要为第三台虚拟机提供第三本手册.
for a per-server specific playbook. This depends on what exactly is within your //
though specific to each VM, and if you wanted a third playbook for the third VM.
这篇关于如何在特定的流浪汉主机上运行Ansible剧本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!