tarilabs / demo20230626-ansible-playground

supporting conversation at https://github.com/ansible/ansible-rulebook/pull/539#discussion_r1240158903

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

supporting conversation at ansible/ansible-rulebook#539 (comment)

given 3 items (vertical scroll in github):

vars:
fruits:
- name: apple
color: red
nesting:
alphabet: vowel
nesting.alphabet: none
- name: banana
color: yellow
nesting.alphabet: none
nesting:
alphabet: consonant
- name: cherry
color: red
nesting:
alphabet: consonant
nesting.alphabet: none

using:

- debug:
msg: '{{ fruits | selectattr("color", "==", "red") }}'

results in:

TASK [debug] **********************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "color": "red",
            "name": "apple",
            "nesting": {
                "alphabet": "vowel"
            },
            "nesting.alphabet": "none"
        },
        {
            "color": "red",
            "name": "cherry",
            "nesting": {
                "alphabet": "consonant"
            },
            "nesting.alphabet": "none"
        }
    ]
}

as expected.

using:

# has no attribute \"nesting['alphabet']\". :
- debug:
msg: '{{ fruits | selectattr("nesting[''alphabet'']", "==", "consonant") }}'

results in has no attribute \"nesting['alphabet']\". see:

TASK [debug] **********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute \"nesting['alphabet']\". 'dict object' has no attribute \"nesting['alphabet']\"\n\nThe error appears to be in '/Users/mmortari/git/demo20230626-ansible-playground/empiric1.playbook.yml': line 43, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    # has no attribute \\\"nesting['alphabet']\\\". :\n    - debug:\n      ^ here\n"}

so it would seems like, empirically, squared accessor is not supported in selectattr.

using:

# has no attribute \"nesting['alph\". :
- debug:
msg: '{{ fruits | selectattr("nesting[''alph.abet'']", "==", "consonant") }}'

results in has no attribute \"nesting['alph\". see:

TASK [debug] **********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute \"nesting['alph\". 'dict object' has no attribute \"nesting['alph\"\n\nThe error appears to be in '/Users/mmortari/git/demo20230626-ansible-playground/empiric1.playbook.yml': line 48, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    # has no attribute \\\"nesting['alph\\\". :\n    - debug:\n      ^ here\n"}

so it would seems like, empirically, squared accessor is completely ignored and just raw/brutally splitted on . for the 1st argument in selectattr.

About

supporting conversation at https://github.com/ansible/ansible-rulebook/pull/539#discussion_r1240158903