saltstack / saltstack/salt

grains.filter_by list broken?

Open
#51,478 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug severity-medium
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with salt/modules/grains.py at filter_by and follow its call into salt/utils/data.py, where the reported traceback reaches traverse_dict_and_list. Reproduce the documented list-valued grain behavior with the command examples in the issue. Done means list syntax returns the first matching lookup value without the reported exception, with coverage for the shown Python versions if applicable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.