【问题标题】:Ansible inventory plugin for azure throws encoding error用于 azure 的 Ansible 库存插件引发编码错误
【发布时间】:2019-06-26 00:00:52
【问题描述】:

据我了解,使用 ansible-inventory-plugins 而不是 dynamic-inventory-provisioners 是处理动态主机的新方法,如云提供商等。

所以,起初我已经在我的环境中设置了 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

帮忙,有人吗?

【问题讨论】:

  • 检查 /path/to/foo.azure_rm.yaml 文件的编码。确保它是“正常”的东西,比如 UTF-8
  • @M_dk,是的……

标签: azure ansible yaml ansible-inventory


【解决方案1】:

我也遇到了同样的问题,用python3解决了

您可以使用以下命令检查您的 ansible 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

如果需要为身份验证导出环境变量

  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

【讨论】:

    【解决方案2】:

    我已经在 github fork 中修复了它,并使用 pipenv 将此版本包含在我的环境中。实际上它应该是来自devel的备份端口,问题已经解决了。也许我会在接下来的几天里解决这个问题,并在 ansible 上做一个 PR 以将其包含到 stable-2.7 中,但也许更好的选择是在 5 月等待 2.8。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 实际上,我已经在我的 pipenv 中使用 python 2.7 倒退了,它使动态库存插件现在可以工作。缺点是,该分子现在不能正常工作。进退两难。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2022-08-21
      相关资源
      最近更新 更多