Ansible Jinja2 Filter - Filter data and create a list from data

doctore

I have a playbook where I want to create a list from data and pass the newly created list

This is my playbook.

playbook.yml

---
- name: Global Objects
  hosts: check_point
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'credentials/my_var.yml'
    - 'credentials/login.yml'

  tasks:
  - name: read-file
    include_vars:
      file: tmp/task.yml

  - name: make-existing-host-a member
    check_point.mgmt.cp_mgmt_group:
      name: gTest1A
      members:
        - "{{ new_list_created }}"
        - "{{ unique_host }}"
      auto_publish_session: true
    loop: "{{ members }}"
    delegate_to: Global
    ignore_errors: yes

The idea is to create a list from my task.yml called new_list_created and parse that list as a member to

members: 
 - "{ new_list_created }"

Below is the list I wanted to apply Jinja2 filter. It contains a list of so many things I want to filter.

task.yml

color: black
comments: Comments
domain:
    domain-type: mds
    name: System Data
    uid: a0eebc99-afed-4ef8-bb6d-fedfedfedfed
groups: []
icon: General/group
members:
-   color: black
    comments: FWP - Testing this
    domain:
        domain-type: mds
        name: System Data
        uid: a0eebc99-afed-4ef8-bb6d-fedfedfedfed
    groups:
    - 603dab88-43a2-437a-b59f-0bee45d769c1
    icon: Objects/host
    interfaces: []
    ipv4-address: 10.1.2.3
    meta-info:
        creation-time:
            iso-8601: 2021-04-09T11:31-0400
            posix: 1617982299819
        creator: SVCFWPWorker
        last-modifier: SVCFWPWorker
        last-modify-time:
            iso-8601: 2021-04-23T13:05-0400
            posix: 1619197548306
        lock: unlocked
        validation-state: ok
    name: gTest101
    nat-settings:
        auto-rule: false
    read-only: false
    tags: []
    type: host
    uid: e12b3f3c-8afd-441f-a5e0-75f399605836
-   color: black
    comments: FWP - Applying this
    domain:
        domain-type: mds
        name: System Data
        uid: a0eebc99-afed-4ef8-bb6d-fedfedfedfed
    groups:
    - 603dab88-43a2-437a-b59f-0bee45d769c1
    icon: Objects/host
    interfaces: []
    ipv4-address: 10.1.2.4
    meta-info:
        creation-time:
            iso-8601: 2021-04-09T11:31-0400
            posix: 1617982301463
        creator: SVCFWPWorker
        last-modifier: SVCFWPWorker
        last-modify-time:
            iso-8601: 2021-04-23T13:05-0400
            posix: 1619197554087
        lock: unlocked
        validation-state: ok
    name: gTest102
    nat-settings:
        auto-rule: false
    read-only: false
    tags: []
    type: host
    uid: 6bcfa0a5-da21-4a6e-8bde-3c5f486012e6
meta-info:
    creation-time:
        iso-8601: 2021-04-09T11:31-0400
        posix: 1617982294818
    creator: SVCFWPWorker
    last-modifier: SVCFWPWorker
    last-modify-time:
        iso-8601: 2021-04-09T11:31-0400
        posix: 1617982305162
    lock: unlocked
    validation-state: ok
name: gTest1A
read-only: false
tags: []
type: group
uid: 603dab88-43a2-437a-b59f-0bee45d769c1

I want the new list created, new_list_created.yml to look like this

list:
-    comments: FWP - Testing this
     ipv4-address: 10.1.2.3
     name: gTest101
-    comments: FWP - Applying this
     ipv4-address: 10.1.2.4
     name: gTest102
Zeitounator

The easiest way for such an operation is to use json_query

This will require on the controller (see above link for more details):

  • pip install jmespath
  • if running ansible >= 2.10: ansible-galaxy collection install community.general

Note that since ipv4-address contains a special character (dash) it is not a valid identifier in ansible as is and needs to be quoted in with double quotes (see jmespath specification) in the query below.

Here is a demo playbook:

---
- hosts: localhost
  gather_facts: false

  vars:
    # Your original data as a sinble minified json string for legibility
    members: [{"color":"black","comments":"FWP - Testing this","domain":{"domain-type":"mds","name":"System Data","uid":"a0eebc99-afed-4ef8-bb6d-fedfedfedfed"},"groups":["603dab88-43a2-437a-b59f-0bee45d769c1"],"icon":"Objects/host","interfaces":[],"ipv4-address":"10.1.2.3","meta-info":{"creation-time":{"iso-8601":"2021-04-09T11:31-0400","posix":1617982299819},"creator":"SVCFWPWorker","last-modifier":"SVCFWPWorker","last-modify-time":{"iso-8601":"2021-04-23T13:05-0400","posix":1619197548306},"lock":"unlocked","validation-state":"ok"},"name":"gTest101","nat-settings":{"auto-rule":false},"read-only":false,"tags":[],"type":"host","uid":"e12b3f3c-8afd-441f-a5e0-75f399605836"},{"color":"black","comments":"FWP - Applying this","domain":{"domain-type":"mds","name":"System Data","uid":"a0eebc99-afed-4ef8-bb6d-fedfedfedfed"},"groups":["603dab88-43a2-437a-b59f-0bee45d769c1"],"icon":"Objects/host","interfaces":[],"ipv4-address":"10.1.2.4","meta-info":{"creation-time":{"iso-8601":"2021-04-09T11:31-0400","posix":1617982301463},"creator":"SVCFWPWorker","last-modifier":"SVCFWPWorker","last-modify-time":{"iso-8601":"2021-04-23T13:05-0400","posix":1619197554087},"lock":"unlocked","validation-state":"ok"},"name":"gTest102","nat-settings":{"auto-rule":false},"read-only":false,"tags":[],"type":"host","uid":"6bcfa0a5-da21-4a6e-8bde-3c5f486012e6"}]

  tasks:
    - name: Extract only a few keys from list members
      vars:
        reduce_query: >-
          [].{
            comments: comments,
            "ipv4-address": "ipv4-address",
            name: name
          }
        new_list: "{{ members | json_query(reduce_query) }}"
      debug:
        var: new_list

Which gives:

PLAY [localhost] **************************************************************************************************************************************************************************************************************

TASK [Extract only a few keys from list members] *******************************************************************************************************************************************************************************
ok: [localhost] => {
    "new_list": [
        {
            "comments": "FWP - Testing this",
            "ipv4-address": "10.1.2.3",
            "name": "gTest101"
        },
        {
            "comments": "FWP - Applying this",
            "ipv4-address": "10.1.2.4",
            "name": "gTest102"
        }
    ]
}


PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related