salt icon indicating copy to clipboard operation
salt copied to clipboard

Filtering Map files

Open haeac opened this issue 9 years ago • 2 comments

Description of Issue/Question

Hello,

I'm trying to filter my map data based on OS and major release version. However, I can't seem to filter out the same. Any help would be appreciated.

Thanks.

Setup

defaults.yaml:

# vim: sts=2 ts=2 sw=2 et ai
{% load_yaml as rawmap %}
RedHat:
  7:
    packages:
      - 'xyz'
      - '.....'
  6:
    packages:
      - 'blahh'   
{% endload %}

packages.sls file:

{% from "defaults/defaults.yaml" import rawmap with context %}
{%- set MajorReleaseVersion = salt['grains.get']('osmajorrelease', {}) %}
{%- set Defaults = salt['grains.filter_by'](rawmap, grain='os_family', merge=salt['pillar.get']('defaults:lookup',{})) %}
{%- set Packages = Defaults[MajorReleaseVersion[0]] %}
install_default_packages:
  pkg.installed:
    - pkgs:
      {% for Package in Packages.packages %}
      - {{ Package }}
      {% endfor %}

Steps to Reproduce Issue

salt 'test*' state.apply defaults.packages

The output is as follows:

Rendering SLS 'dev:defaults.packages' failed: Jinja variable 'dict object' has no attribute '7'

Versions Report

Master:

Salt Version:
           Salt: 2016.11.0

Dependency Versions:
           cffi: 1.6.0
       cherrypy: Not Installed
       dateutil: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: 1.3.8
         Jinja2: 2.7.2
        libgit2: 0.21.0
        libnacl: 1.4.3
       M2Crypto: 0.21.1
           Mako: 0.8.1
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: 2.14
       pycrypto: 2.6.1
         pygit2: 0.21.4
         Python: 2.7.5 (default, Nov  6 2016, 00:28:07)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: 0.6.5
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

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

Minion:

Salt Version:
           Salt: 2016.11.1

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: 1.3.8
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: 1.4.3
       M2Crypto: Not Installed
           Mako: 0.8.1
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.5 (default, Nov  6 2016, 00:28:07)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: 0.6.5
          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.el7.x86_64
         system: Linux
        version: CentOS Linux 7.2.1511 Core

haeac avatar Jan 11 '17 18:01 haeac

@haeac if you want to use multiple grain values to filter on you will need to nest a second filter by call. Here is an example use case:

[root@3ebd0a0a22be /]# cat /srv/salt/defaults/defaults.yaml 
{% set map_test = salt["grains.filter_by"]( 
    salt["grains.filter_by"]({ 
      "RedHat": { 
        "7": { 
          "package": "my_formula_amd64.deb", 
          }, 
      }, 
    }, 
    grain="os_family", 
    merge=salt["pillar.get"]("my_formula:lookup", {})), 
  grain="osmajorrelease") 
%} 
[root@3ebd0a0a22be /]# cat /srv/salt/packages.sls           
{% from "defaults/defaults.yaml" import map_test with context %}
echo_map: 
  cmd.run: 
    - name: | 
        echo 'Map: {{ map_test.package }}' 

Does that work for you?

Ch3LL avatar Jan 12 '17 20:01 Ch3LL

Hello,

@Ch3LL thanks for the response. I think that should work.

haeac avatar Jan 12 '17 22:01 haeac