Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grains.filter_by list broken? #51478

Open
johnnybubonic opened this issue Feb 2, 2019 · 6 comments
Open

grains.filter_by list broken? #51478

johnnybubonic opened this issue Feb 2, 2019 · 6 comments
Labels
Bug broken, incorrect, or confusing behavior severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around
Milestone

Comments

@johnnybubonic
Copy link

Description of Issue/Question

Per the docs, grains.filter_by's grain parameter supposedly accepts a list:

Changed in version 2016.11.0: The grain value could be a list. The function will return the lookup_dict value for a first found item in the list matching one of the lookup_dict keys.

However, this:

{% set os_family_map = salt['grains.filter_by'](
{
    'CentOS Linux-7': {
        'pkg': 'nmap-ncat',
    },
    'CentOS-6': {
        'pkg': 'nc',
    },
    'Red Hat Enterprise Linux Server-7': {
        'pkg': 'nmap-ncat',
    },
    'Arch Linux': {
        'pkg': 'gnu-netcat',
    },
    'default': {}
},
grain="osfinger,os",
merge=salt['pillar.get']('netcat:lookup')) %}

does not work, as it returns blank and defaults to the (incorrect, for those keys) default values. This can be shown at the commandline:

salt -L 'arch,centos6_32' grains.filter_by '{ CentOS Linux-7: { pkg: nmap-ncat }, CentOS-6: { pkg: nc }, Red Hat Enterprise Linux Server-7: {pkg: nmap-ncat }, Arch Linux: { pkg: gnu-netcat }, default: {} }' grain=osfinger,os
jid: 20190202122041160200
centos6_32:
    ----------
arch:
    ----------

Yet:
salt -L 'arch,centos6_32' grains.get osfinger:

(...)
arch:
centos6_32:
    CentOS-6

salt -L 'arch,centos6_32' grains.get os:

centos6_32:
    CentOS
arch:
    Arch

I've tried multiple variations on the list syntax.

  • grain1,grain2: Nothing returns.
  • [grain1,grain2]: The following error is returned:
    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/salt/minion.py", line 1607, in _thread_return
        return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
      File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
        return func(*args, **kwargs)
      File "/usr/lib/python2.7/site-packages/salt/modules/grains.py", line 600, in filter_by
        base=base)
      File "/usr/lib/python2.7/site-packages/salt/utils/data.py", line 403, in filter_by
        val = traverse_dict_and_list(traverse, lookup, [])
      File "/usr/lib/python2.7/site-packages/salt/utils/data.py", line 475, in traverse_dict_and_list
        for each in key.split(delimiter):
    AttributeError: 'list' object has no attribute 'split'
  • ["grain1","grain2"]: Same as [grain1,grain2].
  • ["grain1,grain2"]: Same as [grain1,grain2].
  • grain1:grain2: (As expected, as this is reserverd for dicts, but worth a shot because of the referenced error line) Nothing returns.
  • [grain1:grain2]: Nothing returns.

etc.

It happens on python 3.4 as well:

    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/lib/python3.4/site-packages/salt/minion.py", line 1607, in _thread_return
        return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
      File "/usr/lib/python3.4/site-packages/salt/executors/direct_call.py", line 12, in execute
        return func(*args, **kwargs)
      File "/usr/lib/python3.4/site-packages/salt/modules/grains.py", line 600, in filter_by
        base=base)
      File "/usr/lib/python3.4/site-packages/salt/utils/data.py", line 403, in filter_by
        val = traverse_dict_and_list(traverse, lookup, [])
      File "/usr/lib/python3.4/site-packages/salt/utils/data.py", line 475, in traverse_dict_and_list
        for each in key.split(delimiter):
    AttributeError: 'list' object has no attribute 'split'

Versions Report

Happening on all my minions. Master and all minions are on 2018.3.3, mix of python 3.4 and 2.7.

@Ch3LL
Copy link
Contributor

Ch3LL commented Feb 5, 2019

looks like i'm able to replicate this. glancing at the code it seems we might need to loop through the list before making the call to traverse_dict_and_list.

@Ch3LL Ch3LL added Bug broken, incorrect, or confusing behavior severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around P4 Priority 4 triage labels Feb 5, 2019
@Ch3LL Ch3LL added this to the Approved milestone Feb 5, 2019
@stale
Copy link

stale bot commented Jan 9, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

@stale stale bot added the stale label Jan 9, 2020
@johnnybubonic
Copy link
Author

johnnybubonic commented Jan 9, 2020

keep open

edit: has this been addressed yet?

@stale
Copy link

stale bot commented Jan 9, 2020

Thank you for updating this issue. It is no longer marked as stale.

@stale stale bot removed the stale label Jan 9, 2020
@Ch3LL
Copy link
Contributor

Ch3LL commented Jan 29, 2020

nope, we changed our settings with stalebot to not close issues with bug/feature labels so this should not go stale again.

@sagetherage sagetherage removed the P4 Priority 4 label Jun 3, 2020
@gluf-technik
Copy link

I'm seeing the same issue, when I use a list of grains nothing matches at all:
{{{
kvm-master:~# salt -C "$MINION" grains.filter_by '{Debian: foo, bookworm: bar, Alpine: qux, FreeBSD-13: bsd}' grain="[oscodename, osfinger, os_family]"
u2204-kvm-minion:
None
deb11-kvm-minion:
None
deb12-kvm-minion:
None
fbsd13-kvm-minion:
None
alpine317-kvm-minion:
None
obsd72-kvm-minion:
None
}}}

With a scalar value/simple string it works just fine:
{{{
kvm-master:~# salt -C "$MINION" grains.filter_by '{Debian: foo, bookworm: bar, Alpine: qux, FreeBSD-13: bsd}' grain="os_family"
alpine317-kvm-minion:
qux
deb11-kvm-minion:
foo
fbsd13-kvm-minion:
None
u2204-kvm-minion:
foo
deb12-kvm-minion:
foo
obsd72-kvm-minion:
None
}}}

(Master and all minions but the OpenBSD one are running 3005.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around
Projects
None yet
Development

No branches or pull requests

4 participants