hostvars 中没有 IP 信息

No IP information in hostvars

我的剧本:

- hosts: master
  gather_facts: True
  become: True
  tasks:
    - set_fact: headnode={{ ansible_all_ipv4_addresses[0] }}

- hosts: slave
  become: True
  tasks:
    - debug: msg={{ hostvars.master }}

那里没有事实 headnode。我错过了什么吗?此外,hostvars.master 不包含 ['ansible_eth0'] 那么所有文档都是错误的吗?

这也不起作用:hostvars['master ']['ansible_eth0']['ipv4']['address']
还有这个:hostvars[groups['master'][0]]['ansible_eth0']

http://docs.ansible.com/ansible/playbooks_variables.html#registered-variables

hostvars.master:

ok: [slave0] => {
    "msg": {
        "ansible_check_mode": false,
        "ansible_ssh_host": "xxx.westeurope.cloudapp.azure.com",
        "ansible_ssh_port": 22,
        "ansible_ssh_private_key_file": "/root/.ssh/id_rsa",
        "ansible_ssh_user": "vagrant",
        "ansible_version": {
            "full": "2.2.2.0",
            "major": 2,
            "minor": 2,
            "revision": 2,
            "string": "2.2.2.0"
        },
        "group_names": [
            "master"
        ],
        "groups": {
            "all": [
                "master",
                "slave0"
            ],
            "master": [
                "master"
            ],
            "slave": [
                "slave0"
            ],
            "ungrouped": []
        },
        "inventory_dir": "/xxx/.vagrant/provisioners/ansible/inventory",
        "inventory_file": null,
        "inventory_hostname": "master",
        "inventory_hostname_short": "master",
        "omit": "__omit_place_holder__fb9f90a73039d94f14c1a1f0af132f1c36b9fb4a",
        "playbook_dir": "/xxx/ansible"
    }
}

- debug: msg={{ hostvars.master }} 任务仅显示清单文件中可用的事实。它不包含 IP 地址,也不包含常规安装模块应从远程计算机收集的大量其他信息。

现在,正如您看似 运行 Vagrant 配置中的剧本一样,它可能 运行 剧本两次,首先仅在 master 上,然后在 slave0 上只要。默认情况下,Vagrant 将 --limit 参数传递给 Ansible,将范围限制在当前配置的机器上。

结果,当针对 slave0 执行剧本时,第一个剧本(在 hosts: master 上设置为 运行)未执行,来自属于master 组(在本例中来自也名为 master 的主机)未聚集。


您可以通过设置 ansible.limit = 'all'.

在 Vagrantfile 中更改 slave0 机器的配置范围

或者您可以 运行 ansible-playbook 之后指向 Vagrant 生成的清单文件。