Openstack的Ansible动态库存

时间:2017-11-23 04:30:01

标签: ansible openstack ansible-inventory

我正在将几个Linux主机部署到openstack环境并尝试使用ansible进行配置。我对https://github.com/ansible/ansible/blob/devel/contrib/inventory/openstack.py

的库存动态库存脚本有些困难

如果我使用静态主机文件运行ansible,一切正常

# inventory/static-hosts
localhost   ansible_connection=local
linweb01    ansible_host=10.1.1.101
% ansible linweb01 -m ping -i ./inventory/static-hosts \
   --extra-vars="ansible_user=setup ansible_ssh_private_key_file=/home/ian/keys/setup.key"
linweb01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

但如果我使用动态广告资源,则找不到主机

% ansible linweb01 -m ping -i ./inventory/openstack.py \
   --extra-vars="ansible_user=setup ansible_ssh_private_key_file=/home/ian/keys/setup.key"
linweb01 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname linweb01: Name or service not known\r\n",
    "unreachable": true
}

当我手动运行库存脚本时,找到主机并返回正确的地址

% ./inventory/openstack.py --host linweb01
[...]
"name": "linweb01",
"networks": {},
"os-extended-volumes:volumes_attached": [],
"power_state": 1,
"private_v4": "10.1.1.101",
[...]

我的猜测是库存脚本不知道使用" private_v4" IP地址的价值,虽然我似乎无法为此找到参考。

如何使用" private_v4"清单脚本返回的值为" ansible_host"主人的价值?

1 个答案:

答案 0 :(得分:1)

快速查看代码表明,ip地址应该在interface_ip密钥中:

hostvars[key] = dict(
    ansible_ssh_host=server['interface_ip'],
    ansible_host=server['interface_ip'],
    openstack=server)

如果您需要解决方法,可以尝试将此添加到group_vars/all.yml

ansible_host: "{{ private_v4 }}"
相关问题