我让vagrant配置了多台机器和ansible:
config.vm.box = "ubuntu/trusty64"
config.vm.define "my_server" do |my_server|
my_server.vm.network "private_network", ip: "192.168.50.4"
end
config.vm.define "my_agent" do |my_agent|
my_agent.vm.network "private_network", ip: "192.168.50.5"
end
config.vm.provision "ansible" do |ansible|
ansible.groups = {
"my-server" => ["my_server"],
"my-agent" => ["my_agent"],
"all_groups:children" => ["my-server", "my-agent"]
}
ansible.playbook = "./ansible/my.yml"
end
并且vagrant生成库存文件:
# Generated by Vagrant
my_server ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_private_key_file=/.../private_key
my_agent ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_private_key_file=/.../private_key
...
当我运行vagrant my_server
获取ip:
eth0: 10.0.2.15
eth1: 192.168.50.4
和my_agent
获取ip:
eth0: 10.0.2.15
eth1: 192.168.50.5
我想添加服务器的代理配置ip地址(来自eth1)。 我尝试调试有关服务器的信息:
- debug: var=hostvars[item]
with_items: groups['my-server']
但我只得到:
ok: [my_agent] => (item=my_server) => {
"item": "my_server",
"var": {
"hostvars[item]": {
"ansible_ssh_host": "127.0.0.1",
"ansible_ssh_port": 2222,
"ansible_ssh_private_key_file": ".../private_key",
"group_names": [
"all_groups",
"my-server"
],
"inventory_hostname": "my_server",
"inventory_hostname_short": "my_server"
}
}
}
是否可以在代理角色中获取服务器的IP地址?如果有可能我怎么做呢?
答案 0 :(得分:4)
我解决问题。我需要添加
ansible.limit = "all"
在ansible配置中,因为vagrant运行了两次ansible:首先是my_server,第二次是my_agent
。第二次回复并不收集有关my_server
的信息。现在ansible为每个服务器运行两次。
工作流浪汉配置:
config.vm.provision "ansible" do |ansible|
ansible.groups = {
"my-server" => ["my_server"],
"my-agent" => ["my_agent"],
"all_groups:children" => ["my-server", "my-agent"]
}
ansible.limit = "all"
ansible.playbook = "./ansible/my.yml"
end
and ansible agent角色:
- debug: var=hostvars[item]["ansible_eth1"]["ipv4"]["address"]
with_items: groups['my-server']
sudo: yes