Search code examples
yamljinja2salt-project

Assigning and working with server variables in a Salt State


I'm very new to Salt/Jinja/YAML so apologies if this is a silly question. I have a decent understanding of the high level stuff conceptually but I want to get in an start writing some States to get experience.

So I want to write a State that can compare the versions of the software installed on a server to versions defined elsewhere or hard-coded values, using Jinja. So even just really simple like:

if 'pythonVersion' > 2.7.5
    print a message

I've written bash scripts to get those software versions and run scripts using a Salt state, but how do I actually access server variables and work with them in the State itself?


Solution

  • For example:

    {% set pythonVersion = salt["pkg.version"]("python") %}
    {% if salt["pkg.version_cmp"](pythonVersion, "2.7.5") > 0 %}
    {% do salt["log.info"]("a message") %}
    {% endif %}
    

    But it depends what you mean by "server variables", "write a state", and "print a message". Depending on what your high-level problem is, you may not need to do any of those things in the first place.