ansible-collections / ansible.utils

A collection of ansible utilities for the content creator.
GNU General Public License v3.0
76 stars 78 forks source link

keep_keys doesn't exclude a key if the value is list. #353

Open vbotka opened 6 months ago

vbotka commented 6 months ago
SUMMARY

The function ansible.utils.keep_keys doesn't exclude a key if the value is a list.

ISSUE TYPE
COMPONENT NAME

ansible.utils.keep_keys

ANSIBLE VERSION
ansible [core 2.16.6]
  config file = /export/scratch/sandbox/tmp1/test-15/ansible.cfg
  configured module search path = ['/home/vlado/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vlado/env/lib/python3.12/site-packages/ansible
  ansible collection location = /home/vlado/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/vlado/env/bin/ansible
  python version = 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] (/home/vlado/env/bin/python)
  jinja version = 3.1.4
  libyaml = True
COLLECTION VERSION
# /home/vlado/env/lib/python3.12/site-packages/ansible_collections
Collection    Version
------------- -------
ansible.utils 2.12.0
CONFIGURATION
CONFIG_FILE() = /export/scratch/sandbox/tmp1/test-15/ansible.cfg
DEFAULT_GATHERING(/export/scratch/sandbox/tmp1/test-15/ansible.cfg) = explicit
DEFAULT_HOST_LIST(/export/scratch/sandbox/tmp1/test-15/ansible.cfg) = ['/export/scratch/sandbox/tmp1/test-15/hosts']
DEFAULT_LOG_PATH(/export/scratch/sandbox/tmp1/test-15/ansible.cfg) = /var/log/ansible.log
DEFAULT_STDOUT_CALLBACK(/export/scratch/sandbox/tmp1/test-15/ansible.cfg) = yaml
EDITOR(env: EDITOR) = emacs
PAGER(env: PAGER) = /usr/bin/most -s
OS / ENVIRONMENT
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04 LTS"
STEPS TO REPRODUCE
- hosts: localhost

  vars:

    t: [k0, k1]

    l1:
      - {k0: A, k1: B, k2: [1], k3: foo}
      - {k0: A, k1: B, k2: [2], k3: bar}
    r1: "{{ l1 | ansible.utils.keep_keys(target=t) }}"

    l2:
      - {k0: A, k1: B, k2: C, k3: foo}
      - {k0: A, k1: B, k2: C, k3: bar}
    r2: "{{ l2 | ansible.utils.keep_keys(target=t) }}"

  tasks:

    - debug:
        var: r1
    - debug:
        var: r2

gives (abridged)

  r1:
  - k0: A
    k1: B
    k2:
    - 1
  - k0: A
    k1: B
    k2:
    - 2

  r2:
  - k0: A
    k1: B
  - k0: A
    k1: B
EXPECTED RESULTS
  r1:
  - k0: A
    k1: B
  - k0: A
    k1: B
ACTUAL RESULTS
  r1:
  - k0: A
    k1: B
    k2:
    - 1
  - k0: A
    k1: B
    k2:
    - 2
gaelgatelement commented 1 month ago

It should be fixed by https://github.com/ansible-collections/ansible.utils/pull/379