我正在尝试使用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等).还有一些 - 为每台机器拥有.但是,一旦我"vagrant up"
只运行第一台机器,配置器运行,失败,因为其他2未创建.是否可以在创建所有机器后运行配置程序?或者我的方法不正确,我应该以其他方式执行此操作?感谢您的时间.
我遇到的第一个问题是ERROR: cannot find role in...
.我假设你有这些角色并为了简洁而排除它们.我的建议是在测试时有一个简单的Ansible剧本:
---
- hosts: all
gather_facts: false
tasks:
- command: hostname -f
其次,手头的问题围绕静态库存文件的使用和其中的警告.您看到一个错误,因为Ansible配置程序在第一台计算机启动后运行时找不到所有主机但其他主机没有找到.
最后,每台机器都有一个不同的密钥,您必须通过.因此,遵循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
end