找到你要的答案

Q:How to get ip address from other host in ansible?

Q:如何从Ansible其他主机获得IP地址?

I have vagrant configured with multiple machines and 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

And vagrant generate inventory file:

# 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
...

When I run vagrant my_server gets ip:

eth0: 10.0.2.15
eth1: 192.168.50.4

and my_agent gets ip:

eth0: 10.0.2.15
eth1: 192.168.50.5

I want to add in agent configuration ip address of server (from eth1). I try debug informations about server:

- debug: var=hostvars[item]
  with_items: groups['my-server']

but i only gets:

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"
        }
    }
}

Is it possible to get ip address of server in agent role? If it is possible how I can do it?

我有流浪的配置多台机器和安塞波:

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

流浪者生成清单文件:

# 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
...

当我跑的流浪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

I want to add in agent configuration ip address of server (from eth1). I try debug informations about server:

- 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地址?如果可能的话我怎么办?

answer1: 回答1:

I resolve problem. I need add

ansible.limit = "all"

in ansible configuration, because vagrant runs ansible two times: first for my_server and second for my_agent. Second run ansible don't collect informations about my_server. Now ansible runs two times for each server.

Working vagrant configuration:

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 role:

- debug: var=hostvars[item]["ansible_eth1"]["ipv4"]["address"]
  with_items: groups['my-server']
  sudo: yes

我解决问题。我需要加

ansible.limit = "all"

在安塞波配置,因为流浪是Ansible两次:第一次为my_agent my_server和第二。二跑起来不收集信息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

和Ansible剂的作用:

- debug: var=hostvars[item]["ansible_eth1"]["ipv4"]["address"]
  with_items: groups['my-server']
  sudo: yes
vagrant  ansible