bbinet / pillarstack

Custom saltstack ext_pillar heavily based on jinja2 for maximum flexibility
MIT License
58 stars 15 forks source link

pillar.get in stack.cfg does not get nested values #52

Open giorgiozeeman opened 6 years ago

giorgiozeeman commented 6 years ago

Hello,

we are successfully using PillarStack with GitStack-Pillar.

However, we noticed that the pillar.get function in the PillarStack configuration file (stack.cfg), seems unable to retrieve nested items and it falls back to the default value.

We would like to use this function to be able to set a default value in case the nested variable is not defined.

The nested value actually exists in the pillar dictionary.

[saltmaster ~]# salt host.dev.acme.net pillar.item ssl_certs:cert_names
host.dev.acme.net:
    ----------
    ssl_certs:cert_names:
        - star_dev_acme_net

The pillar function works...

pillar['ssl_certs']['cert_names']  

pillar.ssl_certs.cert_names       

However the pillar.get function is unable to get the nested item and it always falls back to the default star_acme_net.

pillar.get('ssl_certs:cert_names', ['star_acme_net']) 

This is an excerpt of our /etc/salt/master

ext_pillar:
  - git:
    - __env__ https://bitbucket.acme.net/scm/salt/pillar.git
  - gitstack:
    - __env__ https://bitbucket.acme.net/scm/salt/pillar.git:
      - stack:
        - stack.cfg

This is the output of salt --versions

Salt Version:
           Salt: 2018.3.3

Dependency Versions:
           cffi: 1.6.0
       cherrypy: unknown
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: 0.26.3
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
   mysql-python: Not Installed
      pycparser: 2.14
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: 0.26.4
         Python: 2.7.5 (default, Jul 13 2018, 13:06:57)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: Not Installed
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: centos 7.5.1804 Core
         locale: UTF-8
        machine: x86_64
        release: 3.10.0-862.14.4.el7.x86_64
         system: Linux
        version: CentOS Linux 7.5.1804 Core

Thanks!

b-a-t commented 5 years ago

I've faced with the same problem and the official answer is that pillar.get is a pure dict pillar method get, which has no idea of the nested hierarchy and complex keys. Contrary to that salt['pillar.get'] module function has knowledge about the nested keys and should work in such situation.

The only problem is that in the Stack config file salt is undefined and __salt__['pillar.get']('ssl_certs:cert_names', []) doesn't return given certificate names either....

See also: https://github.com/saltstack/salt/pull/7896/files

bbinet commented 5 years ago

This is not documented yet, but you can use the __stack__['traverse'] function as described in a previous comment: https://github.com/bbinet/pillarstack/issues/13#issuecomment-223214506 If this works for you, feel free to submit a pull request to add this feature to the readme.

gbunt commented 4 years ago

Haven't been able to get nested keys directly working either but this seems to works for me:

{%- set node = pillar.get('node') %}
regions/{{ node.region }}.yml

where node is defined in a stack config loaded before the one containing the above example.