Ansible Collection - c2platform.core

GitLab: c2platform/ansible-collection-core 

Pipeline Status Latest Release

C2 Platform generic roles that are used by all or some other roles. These roles typically don’t create services / processes on target node but are depedencies e.g. packages required by those roles. Or these roles help with Ansible provisioning for example offers generic Ansible modules, filters etc.

Roles

Plugins

Module plugins:

Filter plugins:

Filters

groups2items

The groups2items filter in c2platform.core is a powerful tool to transform dictionaries of “grouped” lists or dictionaries into flat lists. This filter is especially valuable in the context of C2 Ansible collections, where the use of “grouped” data structures is a common practice.

The groups2items filter shares similarities with the built-in `dict2items`  filter. Its primary purpose is to simplify complex data structures for easier processing, particularly when utilizing Ansible’s loop or with_items.

Consider the following scenario:

my_grouped_lists:
 group1:
   - name: file1
   - name: file2
 group2:
   - name: file3
   - name: file4
my_list: "{{ my_grouped_lists | c2platform.core.groups2items }}"

In this case, my_grouped_lists contains groups with associated lists. Applying the groups2items filter to it results in a flattened list:

my_list:
-   group: group1
    name: file1
-   group: group1
    name: file2
-   group: group2
    name: file3
-   group: group2
    name: file4

The groups2items filter is versatile and can handle nested structures as well. For instance:

my_grouped_lists:
 group1:
   name: file1
   url: url1
 group2:
   name: file3
   url: url3
my_list: "{{ my_grouped_lists | c2platform.core.groups2items }}"

In this example, my_grouped_lists contains nested dictionaries within groups. The groups2items filter transforms them into a flat list:

my_list:
-   group: group1
    name: file1
    url: url1
-   group: group2
    name: file3
    url: url3

The groups2items filter simplifies complex data structures, making it easier to work with structured data in your Ansible playbooks and roles.

product

The product filter is a custom Jinja filter designed to facilitate the merging of attributes from two lists of dictionaries at the same level. It is particularly useful when you need to generate combinations of attributes from these two lists. While it shares a similar purpose with `ansible.builtin.product`  filter, the c2platform.core.product filter is specifically tailored to work seamlessly with lists of dictionaries that can be merged.

For example, consider the following lists:

list1:
  - { key1: value1, key2: value2 }
  - { key1: value3, key2: value4 }

list2:
  - { key3: value5, key4: value6 }
  - { key3: value7, key4: value8 }

You can merge these lists using the c2platform.core.product filter:

merged_lists: "{{ list1 | product(list2) }}"

This results in the following merged list, which includes all conceivable combinations of attributes from list1 and list2:

merged_lists:
  - { key1: value1, key2: value2, key3: value5, key4: value6 }
  - { key1: value1, key2: value2, key3: value7, key4: value8 }
  - { key1: value3, key2: value4, key3: value5, key4: value6 }
  - { key1: value3, key2: value4, key3: value7, key4: value8 }

For a practical demonstration of how to use this filter, please consult the group_vars/windows/proxy.yml file within the RWS Inventory Project .

This file provides a real-world example showcasing the filter’s application in a specific context.

resources

Filter to prepare items for processing of a “resources” variable like for example win_resources variable in the c2platform.wincore.win Ansible role.



Last modified February 20, 2024: project update RWS-628 (531032b)