ansible:ansible.utils.ipaddr('private')失败并出现AttributeError:'IPNetwork'对象没有属性'is_private'

问题描述 投票:0回答:1

这是一个有答案的问题,以便通过解决方案搜索此错误消息。

以下任务:

- name: Filter out private IPs from addresses
  set_fact:
    private_ips: "{{ addresses | ansible.utils.ipaddr('private') | list }}"

此输出失败,但之前曾工作过!

TASK [Filter out private IPs from addresses] **********************************************************************
The full traceback is:
Traceback (most recent call last):
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/executor/task_executor.py", line 526, in _execute
    self._task.post_validate(templar=templar)
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/playbook/task.py", line 290, in post_validate
    super(Task, self).post_validate(templar)
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/playbook/base.py", line 543, in post_validate
    value = method(attribute, getattr(self, name), templar)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/playbook/task.py", line 298, in _post_validate_args
    args = templar.template(value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/template/__init__.py", line 791, in template
    d[k] = self.template(
           ^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/template/__init__.py", line 764, in template
    result = self.do_template(
             ^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/template/__init__.py", line 1010, in do_template
    res = myenv.concat(rf)
          ^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/template/native_helpers.py", line 43, in ansible_eval_concat
    head = list(islice(nodes, 2))
           ^^^^^^^^^^^^^^^^^^^^^^
  File "<template>", line 24, in root
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible/template/__init__.py", line 295, in wrapper
    ret = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible_collections/ansible/utils/plugins/filter/ipaddr.py", line 283, in _ipaddr
    return ipaddr(**updated_data)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible_collections/ansible/utils/plugins/plugin_utils/base/ipaddr_utils.py", line 458, in ipaddr
    _ret = [ipaddr(element, str(query), version) for element in value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible_collections/ansible/utils/plugins/plugin_utils/base/ipaddr_utils.py", line 458, in <listcomp>
    _ret = [ipaddr(element, str(query), version) for element in value]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible_collections/ansible/utils/plugins/plugin_utils/base/ipaddr_utils.py", line 573, in ipaddr
    return query_func_map[query](v, *extras)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timh/.pyenv/versions/3.11.8/lib/python3.11/site-packages/ansible_collections/ansible/utils/plugins/plugin_utils/base/ipaddr_utils.py", line 292, in _private_query
    if v.is_private():
       ^^^^^^^^^^^^
AttributeError: 'IPNetwork' object has no attribute 'is_private'
fatal: [host-2]: FAILED! => {
    "changed": false
}

另一个关键信息是我安装了最新版本的

netaddr

% pip list | grep netaddr
netaddr                   1.2.1
ansible
1个回答
0
投票

netaddr 1.0.0 版本弃用了该函数

IPAddress.is_private
-- https://netaddr.readthedocs.io/en/latest/changes.html#release-1-0-0

基础ansible_collections的3.1.0版本仍然使用此功能:https://github.com/ansible-collections/ansible.utils/blob/2933ef2e37d3376b6a02baf39e7fa78ce48cf7cf/plugins/plugin_utils/base/ipaddr_utils.py#L292

此问题已在代码中修复,但截至 2024 年 3 月 19 日尚未发布: https://github.com/ansible-collections/ansible.utils/commit/c176c23392d0219cc3b0baefe029ef2b8641bc94

因此,请确保您使用的是 0.10.0 版本的 netaddr

pip install netaddr==0.10.0
© www.soinside.com 2019 - 2024. All rights reserved.