pillar.get and subkeys

430 views
Skip to first unread message

schlag

unread,
Jan 15, 2014, 4:14:19 PM1/15/14
to salt-...@googlegroups.com
Hi,

Is there a way to use pillar.get with subkeys?  I've tried all of the following which aren't working.  For example:

pillar.get('postgres_settings:pg_version', '9.2') 
pillar.get(['postgres_settings']['pg_version'], '9.2') 

This works on the cli, but does not in my state (see below).

salt-call pillar.get postgres_settings:pg_version
local:
    9.3


The reason I want this is to avoid extra logic in my states.  For example, I manage postgresql and it's requirements with a state file and multiple pillar values.  

Instead of this:

--snip
postgresql install:
  pkg.installed:
{% if pillar['postgres_settings']['pg_version'] is defined %}
    - name:     postgresql-{{ pillar['postgres_settings']['pg_version'] }}
{% else %}
    - name:     postgresql-9.2
{% endif %}
    - fromrepo: {{ grains.get('lsb_distrib_codename') }}-pgdg
--snip

I would like to do this with a single line:

--snip
postgresql install:
  pkg.installed:
    - name:     postgresql-{{ pillar.get('postgres_settings:pg_version', '9.2') }}
    - fromrepo: {{ grains.get('lsb_distrib_codename') }}-pgdg
--snip

Thanks in advance

Luminous Salt

unread,
Jan 16, 2014, 7:19:27 AM1/16/14
to salt-...@googlegroups.com
On 2014-01-15 16:14, schlag wrote:
> Hi,
>
> Is there a way to use pillar.get with subkeys? I've tried all of the
> following which aren't working. For example:
>
> pillar.get('postgres_settings:pg_version', '9.2')
>
> pillar.get(['postgres_settings']['pg_version'], '9.2')
>
> This works on the cli, but does not in my state (see below).
>
> salt-call pillar.get postgres_settings:pg_version
> local:
> 9.3
>
> The reason I want this is to avoid extra logic in my states. For
> example, I manage postgresql and it's requirements with a state file
> and multiple pillar values.

I generally simplify and reduce jinja clutter using 'set'.


eg, at the top of .sls:


{% set src_path = salt['pillar.get']('src_path', '/var/src/') %}

...

{# git repositories #}
{% for repo in salt['pillar.get']('repos', {}) %}
{% set r = pillar['repos'][repo] %}
{{ repo }}-git:
git.latest:
- name: {{ r.url }}
- rev: {{ r.rev }}
- target: {{ src_path }}/{{ repo }}/
- user: root
- require:
- file: root_ssh_private_key
- file: src_workdir
{% endfor %}


Tim O'Guin

unread,
Jan 16, 2014, 9:42:09 AM1/16/14
to salt-...@googlegroups.com
I've successfully used the Salt dictionary for this:

salt['pillar.get']('foo:bar:baz', 'default_value')


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Colton Myers

unread,
Jan 21, 2014, 11:40:03 AM1/21/14
to salt-...@googlegroups.com
If you look in the note in this section you'll see a note that points out that in states, just directly calling `pillar.get` actually just uses the jinja/python `.get()` method for dictionaries.  So you must do as Tim has shown and call the actual execution module function `pillar.get()` from within the `salt` dictionary.

--
Colton Myers

schlag

unread,
Jan 22, 2014, 11:14:56 AM1/22/14
to salt-...@googlegroups.com
yep, that works.  thanks all!
Reply all
Reply to author
Forward
0 new messages