2

I have a simple problem, i have two dictionaries, one being static with some data, and an other being the result of the stat command stored in a variable (with register). The goal is simply to compare the checksum of some files on a host, with the checksum stored in the static dictionary. What would be the easiest way to do it? I want to ensure that the values correspond to the correct id, so not just loop by list but use id as an index (because the id numbers are not following each others, we have random id values).

static_dir: 
  id1: <checksum>
  id2: <checksum>
  id5: <checksum>

result_dir: results: item: id1 stat: checksum: <checksum> item: id5 stat: checksum: <checksum> .....

Thank you in advance for your answers and explanations.

Regards,

Mathew

math
  • 121

1 Answers1

3

For example, given the static_dir for testing

    static_dir: 
      id1: 123
      id2: 456
      id5: 789

I assume the results is a list. For example,

    result_dir:
      results:
        - item: id1
          stat:
            checksum: 123
        - item: id5
          stat:
            checksum: 789

Create a dictionary

    dynamic_dir: "{{ dict(result_dir.results |
                          json_query('[].[item, stat.checksum]')) }}"

gives

  dynamic_dir:
    id1: 123
    id5: 789

Then, the comparison is trivial. For example,

    - debug:
        msg: |
          static: {{ static }}
          dynamic: {{ dynamic }}
      vars:
        static: "{{ dynamic_dir.keys() | map('extract', static_dir) }}"
        dynamic: "{{ dynamic_dir.values() }}"

gives

  msg: |-
    static: [123, 789]
    dynamic: [123, 789]

Example of a complete playbook for testing

- hosts: all

vars:

static_dir: 
  id1: 123
  id2: 456
  id5: 789

result_dir:
  results:
    - item: id1
      stat:
        checksum: 123
    - item: id5
      stat:
        checksum: 789

dynamic_dir: &quot;{{ dict(result_dir.results |
                      json_query('[].[item, stat.checksum]')) }}&quot;

tasks:

- debug:
    var: dynamic_dir

- debug:
    msg: |
      static: {{ static }}
      dynamic: {{ dynamic }}
  vars:
    static: &quot;{{ dynamic_dir.keys() | map('extract', static_dir) }}&quot;
    dynamic: &quot;{{ dynamic_dir.values() }}&quot;

- assert:
    that: dynamic | difference(static) | length == 0
    success_msg: '[OK]  Checksums match.'
    fail_msg: '[ERR] Checksums do not match.'
  vars:
    static: &quot;{{ dynamic_dir.keys() | map('extract', static_dir) }}&quot;
    dynamic: &quot;{{ dynamic_dir.values() }}&quot;

Optionally, list failed ID(s)

    - assert:
        that: dynamic | difference(static) | length == 0
        success_msg: '[OK]  Checksums match.'
        fail_msg: |
          [ERR] Failed checksums: {% for id,checksum in dynamic_dir.items() %}
          {% if static_dir[id] != checksum %}
          {{ id }} {% endif %}
          {% endfor %}
      vars:
        static: "{{ dynamic_dir.keys() | map('extract', static_dir) }}"
        dynamic: "{{ dynamic_dir.values() }}"