Vagrant 多机配置
Vagrant multi-machine provisioning
我正在尝试使用 Ansible 作为供应商在 Vagrant 中创建多机环境。
我的 Vagrantfile 如下所示:
Vagrant.configure("2") do |config|
config.vm.provision "ansible" do |ansible|
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-vvvv"
end
config.vm.define "machine1" do |machine1|
machine1.vm.box = "ubuntu/trusty64"
machine1.vm.network "private_network", ip:"192.168.77.10"
machine1.vm.hostname = "machine1"
machine1.vm.provider :virtualbox do |vb|
vb.name = "machine1"
end
end
config.vm.define "machine2" do |machine2|
machine2.vm.box = "ubuntu/trusty64"
machine2.vm.network "private_network", ip:"192.168.77.11"
machine2.vm.hostname = "machine2"
machine2.vm.provider :virtualbox do |vb|
vb.name = "machine2"
end
end
config.vm.define "machine3" do |machine3|
machine3.vm.box = "ubuntu/trusty64"
machine3.vm.network "private_network", ip:"192.168.77.12"
machine3.vm.hostname = "machine3"
machine3.vm.provider :virtualbox do |vb|
vb.name = "machine3"
end
end
end
库存:
[AppServers]
192.168.77.10
192.168.77.11
192.168.77.12
[WebServers]
192.168.77.11
192.168.77.12
[WebLoadBalancers]
192.168.77.10
[SlaveDbServers]
192.168.77.10
192.168.77.12
[MasterDbServers]
192.168.77.11
[DbLoadBalancers]
192.168.77.11
main.yml:
- hosts: all
roles:
- Common
- ConsulServer
- ConsulAgent
- hosts: WebServers
roles:
- WebServer
- hosts: WebLoadBalancers
roles:
- LoadBalancer
- hosts: MasterDbServers
roles:
- DbServer
我想要3台机器。它们都必须包含通用软件(Consul 服务器和代理,vim 等)。还有一些额外的 - 每台机器都拥有。但是一旦我 运行ning "vagrant up"
只创建了第一台机器,provisioner 运行s 失败,因为其他 2 个未创建。
是否可以在 所有机器创建后 运行 配置器 ?或者我的方法不正确,我应该以其他方式执行此操作?
谢谢你的时间。
我遇到的第一个问题是 ERROR: cannot find role in...
。我假设您拥有这些角色,并为简洁起见将它们排除在外。我的建议是在测试时使用简单的 Ansible 剧本:
---
- hosts: all
gather_facts: false
tasks:
- command: hostname -f
其次,手头的问题围绕着静态清单文件的使用和其中的注意事项。您看到错误是因为 Ansible provisioner 在第一台机器启动但其他机器未启动后运行时无法找到所有主机。
最后,每台机器都有不同的密钥,您必须通过。所以,在 Vagrant 的 documented approach for multi-machine parallelism with Ansible and with help from this work-around 之后,这是我推荐你的 Vagrantfile 的样子:
Vagrant.configure("2") do |config|
N = 3
VAGRANT_VM_PROVIDER = "virtualbox"
ANSIBLE_RAW_SSH_ARGS = []
(1..N-1).each do |machine_id|
ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
end
(1..N).each do |machine_id|
config.vm.define "machine#{machine_id}" do |machine|
machine.vm.box = "ubuntu/trusty64"
machine.vm.hostname = "machine#{machine_id}"
machine.vm.network "private_network", ip: "192.168.77.#{10+machine_id-1}"
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if machine_id == N
machine.vm.provision :ansible do |ansible|
# Disable default limit to connect to all the machines
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-v"
ansible.raw_ssh_args = ANSIBLE_RAW_SSH_ARGS
end
end
end
end
end
我正在尝试使用 Ansible 作为供应商在 Vagrant 中创建多机环境。
我的 Vagrantfile 如下所示:
Vagrant.configure("2") do |config|
config.vm.provision "ansible" do |ansible|
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-vvvv"
end
config.vm.define "machine1" do |machine1|
machine1.vm.box = "ubuntu/trusty64"
machine1.vm.network "private_network", ip:"192.168.77.10"
machine1.vm.hostname = "machine1"
machine1.vm.provider :virtualbox do |vb|
vb.name = "machine1"
end
end
config.vm.define "machine2" do |machine2|
machine2.vm.box = "ubuntu/trusty64"
machine2.vm.network "private_network", ip:"192.168.77.11"
machine2.vm.hostname = "machine2"
machine2.vm.provider :virtualbox do |vb|
vb.name = "machine2"
end
end
config.vm.define "machine3" do |machine3|
machine3.vm.box = "ubuntu/trusty64"
machine3.vm.network "private_network", ip:"192.168.77.12"
machine3.vm.hostname = "machine3"
machine3.vm.provider :virtualbox do |vb|
vb.name = "machine3"
end
end
end
库存:
[AppServers]
192.168.77.10
192.168.77.11
192.168.77.12
[WebServers]
192.168.77.11
192.168.77.12
[WebLoadBalancers]
192.168.77.10
[SlaveDbServers]
192.168.77.10
192.168.77.12
[MasterDbServers]
192.168.77.11
[DbLoadBalancers]
192.168.77.11
main.yml:
- hosts: all
roles:
- Common
- ConsulServer
- ConsulAgent
- hosts: WebServers
roles:
- WebServer
- hosts: WebLoadBalancers
roles:
- LoadBalancer
- hosts: MasterDbServers
roles:
- DbServer
我想要3台机器。它们都必须包含通用软件(Consul 服务器和代理,vim 等)。还有一些额外的 - 每台机器都拥有。但是一旦我 运行ning "vagrant up"
只创建了第一台机器,provisioner 运行s 失败,因为其他 2 个未创建。
是否可以在 所有机器创建后 运行 配置器 ?或者我的方法不正确,我应该以其他方式执行此操作?
谢谢你的时间。
我遇到的第一个问题是 ERROR: cannot find role in...
。我假设您拥有这些角色,并为简洁起见将它们排除在外。我的建议是在测试时使用简单的 Ansible 剧本:
---
- hosts: all
gather_facts: false
tasks:
- command: hostname -f
其次,手头的问题围绕着静态清单文件的使用和其中的注意事项。您看到错误是因为 Ansible provisioner 在第一台机器启动但其他机器未启动后运行时无法找到所有主机。
最后,每台机器都有不同的密钥,您必须通过。所以,在 Vagrant 的 documented approach for multi-machine parallelism with Ansible and with help from this work-around 之后,这是我推荐你的 Vagrantfile 的样子:
Vagrant.configure("2") do |config|
N = 3
VAGRANT_VM_PROVIDER = "virtualbox"
ANSIBLE_RAW_SSH_ARGS = []
(1..N-1).each do |machine_id|
ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
end
(1..N).each do |machine_id|
config.vm.define "machine#{machine_id}" do |machine|
machine.vm.box = "ubuntu/trusty64"
machine.vm.hostname = "machine#{machine_id}"
machine.vm.network "private_network", ip: "192.168.77.#{10+machine_id-1}"
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if machine_id == N
machine.vm.provision :ansible do |ansible|
# Disable default limit to connect to all the machines
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-v"
ansible.raw_ssh_args = ANSIBLE_RAW_SSH_ARGS
end
end
end
end
end