簡體   English   中英

Ansible Jinja2 過濾器 - 過濾數據並從數據創建列表

[英]Ansible Jinja2 Filter - Filter data and create a list from data

我有一本劇本,我想從數據中創建一個列表並傳遞新創建的列表

這是我的劇本。

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

這個想法是從我的task.yml創建一個名為new_list_created的列表,並將該列表解析為成員

members: 
 - "{ new_list_created }"

下面是我想應用 Jinja2 過濾器的列表。 它包含我想要過濾的很多東西的列表。

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

我希望創建的新列表new_list_created.yml看起來像這樣

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

這種操作最簡單的方法是使用json_query

這將需要 controller(有關更多詳細信息,請參見上面的鏈接):

  • pip install jmespath
  • 如果運行 ansible >= 2.10: ansible-galaxy collection install community.general

請注意,由於ipv4-address包含一個特殊字符(破折號),因此它不是 ansible 中的有效標識符,需要在下面的查詢中用雙引號引起來(請參閱jmespath規范)。

這是一個演示劇本:

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

這使:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM