我正在尝试使用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.11main.yml:
- hosts: all
roles:
- Common
- ConsulServer
- ConsulAgent
- hosts: WebServers
roles:
- WebServer
- hosts: WebLoadBalancers
roles:
- LoadBalancer
- hosts: MasterDbServers
roles:
- DbServer我想买三台机器。它们都必须包含通用的软件(领事服务器和代理、vim等)。每台机器都有一些额外的设备。但是,一旦我只运行第一台创建的"vagrant up",供应程序就会运行,因为其他2台没有创建,所以就会失败。在创建了所有机器之后运行提供程序是否可能?或者我的方法是不正确的,我应该用另一种方式来做这件事?谢谢您抽时间见我。
发布于 2015-12-23 23:25:21
我遇到的第一个问题是ERROR: cannot find role in...。我假设你有这些角色,为了简洁而把它们排除在外。我在这里的建议是,在测试时要有一个简单的Ansible游戏手册:
---
- hosts: all
gather_facts: false
tasks:
- command: hostname -f其次,手头的问题围绕着静态库存文件的使用和其中的注意事项。您正在看到一个错误,因为Ansible provisioner在第一台计算机启动后运行时未能找到所有主机,而其他主机却没有。
最后,每台机器将有一个不同的键,您必须传递。因此,在Vagrant的记录的方法中,使用Ansible和这个工作-的帮助实现多机并行,下面是我推荐的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
endhttps://stackoverflow.com/questions/34444506
复制相似问题