Skip to main content

Travis (.com) branch Codecov Code Climate maintainability PyPI PyPI - Downloads

Ansible Filter

Contains Ansible related filter set for collection/object operations. Aims to extend the official Ansible Filters.

Available filters are listed below.

Install

PyPI

pip install -U ansible-filter

Source

git clone git@github.com:nl2go/ansible-filter.git
cd ansible-filter/
pip install .

Filters

Change Set

Computes the change set between a list of objects using a key attribute non-recursively.

The filter arguments local and origin are non-associative. Objects present at the origin but missing within local are considered non managed.

The result is a change set with a map of lists to:

  • create - objects to create at origin
  • update - objects to update at origin
  • delete - objects to delete at origin
  • noop - objects with no operation required

Useful to interact with any kind of a stateful API.

from ansible_filter import change_set

local = [{ 'id': 1, 'foo': 'bar' }, { 'id': 2, 'foo': 'foo' }, { 'id': 3, 'foz':'baz' }, { 'id': 4, 'state': 'absent' }]
origin = [{ 'id': 2, 'foo': 'bar' }, { 'id': 3, 'foz':'baz' }, { 'id': 4, 'x': 'y' }, { 'id': 5, 'foo': 'bar' }]

result = change_set.change_set(local, origin, 'id')
print(result)  

[
    'create': [{ 'id': 1, 'foo': 'bar' }],
    'update': [{ 'id': 2, 'foo': 'foo' }],
    'delete': [{ 'id': 4, 'x': 'y' }],
    'noop': [{ 'id': 3, 'foz': 'baz' }],
]

Form URL Encode

Encodes arbitrary objects to form URL format.

from ansible_filter import form_urlencode

obj = { 'foo': 'bar', 'foz': ['baz'] }

result = form_urlencode.form_urlencode(obj)
print(result)

foo=bar&foz[0]=baz&

Pick

Filters a list of objects retaining attributes only matching the names passed as argument, non-recursively.

from ansible_filter import pick

elements = [{ 'foo': 'bar', 'foz': 'baz' }]

result = pick.pick(elements, ['foo'])
print(result)

[{ 'foo': 'bar' }]

Omit

Filters a list objects omitting attributes matching the names passed as argument, non-recursively.

from ansible_filter import omit

elements = [{ 'foo': 'bar', 'foz': 'baz' }]

result = omit.omit(elements, ['foo'])
print(result)

[{ 'foz': 'baz' }]

Group By

Groups elements by key attribute.

from ansible_filter import group_by

left = [{ 'id': '1', 'foo': 'a' }, { 'id': '2', 'foz': 'x' }]
right = [{ 'id': '1', 'foo': 'b' }, { 'id': '2', 'foz': 'y' }]

result = group_by.group_by(left, right, 'id')
print(result)

[
    { 'id': '1', 'group': [{ 'id': '1', 'foo': 'a' }, { 'id': '1', 'foo': 'b' }] }, 
    { 'id': '2', 'group': [{ 'id': '2', 'foz': 'x' }, { 'id': '2', 'foz': 'y' }] }
]

List 2 Dict

Converts a list to dict by key attribute.

from ansible_filter import list_to_dict

elements = [{ 'id': '1', 'foo': 'bar' }, { 'id': '2', 'foz': 'baz' }]

result = list_to_dict.list_to_dict(elements, 'id')
print(result)

{'1': {'foo': 'bar', 'id': '1'}, '2': {'id': '2', 'foz': 'baz'}}

Point to Point Connections

Resolves point to point connections between the local and remote hosts.

from ansible_filter import network

remote_hostnames = ['two', 'three']
hostname = 'one'
hostvars = {
    'one': {
        'ansible_default_ipv4': {
            'interface': 'eth0'
        },
        'ansible_eth0': {
            'ipv4': {
                'address': '127.0.0.1',
            }
        }
    },
    'two': {
        'ansible_default_ipv4': {
            'interface': 'eth0'
        },
        'ansible_eth0': {
            'ipv4': {
                'address': '127.0.0.2',
            }
        }
    },
    'three': {
        'ansible_default_ipv4': {
            'interface': 'eth0'
        },
        'ansible_eth0': {
            'ipv4': {
                'address': '127.0.0.3',
            }
        }        
    }
}

result = network.get_point_to_point_connections(remote_hostnames, hostname, hostvars)

[
  {
    'remote': {
      'interface': 'eth0',
      'hostname': 'two',
      'address': '127.0.0.2'
    },
    'local': {
      'interface': 'eth0',
      'hostname': 'one',
      'address': '127.0.0.1'
    }
  },
  {
    'remote': {
      'interface': 'eth0',
      'hostname': 'three',
      'address': '127.0.0.3'
    },
    'local': {
      'interface': 'eth0',
      'hostname': 'one',
      'address': '127.0.0.1'
    }
  }
]

Links

Metadata

Release files for ansible-filter 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ansible-filter 1.1.1
File Size Uploaded Provenance
ansible-filter-1.1.1.tar.gz 5.8 kB Without provenance Details

Release files / ansible-filter-1.1.1.tar.gz

Download URL ansible-filter-1.1.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0d44bca291f239a4f546b21dc509cbe49a8bad936b1103ba1f2ca69707425556
BLAKE2b-256 checksum
How to use checksums
795ca34b1b0586a3b1d3cbd485941f458103296a0e14ee56b3e438a06db9080d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/2.7.15

Security for ansible-filter 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page