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.
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