ansible: ansible.utils.ipaddr('private') fails with AttributeError: 'IPNetwork' object has no attribute 'is_private'
Asked Answered
C

2

5

The following task:

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

Failed with this output, but had worked before!

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
}

The other key piece of information is that I had the latest version of netaddr installed:

% pip list | grep netaddr
netaddr                   1.2.1

How can I fix this code? It seems to comply with the documentation.

Commination answered 18/3, 2024 at 13:42 Comment(2)
What is "a question with an answer"? If you figured out an answer to your question, please post it as an answer to the original question text, not as an edit to it.Verst
@AlexanderPletnev Its something I worked hours to figure out so I would get on the internet so the next person could get the answer with a quick Google :-). I'll separate out the Q and ACommination
C
8

Version 1.0.0 of netaddr deprecates the function IPAddress.is_private -- https://netaddr.readthedocs.io/en/latest/changes.html#release-1-0-0

Version 3.1.0 of the base ansible_collections still uses this function: https://github.com/ansible-collections/ansible.utils/blob/2933ef2e37d3376b6a02baf39e7fa78ce48cf7cf/plugins/plugin_utils/base/ipaddr_utils.py#L292

This has been fixed in the code, but not yet released as of 19 March 2024: https://github.com/ansible-collections/ansible.utils/commit/c176c23392d0219cc3b0baefe029ef2b8641bc94

So in the meantime, be sure you're using version 0.10.0 of netaddr

pip install netaddr==0.10.0
Commination answered 19/3, 2024 at 15:15 Comment(0)
H
0

Another solution is to specify collection versions in requirements.txt:

collections:
  - name: ansible.posix
    version: 1.5.4
  - name: ansible.netcommon
    version: 7.0.0
  - name: community.zabbix
    version: 3.1.1

Then update:

ansible-galaxy install -r requirements.yml --force

Links to the latest versions:

Hepplewhite answered 29/8, 2024 at 8:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.