如何在特定的流浪主机上运行 ansible playbook [英] How to run an ansible playbook on a specific vagrant host
问题描述
我有一个创建 3 个服务器的 Vagrantfile.我有两本可靠的剧本.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 上执行
code> 在 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
针对每个服务器的特定剧本.这取决于您的 //
中究竟有什么内容(尽管特定于每个 VM),以及您是否想要第三个 VM 的第三个剧本.
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 playbook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!