Search code examples
salt-project

Salt States and grain values


I added the following logic to my state file which basically sets a new grain value after installing a utility called agent for the first time.

{% if salt['grains.get']('agent') != 'installed' %}
..............
agent_status:
  grains.present:
    - name: agent
    - value: installed

{% endif %}

The first time I run salt 'server1' state.highstate it returns the following which is what I expect:

----------
          ID: agent_status
    Function: grains.present
        Name: agent
      Result: True
     Comment: Set grain agent to installed
     Started: 16:03:27.083578
    Duration: 709.795 ms
     Changes:   
              ----------
              agent:
                  installed

When I subsequently run salt 'server1' state.highstate it returns:

server1:
 ----------
          ID: states
    Function: no.None
      Result: False
     Comment: No states found for this minion
     Started: 
    Duration: 
     Changes:   

Summary
------------
Succeeded: 0
Failed:    1

Is this the correct behaviour as I'm a little confused because I would have expected this not to show as a failed? Also, the comment is seems to be a bit misleading here.


Solution

  • Yeah, this is the correct behavior. What's happening is that first Salt renders the jinja. Since the second time you run this the grain exists, your minion is seeing an empty sls file. Hence the "No states found for this minion"

    Edit: If you want to avoid getting the "No states found for this minion" error, you could add an innocuous state at the bottom, outside the jinja like this

    /tmp/deletemeplease.txt:
      file.absent