用于Azure的Ansible广告资源插件抛出编码错误

时间:2019-02-01 11:54:16

标签: azure ansible yaml ansible-inventory

据我了解,在动态库存供应商上使用ansible-inventory插件是处理动态主机(如云提供商等)的新方法。

因此,首先,我在环境中设置了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

有帮助吗?

3 个答案:

答案 0 :(得分:1)

我认为这是脚本中的错误。将调试标志添加到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或使用开发版本。

答案 1 :(得分:1)

我已将其修复在github分支中,并使用pipenv在我的环境中包括了此版本。实际上,它应该是devel的备用端口,该问题已得到解决。也许我会在未来几天内对此问题进行修复,并在ansible上进行PR,以将其纳入稳定版2.7,但更好的选择是在5月等待2.8。

答案 2 :(得分:0)

我遇到了相同的问题,并使用python3解决了该问题

您可以使用以下命令检查您的python版本

  ansible --version | grep "python version"
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]

安装所有python3软件包

  pip3 install ansible azure azure-cli

如果需要导出env变量进行身份验证

  export ANSIBLE_AZURE_AUTH_SOURCE=cli

然后使用

运行ansible库存
python3 $(which ansible-inventory) -i my.azure_rm.yaml --graph

my.azure_rm.yml文件如下所示:

plugin: azure_rm
include_vm_resource_groups:
  - my_resource_group_rg
auth_source: cli