salt icon indicating copy to clipboard operation
salt copied to clipboard

Grain matching failing where grain value is an INT

Open mgresser opened this issue 9 years ago • 3 comments

Description of Issue/Question

After upgrading from 2016.3.2 to 2016.11.2 I am seeing errors where map.jinja is hitting errors during grains.filter_by when it appears to be doing case matching non string values.

local:
    Data failed to compile:
----------
    Rendering SLS 'base:test' failed: Jinja error: expected string or buffer
/var/cache/salt/minion/files/base/test/map.jinja(7):
---
[...]
# vim: ft=jinja

{## Start with  defaults from defaults.yaml ##}
{% import_yaml "test/defaults.yaml" as default_settings %}

{% set test_map = salt['grains.filter_by']({    <======================
    'default': {},
    '0': {
      'status': 'Overridden', },
  },
  grain='gid', merge=salt['pillar.get']('test:lookup'))
[...]
---
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 368, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 1013, in make_module
    return TemplateModule(self, self.new_context(vars, shared, locals))
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 1070, in __init__
    self._body_stream = list(template.root_render_func(context))
  File "/var/cache/salt/minion/files/base/test/map.jinja", line 7, in top-level template code
    {% set test_map = salt['grains.filter_by']({
  File "/usr/lib/python2.7/site-packages/salt/modules/grains.py", line 563, in filter_by
    if fnmatch.fnmatchcase(each, key):
  File "/usr/lib64/python2.7/fnmatch.py", line 79, in fnmatchcase
    return _cache[pat].match(name) is not None
TypeError: expected string or buffer

Setup

Here's the state I'm testing with:

init.sls

{% from "test/map.jinja" import test_settings with context %}

file_tmp_test_grains:
  file.managed:
    - name: /tmp/test.txt
    - source: salt://test/files/test.txt
    - user: root
    - group: root
    - mode: 744
    - template: jinja

defaults.yaml

# -*- coding: utf-8 -*-
# vim: ft=yaml
default:
  status: 'Default'

map.jinja

# -*- coding: utf-8 -*-
# vim: ft=jinja

{## Start with  defaults from defaults.yaml ##}
{% import_yaml "test/defaults.yaml" as default_settings %}

{% set test_map = salt['grains.filter_by']({
    'default': {},
    '0': { 
      'status': 'Overridden', },
  },
  grain='gid', merge=salt['pillar.get']('test:lookup'))
%}

{% do default_settings.default.update(test_map) %}

{## Merge in test:lookup pillar ##}
{% set test_settings = salt['pillar.get'](
    'test:lookup',
    default=default_settings.default,
    merge=True)
%}

test.txt

{% from "test/map.jinja" import test_settings with context %}

{{ test_settings.status }}

Steps to Reproduce Issue

salt-call state.sls test

Versions Report

Minion:

           Salt: 2016.11.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.5 (default, Nov 20 2015, 02:00:19)
   python-gnupg: Not Installed
         PyYAML: 3.10
          PyZMQ: 14.7.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5

System Versions:
           dist: centos 7.2.1511 Core
        machine: x86_64
        release: 3.10.0-327.el7.x86_64
         system: Linux
        version: CentOS Linux 7.2.1511 Core

Master:

           Salt: 2016.11.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: 3.2.2
       dateutil: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.7
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.5 (default, Sep 15 2016, 22:37:39)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: centos 7.2.1511 Core
        machine: x86_64
        release: 3.10.0-327.18.2.el7.x86_64
         system: Linux
        version: CentOS Linux 7.2.1511 Core

mgresser avatar Feb 10 '17 20:02 mgresser

I saw an issue I believe to be related with the biosversion grain which in this case is "-[TKE140YUS-1.60]-" and with productname which in this case is "BladeCenter HS23 -[7875AC1]-"

For each of these using a version of the above state with the grain to match and value replaced the default value is returned from the map. It seems like the grain returns variables that are no longer valid cases in this map. These states worked correctly in 2016.3.2 but stopped matching correctly after upgrading.

mgresser avatar Feb 10 '17 20:02 mgresser

Thanks for reporting this.

This was introduced when we started switching grains variables over to integers instead of strings.

https://github.com/saltstack/salt/pull/39361

I have fixed the issue in the above pull request,

Thanks, Daniel

gtmanfred avatar Feb 13 '17 16:02 gtmanfred

Thanks for the quick fix, Daniel!

mgresser avatar Feb 13 '17 16:02 mgresser