从动作插件调用过滤器插件

时间:2016-05-19 12:04:22

标签: python ansible pickle ansible-2.x ansible-api

我正在开发一个自定义的Ansible动作插件。从那里我想从CIDR计算网关和子网掩码。 ipaddr filter已经可以实现这一点。有没有办法从动作插件中调用ipaddr过滤器?

我知道您可以通过动作插件调用模块,如下所示:

self._execute_module(...)

这样的过滤器可用吗?

我查看了Ansible的来源并找到了filter_loader类,但还没弄清楚如何使用它。

from ansible.errors import AnsibleError, AnsibleFilterError
from ansible.plugins import filter_loader
...
ipaddr = filter_loader.get('ipaddr')

这导致错误,我想我无法避免在ipaddr过滤器本身没有修改,这当然是没有选择。

Traceback (most recent call last):
  File "/usr/lib64/python2.7/multiprocessing/queues.py", line 266, in _feed
    send(obj)
PicklingError: Can't pickle <class '/usr/lib/python2.7/site-packages/ansible/plugins/filter/ipaddr.FilterModule'>: import of module /usr/lib/python2.7/site-packages/ansible/plugins/filter/ipaddr failed

1 个答案:

答案 0 :(得分:3)

如果您只需要使用ipaddr模块,为什么不直接导入它?

>>> from ansible.plugins.filter import ipaddr
>>> dir(ipaddr)
['FilterModule', '_6to4_query', '__builtins__', '__doc__', '__file__', '__metaclass__', '__name__', '__package__', '_bare_query', '_bool_hwaddr_query', '_bool_ipaddr_query', '_broadcast_query', '_cidr_lookup_query', '_cidr_query', '_cisco_query', '_empty_hwaddr_query', '_empty_ipaddr_query', '_gateway_query', '_host_query', '_hostmask_query', '_int_query', '_ip_query', '_ipv4_query', '_ipv6_query', '_link_local_query', '_linux_query', '_loopback_query', '_multicast_query', '_need_netaddr', '_net_query', '_netmask_query', '_network_query', '_postgresql_query', '_prefix_query', '_private_query', '_public_query', '_revdns_query', '_size_query', '_subnet_query', '_type_query', '_unicast_query', '_unix_query', '_version_query', '_win_query', '_wrap_query', 'absolute_import', 'division', 'errors', 'hwaddr', 'ipaddr', 'ipsubnet', 'ipv4', 'ipv6', 'ipwrap', 'macaddr', 'netaddr', 'nthhost', 'partial', 'print_function', 'slaac', 'types']