[Python] FIND duplicates in Ansible Dictionary

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I need to check that the same port isn't used in two sub-dictionaries.

    my_dict:
    first:
    redis:
    port: 1234
    second:
    redis:
    port: 1235
    third:
    redis:
    port: 1234


    In a task I can use the following:

    - debug:
    msg: '{{ my_dict | dict2items | map(attribute="value.redis.port") | list }}'


    This returns:

    ['1234', '1235', '1234']


    What I need, is some way to filter only the duplicates. So I get:

    ['1234', '1234']


    Returned.

    Basically, I need to check that the ports aren't duplicated, and if they are apply a fail task that checks, and shows which items are causing the conflict.

    Any ideas?

    Continue reading...

Compartilhe esta Página