据我所知,使用ansible-inventory-plugins而不是动态库存供应商是处理动态主机的新方法,如云提供商等。
所以,首先我在我的环境中设置了azure凭据:
± env | grep AZ
AZURE_SECRET=asdf
AZURE_TENANT=asdf
AZURE_SUBSCRIPTION_ID=asdf
AZURE_CLIENT_ID=asdf
接下来,我写了一个带有以下内容的ansible.cfg:
± cat ansible.cfg
[inventory]
enable_plugins = azure_rm
最后,我用ansible inventory plugin页面显示的最小设置编写了yaml文件:
± cat foo.azure_rm.yaml
---
plugin: azure_rm
当我在该文件上运行ansible-inventory二进制文件时,我得到:
± ansible-inventory -i foo.azure_rm.yaml --list
[WARNING]: * Failed to parse /path/to/foo.azure_rm.yaml with azure_rm plugin: Unicode-objects must be encoded before hashing
[WARNING]: Unable to parse /path/to/foo.azure_rm.yaml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {}
}
总结:主要问题似乎是:
[WARNING]: * Failed to parse /path/to/foo.azure_rm.yaml with azure_rm plugin: Unicode-objects must be encoded before hashing
帮忙,有人吗?
我认为这是脚本中的错误。将调试标志添加到Ansible会给我以下堆栈跟踪:
File "/usr/local/lib/python3.6/site-packages/ansible/inventory/manager.py", line 273, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/azure_rm.py", line 235, in parse
self._get_hosts()
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/azure_rm.py", line 292, in _get_hosts
self._process_queue_batch()
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/azure_rm.py", line 412, in _process_queue_batch
result.handler(r['content'], **result.handler_args)
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/azure_rm.py", line 357, in _on_vm_page_response
self._hosts.append(AzureHost(h, self, vmss=vmss))
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/azure_rm.py", line 466, in __init__
self.default_inventory_hostname = '{0}_{1}'.format(vm_model['name'], hashlib.sha1(vm_model['id']).hexdigest()[0:4])
这似乎只是最近修复:https://github.com/ansible/ansible/pull/46608。所以要么你必须等待2.8或使用开发版本。
我已经在github fork中修复它并使用pipenv在我的环境中包含这个版本。实际上它应该是来自devel的备份端口,其中问题已经解决。也许我会在接下来的几天内解决这个问题并在ansible做一个PR来将它包含在稳定的2.7中,但也许更好的选择是在5月等待2.8。