首页 文章

Openstack的Ansible动态库存

提问于
浏览
0

我正在将几个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",
[...]

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

如何使用inventory脚本返回的“private_v4”值作为主机的“ansible_host”值?

1 回答

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

相关问题