hostvars中没有IP信息

时间:2017-04-02 10:38:59

标签: vagrant ansible

我的手册:

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

1 个答案:

答案 0 :(得分:0)

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

现在,当你看似在Vagrant配置中运行剧本时,它可能会运行剧本两次,首先只在master,然后只在slave0。默认情况下,Vagrant将--limit参数传递给Ansible,将范围限制为仅限当前配置的计算机。

结果,当对slave0执行游戏手册时,第一个游戏(设置为在hosts: master上运行)不会被执行,并且来自属于master组的主机的事实(在这种情况下,主机也名为master)未收集。

您可以通过设置slave0来更改Vagrantfile中ansible.limit = 'all'计算机的配置范围。

或者您可以在指向Vagrant生成的库存文件后运行ansible-playbook