Search code examples
salt-project

Saltstack cmd.run - if statement and unless


I manage the installation of our workstations with Saltstack. In my recipe for the ipa-client-automount installation on clients, I need to:

  • set the location based on fqdn
  • check if ipa-client-automount is already configured

Currently, I have the following state:

 ipa-client-automount:
  cmd.run:
    {% if salt['cmd.run']('hostname -f | grep domain1') %}
    - name: ipa-client-automount --location=linkedtodomain1 -U
    {% elif salt['cmd.run']('hostname -f | grep domain2') %}
    - name: ipa-client-automount --location=linkedtodomain2 -U
    {% endif %}
    - unless: python -c "from ipapython import sysrestore; from ipaplatform.paths import paths; statestore = sysrestore.StateFile(paths.IPA_CLIENT_SYSRESTORE); exit(not statestore.has_state('autofs'))"

The issue is that when adding the if and elif statement, it doesn't consider the unless. It runs directly the command without checking the unless condition. Also, I'm sure that my unless statement is working, it was all fine with only one location.

How can I write this to have the if and unless working at the same time? Thanks


Solution

  • I have a working solution:

    ipa-client-automount:
      cmd.run:
        - names: 
          {% if salt['cmd.run']('hostname -f | grep domain1') %}
          - ipa-client-automount --location=linkedtodomain1 -U
          {% elif salt['cmd.run']('hostname -f | grep domain2') %}
          - ipa-client-automount --location=linkedtodomain2 -U
          {% endif %}
        - unless: condition
    

    It's not the cleanest solution but it worked for me. Don't know why this doesn't work on names but does on name.