I am working with Ansible Core , I want to apply filter to only when a task is changed in the command line
Here is my 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 }}"
Here is the output of what I want to filter
- 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
When I run the code, gTest104 and gTest101 changed, and gTest103 failed
I only want to get the results of gTest104 and gTest101 since that's the only task that changed.
The Logic is when changed: true and failed: false - it changes a task, but when changed: false and failed: true - it fails a task
Question: How can I use Jinja2 Filter in Ansible to get only result that changed and display only it to the console in ansible?
if you are trying to filter and display changed but not failed tasks, this should work.
- 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
condition can be altered to extract different results.