ansible.utils.fact_diff (4.0.0) — module

Find the difference between currently set facts

| "added in version" 1.0.0 of ansible.utils"

Authors: Bradley Thornton (@cidrblock)

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install ansible.utils:==4.0.0


Add to requirements.yml

  collections:
    - name: ansible.utils
      version: 4.0.0

Description

Compare two facts or variables and get a diff.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.set_fact:
    before:
      a:
        b:
          c:
            d:
              - 0
              - 1
    after:
      a:
        b:
          c:
            d:
              - 2
              - 3
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Show the difference in json format
  ansible.utils.fact_diff:
    before: "{{ before }}"
    after: "{{ after }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [ansible.utils.fact_diff] **************************************
# --- before
# +++ after
# @@ -3,8 +3,8 @@
#          "b": {
#              "c": {
#                  "d": [
# -                    0,
# -                    1
# +                    2,
# +                    3
#                  ]
#              }
#          }
#
# changed: [localhost]

- name: Show the difference in path format
  ansible.utils.fact_diff:
    before: "{{ before | ansible.utils.to_paths }}"
    after: "{{ after | ansible.utils.to_paths }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [ansible.utils.fact_diff] **************************************
# --- 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: [localhost]

- name: Show the difference in yaml format
  ansible.utils.fact_diff:
    before: "{{ before | to_nice_yaml }}"
    after: "{{ after | to_nice_yaml }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [ansible.utils.fact_diff] **************************************
# --- before
# +++ after
# @@ -2,5 +2,5 @@
#      b:
#          c:
#              d:
# -            - 0
# -            - 1
# +            - 2
# +            - 3

# changed: [localhost]


#### Show the difference between complex object using restconf
#  ansible_connection: ansible.netcommon.httpapi
#  ansible_httpapi_use_ssl: True
#  ansible_httpapi_validate_certs: False
#  ansible_network_os: ansible.netcommon.restconf

- name: Get the current interface config prior to changes
  ansible.netcommon.restconf_get:
    content: config
    path: /data/Cisco-NX-OS-device:System/intf-items/phys-items
  register: pre
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Update the description of eth1/100
  ansible.utils.update_fact:
    updates:
      - path: "pre['response']['phys-items']['PhysIf-list'][{{ index }}]['descr']"
        value: "Configured by ansible {{ 100 | random }}"
  vars:
    index: "{{ pre['response']['phys-items']['PhysIf-list'] | ansible.utils.index_of('eq', 'eth1/100', 'id') }}"
  register: updated
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Apply the configuration
  ansible.netcommon.restconf_config:
    path: 'data/Cisco-NX-OS-device:System/intf-items/'
    content: "{{ updated.pre.response}}"
    method: patch
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Get the current interface config after changes
  ansible.netcommon.restconf_get:
    content: config
    path: /data/Cisco-NX-OS-device:System/intf-items/phys-items
  register: post
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Show the difference
  ansible.utils.fact_diff:
    before: "{{ pre.response | ansible.utils.to_paths }}"
    after: "{{ post.response | ansible.utils.to_paths }}"

Inputs

    
after:
    description:
    - The second fact to be used in the comparison.
    required: true
    type: raw

before:
    description:
    - The first fact to be used in the comparison.
    required: true
    type: raw

plugin:
    default: {}
    description:
    - Configure and specify the diff plugin to use
    suboptions:
      name:
        default: ansible.utils.native
        description:
        - The diff plugin to use, in fully qualified collection name format.
        type: str
      vars:
        default: {}
        description:
        - Parameters passed to the diff plugin.
        suboptions:
          skip_lines:
            description:
            - Skip lines matching these regular expressions.
            - Matches will be removed prior to the diff.
            - If the provided I(before) and I(after) are a string, they will be split.
            - Each entry in each list will be cast to a string for the comparison
            elements: str
            type: list
        type: dict
    type: dict

Outputs

diff_lines:
  description: The I(diff_text) split into lines.
  elements: str
  returned: always
  type: list
diff_text:
  description: The diff in text format.
  returned: always
  type: str