Dresden
Dresden

Reputation: 31

How to compared a nested pillar key value in an if statement in jinja2 for saltstack

I am working on a saltstack state with some salt wrapped in jinja2.

When I attempt to compare a value from a pillar using jinja2 it appears argument evaluates to nothing.

If I query the value using salt cli, it returns the expected value.

I expect I am referencing the value incorrectly in the if statement with jinja2.

Here is all the needed info to understand and look at this problem:

Salt Master id is salt-dev

Salt Minion id is on same instance and is salt-dev

Here is the pillar top file:

base:
  'salt-dev':
    - docker-daemon.docker-daemon

Here is the nested pillar file locate at /srv/pillar/docker-daemon/docker-daemon.sls

docker-daemon:
  - action: start
  - runlevel: enabled

Here is the output of the salt cli command returning the content of the pillar for the minion salt-dev:

# salt 'salt-dev' pillar.items
salt-dev:
    ----------
    docker-daemon:
        |_
          ----------
          action:
              start
        |_
          ----------
          runlevel:
              enabled

Here is the output of the value I am using in the if statement where the value returns nothing with jinja2, but returns as expected here with cli:

# salt 'salt-dev' pillar.get docker-daemon:action
salt-dev:
    start

The line of jinja2 that is incorrect is:

{% if salt['pillar.get']('docker-daemon:action') == 'start' %}

It appears: salt['pillar.get']('docker-daemon:action') returns nothing, but from cli as shown above it does return something.

Also if I add a default value, which is used in the event this arg returned nothing it also works.

An example of adding a default value is:

{% if salt['pillar.get']('docker-daemon:action', 'def_value') == 'start' %}

I have shown it in context below:

Here is the state file where the if statements are having the same issue:

{% if ( (grains['osfinger'] == 'Oracle Linux Server-6') and (grains['osarch'] == 'x86_64')) %}
sync_docker-init:
  file.managed:
    - name: /etc/init.d/docker
    - source: salt://docker-daemon/templates/docker-init
    - user: root
    - group: root
    - mode: 755

action_docker-init:
  {% if salt['pillar.get']('docker-daemon:action') == 'start' %}
  service.running:
  {% endif %}

  {% if salt['pillar.get']('docker-daemon:action') == 'stop' %}
  service.dead:
  {% endif %}

    - name: docker
    - require: 
      - pkg: install_docker-engine
    - watch: 
      - file: sync_docker-init

  {% if salt['pillar.get']('docker-daemon:runlevel') == 'enabled' %}
    -- enable: True
  {% endif %}

  {% if salt['pillar.get']('docker-daemon:runlevel') == 'disabled' %}
    -- enable: False
  {% endif %}

  {% else %}
  event.send:
    - tag: 'salt/custom/docker-init/failure'
    - data: "Management of docker init failed, OS not permitted."
{% endif %}

I am quite new at the moment to salt and jinja2, so this is 101 stuff, but I would appreciate some help, I have found nothing for some hours yet.

I attempted to echo this out and it seemed I just get a blank line

Upvotes: 0

Views: 2961

Answers (1)

Dresden
Dresden

Reputation: 31

I found the solution.

The pillar file /srv/pillar/docker-daemon/docker-daemon.sls was formed as a list instead of a map.

I changed it to this:

docker-daemon:
  action: restart
  runlevel: disabled

Upvotes: 2

Related Questions