Jinja2 Filter - 仅在文件更改时过滤和输出结果

Jinja2 Filter - filter and output result only when file changed

我正在使用 Ansible Core,我只想在命令行中更改任务时应用过滤器

这是我的 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-csv-file
    read_csv:
      path: file_reader/Networks.csv
      key: Name
    register: user
    
  - set_fact:
      hosts_in_group: "{{ user.dict | dict2items | map(attribute='key') | list }}"

  - name: add-host-object
    check_point.mgmt.cp_mgmt_host:                    
      name: "{{ item.value.Name | quote }}"          
      ip_address: "{{ item.value.IP | quote }}"      
      comments: "{{ item.value.Comments }}"           
      state: present
    loop: "{{ user.dict | dict2items }}"               
    ignore_errors: yes
    delegate_to: Global
    register: servers_changed_now



  - set_fact:
      new_list: "{{ servers_changed_now.results }}"
    register: new_result
  - debug:
      msg: "{{ new_result }}"

这是我要过滤的输出


-   add-host:
        color: black
        comments: FWP - Ansible
        domain:
            domain-type: global domain
            name: Global
            uid: 1e294ce0-367a-11e3-aa6e-0800200c9a
        groups: []
        icon: Objects/host
        interfaces: []
        ipv4-address: 10.1.2.5
        meta-info:
            creation-time:
                iso-8601: 2021-04-16T15:34-04
                posix: 161860168011
            creator: SVCFWPWorker
            last-modifier: SVCFWPWorker
            last-modify-time:
                iso-8601: 2021-04-16T15:34-0400
                posix: 161860168011
            lock: unlocked
            validation-state: ok
        name: gTest104
        nat-settings:
            auto-rule: false
        read-only: true
        tags: []
        type: host
        uid: 7b73455a-50ad-4af1-9968-a58ae5232e
    ansible_loop_var: item
    changed: true
    checkpoint_session_uid: 323b6867-6bf6-4f1e-8be8-f0b5e6f885
    failed: false
    invocation:
        module_args:
            auto_publish_session: null
            color: null
            comments: FWP - Ansible
            details_level: null
            groups: null
            host_servers: null
            ignore_errors: null
            ignore_warnings: null
            interfaces: null
            ip_address: 10.1.2.5
            ipv4_address: null
            ipv6_address: null
            name: gTest104
            nat_settings: null
            state: present
            tags: null
            version: null
            wait_for_task: true
            wait_for_task_timeout: 30
    item:
        key: gTest104
        value:
            Comments: FWP - Ansible
            IP: 10.1.2.5
            Name: gTest104
-   ansible_loop_var: item
    changed: true
    checkpoint_session_uid: 323b6867-6bf6-4f1e-8be8-f0b5e6f885
    failed: false
    invocation:
        module_args:
            auto_publish_session: null
            color: null
            comments: FWP - Add Group
            details_level: null
            groups: null
            host_servers: null
            ignore_errors: null
            ignore_warnings: null
            interfaces: null
            ip_address: 10.1.2.3
            ipv4_address: null
            ipv6_address: null
            name: gTest101
            nat_settings: null
            state: present
            tags: null
            version: null
            wait_for_task: true
            wait_for_task_timeout: 30
    item:
        key: gTest101
        value:
            Comments: FWP - Add Group
            IP: 10.1.2.3
            Name: gTest101
    set-host:
        color: black
        comments: FWP - Add Group
        domain:
            domain-type: global domain
            name: Global
            uid: 1e294ce0-367a-11e3-aa6e-0800200c9a
        groups:
        -   domain:
                domain-type: global domain
                name: Global
                uid: 1e294ce0-367a-11e3-aa6e-0800200c9a
            name: gTest1A
            type: group
            uid: eeb297c4-43a8-45e9-b06f-50efd71a21
        icon: Objects/host
        interfaces: []
        ipv4-address: 10.1.2.3
        meta-info:
            creation-time:
                iso-8601: 2021-04-16T09:43-04
                posix: 16185806377
            creator: SVCFWPWorker
            last-modifier: SVCFWPWorker
            last-modify-time:
                iso-8601: 2021-04-16T15:34-04
                posix: 16186016815
            lock: unlocked
            validation-state: ok
        name: gTest101
        nat-settings:
            auto-rule: false
        read-only: true
        tags: []
        type: host
        uid: 6a65f195-9ece-4c45-9293-5d5e6b4ff2
-   ansible_loop_var: item
    changed: false
    failed: true
    invocation:
        module_args:
            auto_publish_session: null
            color: null
            comments: FWP - Applying this
            details_level: null
            groups: null
            host_servers: null
            ignore_errors: null
            ignore_warnings: null
            interfaces: null
            ip_address: 10.1.2.4
            ipv4_address: null
            ipv6_address: null
            name: gTest103
            nat_settings: null
            state: present
            tags: null
            version: null
            wait_for_task: true
            wait_for_task_timeout: 30
    item:
        key: gTest103
        value:
            Comments: FWP - Applying this
            IP: 10.1.2.4
            Name: gTest103
    msg: 'Checkpoint device returned error 400 with message {u''message'': u''Validation
        failed with 1 warning'', u''code'': u''err_validation_failed'', u''warnings'':
        [{u''message'': u''Multiple objects have the same IP address 10.1.2.4''}]}
        Unpublished changes were discarded'

Network.csv

Name,IP,Comments
gTest101,10.1.2.3,FWP - Add Group
gTest103,10.1.2.4,FWP - Applying this
gTest104,10.1.2.5,FWP - Ansible


当我运行代码时,gTest104和gTest101变了,gTest103失败了 我只想获得 gTest104gTest101 的结果,因为这是 更改的唯一任务
逻辑 是当 changed: truefailed: false - 它改变了一个任务,但是当 changed: false and failed: true - 任务失败

问题:如何在 Ansible 中使用 Jinja2 Filter 来仅获取已更改的结果并将其仅显示到 ansible 中的控制台?

如果您尝试过滤并显示已更改但未失败的任务,这应该可行。

    - name: filter results
      set_fact:
        filtered_results: "{{ filtered_results|default([]) + [item] }}"
      with_items:
        - "{{ servers_changed_now.results }}"
      when:
        - item.changed == true
        - item.failed == false

    - name: display filtered result
      debug:
        msg: "{{ filtered_results }}"

when 可以更改条件以提取不同的结果。