ansible.utils.fact_diff filter – Find the difference between currently set facts
Note
This filter plugin is part of the ansible.utils collection (version 5.1.2).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install ansible.utils
.
To use it in a playbook, specify: ansible.utils.fact_diff
.
New in ansible.utils 2.12.0
Synopsis
Compare two facts or variables and get a diff.
Keyword parameters
This describes keyword parameters of the filter. These are the values key1=value1
, key2=value2
and so on in the following
example: input | ansible.utils.fact_diff(key1=value1, key2=value2, ...)
Parameter |
Comments |
---|---|
The second fact to be used in the comparison. |
|
The first fact to be used in the comparison. |
|
Show all common lines. Choices:
|
|
Configure and specify the diff plugin to use Default: |
|
The diff plugin to use, in fully qualified collection name format. Default: |
|
Parameters passed to the diff plugin. Default: |
|
Skip lines matching these regular expressions. Matches will be removed prior to the diff. If the provided before and after are a string, they will be split. Each entry in each list will be cast to a string for the comparison |
Examples
- name: Set fact
ansible.builtin.set_fact:
before:
a:
b:
c:
d:
- 0
- 1
after:
a:
b:
c:
d:
- 2
- 3
- name: Show the difference in json format
ansible.builtin.set_fact:
result: "{{before | ansible.utils.fact_diff(after)}}"
# TASK [Show the difference in json format] **********************************************************************************************
# ok: [localhost] => {
# "ansible_facts": {
# "result": [
# "--- before",
# "+++ after",
# "@@ -3,8 +3,8 @@",
# " "b": {",
# " "c": {",
# " "d": [",
# "- 0,",
# "- 1",
# "+ 2,",
# "+ 3",
# " ]",
# " }",
# " }",
# ""
# ]
# },
# "changed": false
# }
- name: Set fact
ansible.builtin.set_fact:
before: "{{ before|ansible.utils.to_paths }}"
after: "{{ after|ansible.utils.to_paths }}"
- name: Show the difference in path format
ansible.builtin.set_fact:
result: "{{before | ansible.utils.fact_diff(after)}}"
# TASK [Show the difference in path format] **********************************************************************************************
# ok: [localhost] => {
# "ansible_facts": {
# "result": [
# "--- before",
# "+++ after",
# "@@ -1,4 +1,4 @@",
# " {",
# "- "a.b.c.d[0]": 0,",
# "- "a.b.c.d[1]": 1",
# "+ "a.b.c.d[0]": 2,",
# "+ "a.b.c.d[1]": 3",
# " }",
# ""
# ]
# },
# "changed": false
# }
- name: Set fact
ansible.builtin.set_fact:
before: "{{ before|to_nice_yaml }}"
after: "{{ after|to_nice_yaml }}"
- name: Show the difference in yaml format
ansible.builtin.set_fact:
result: "{{before | ansible.utils.fact_diff(after)}}"
# TASK [Show the difference in yaml format] **********************************************************************************************
# ok: [localhost] => {
# "ansible_facts": {
# "result": [
# "--- before",
# "+++ after",
# "@@ -1,2 +1,2 @@",
# "-a.b.c.d[0]: 0",
# "-a.b.c.d[1]: 1",
# "+a.b.c.d[0]: 2",
# "+a.b.c.d[1]: 3",
# ""
# ]
# },
# "changed": false
# }
Return Value
Key |
Description |
---|---|
Returns diff between before and after facts. Returned: success |