Search code examples
ansiblemigrationsalt-project

What is the equivalent of Ansible's "when" clause in SaltStack?


For the given below Ansible code, how can I implement a similar functionality in SaltStack (specifically when clause)?

---
- include: install-redhat.yml
  when: ansible_os_family == "RedHat"

- include: install-debian.yml
  when: ansible_os_family == "Debian"

Do I have to use Jinja2 templates for this? It looks like unless and onlyif can only test return codes of shell commands.


Solution

  • Yes, you have to use jinja for this. Something like

    {% if grains['os'] == 'Redhat' %}
    include:
      - install-redhat
    {% endif %}
    
    

    But I would rather include the states in top file for example, in top.sls, you can do

    'os:Redhat':
      - match: grain
      - state1_redhat
      - state2_redhat
    
    'os:FreeBSD':
      - match: grain
      - freebsd1
      - freebsd2