Comparing 2 nested dictionaries and print the difference

HI
I have 2 nested dictionaries to compare and print their result. How can this be done ? Any thoughts ? I want to check if there are any databases and tables are missing or additonal tables are present by comparing the following 2 nested dictionaries.

expected_dbs:
‘database1’:
db_tables:

  • “non-existant-table”
  • “table1_db1”
  • “table2_db2”
  • “table3_db3”

‘database2’:
db_tables:

  • “non-existant-table”
  • “table1_db2”
  • “table2_db2”
  • “table3_db2”
    ‘database3’:
    db_tables:
  • “non-existant-table”
  • “table1_db3”
  • “table2_db3”
  • “table3_db3”

actual_dbs:
‘database1’:
db_tables:

  • “table1_db1”
  • “table2_db2”
  • “table3_db3”

‘database2’:
db_tables:

  • “table1_db2”
  • “table2_db2”
  • “table3_db2”
  • “additional_table”

‘database3’:
db_tables:

  • “table1_db3”
  • “table2_db3”

I could iterate over one database at a time and then compare, but was thinking if there is any better way to do it ?

This is a simple difference of (expected - actual) with common set of
the keys

    - set_fact:
        diff_dbs: "{{ diff_dbs|default({})|
                      combine({item: diff}) }}"
      loop: "{{ expected_dbs|intersect(actual_dbs) }}"
      vars:
        diff: "{{ expected_dbs[item]['db_tables']|
                  difference(actual_dbs[item]['db_tables']) }}"

There are other options, e.g. see "symmetric_difference"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-from-sets-or-lists-set-theory

Thank you so much.