Answers: <p>It looks like you are on the right approach here, the issue seems to be one of debugging. I have found this <a href="https://cryptic-cliffs-32040.herokuapp.com/" rel="nofollow noreferrer">jinja2 live templating engine</a> to be helpful with debugging in the past.</p>
  <p>In python (on which jinja is based) <a href="https://problemsolvingwithpython.com/04-Data-Types-and-Variables/04.02-Boolean-Data-Type/" rel="nofollow noreferrer">&quot;True&quot; is a keyword</a> which is probably getting interpreted as a boolean <a href="https://en.wikipedia.org/wiki/Data_type" rel="nofollow noreferrer">data type</a>. This would be in contrast to the string, integer, or floating point (decimal) data types which are a C/C++ concept that python inherits since it's interpreter is written in C. Python tries to be agnostic to data type and variables are not <a href="https://en.wikipedia.org/wiki/Strong_and_weak_typing" rel="nofollow noreferrer">strongly typed</a>. This appears to be a situation in which the lack of strong typing may be getting you into some hot water.</p>
  <p>In your boolean logic in the &quot;if&quot; block says to only render the section if <code>allow_httpd</code> is a string - but it isn't! It's a boolean! This is likely the error. Instead, you may wish to try simply:</p>
  <pre><code>{% if allow_httpd %}
  Make sure httpd service is running:
    service.enabled:
      - name: httpd
  
  {% else %}
  
  stop and disable httpd:
    service.dead:
      - name: httpd
      - enable: False
  
  {% endif %}
  </code></pre>
  <p>I'd also recommend you remove your quotes in the pillar data if you use this approach.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-05-12T01:43:06.773Z
Answers: 2021-05-12T01:43:06.773Z
Answers: James Shewey
Body: <p>This is salt 101 I think, but I'm really a salt N00b and need assistance.</p>
  <p>I need to disable specific services on all of our servers by default, but I need to create a pillar that allows me to override this, and allow the specific servers mentioned in pillar to have those services running.  For the sake of discussion, let's assume the service is httpd.</p>
  <p>Currently I have a VERY simple state named &quot;disable_httpd.sls&quot; that says this...</p>
  <pre><code>{% from &quot;security/map.jinja&quot; import security with context %}
  stop and disable httpd-server:
    service.dead:
      - name: httpd
      - enable: False
  </code></pre>
  <p>Now we do have a few web servers where we need to allow it, and obviously my state, which is called as part of our security items, will break those servers.</p>
  <p>I need to figure out how to write a pillar that will exempt specific servers.</p>
  <p>Thoughts?</p>
  <hr />
  <p>Update: Here's what I have tried so far, but it's not working.</p>
  <p>Note that this is being tried in a lab, and the system I'm playing with is named &quot;saltminion1.local&quot;.</p>
  <ol>
  <li>Changed the state (disable_httpd.sls) to say this...</li>
  </ol>
  <pre><code>{% from &quot;security/map.jinja&quot; import security with context %}
  
  {% set allow_httpd = salt['pillar.get']('security:options:allow_httpd', &quot;&quot;) %}
  {% if &quot;True&quot; in allow_httpd and allow_httpd is string %}
  Make sure httpd service is running:
    service.enabled:
      - name: httpd
  
  {% else %}
  
  stop and disable httpd:
    service.dead:
      - name: httpd
      - enable: False
  
  {% endif %}
  </code></pre>
  <p>Then in pillar I created a file named &quot;saltminion1.sls&quot; in the &quot;security&quot; directory.</p>
  <pre><code>security:
    options:
      allow_httpd: 'True'
  </code></pre>
  <p>I modified pillar/top.sls to add this section...</p>
  <pre><code>prod:
    'saltminion1.local':
      - match: nodegroup
      - security.saltminion1
  </code></pre>
  <p>The thought is that it should pick up the &quot;True&quot; in the saltminion1.sls pillar file, and allow httpd.  What it's actually doing is the &quot;else&quot; section of the state, and killing the service.</p>
  
Tags: <salt><pillar>
Title: Salt - Need state to check pillar and perform a if/else check
UserDisplayName: Egyas
ThreadText: # Salt - Need state to check pillar and perform a if/else check
  
  (Asked by: username_0 on 2021-02-09)
  
  This is salt 101 I think, but I'm really a salt N00b and need assistance.
  
  I need to disable specific services on all of our servers by default, but I need to create a pillar that allows me to override this, and allow the specific servers mentioned in pillar to have those services running. For the sake of discussion, let's assume the service is httpd.
  
  Currently I have a VERY simple state named "disable\_httpd.sls" that says this...
  
  ```
  {% from "security/map.jinja" import security with context %}
  stop and disable httpd-server:
    service.dead:
      - name: httpd
      - enable: False
  
  ```
  
  Now we do have a few web servers where we need to allow it, and obviously my state, which is called as part of our security items, will break those servers.
  
  I need to figure out how to write a pillar that will exempt specific servers.
  
  Thoughts?
  
  ---
  
  Update: Here's what I have tried so far, but it's not working.
  
  Note that this is being tried in a lab, and the system I'm playing with is named "saltminion1.local".
  
  1. Changed the state (disable\_httpd.sls) to say this...
  
  ```
  {% from "security/map.jinja" import security with context %}
  
  {% set allow_httpd = salt['pillar.get']('security:options:allow_httpd', "") %}
  {% if "True" in allow_httpd and allow_httpd is string %}
  Make sure httpd service is running:
    service.enabled:
      - name: httpd
  
  {% else %}
  
  stop and disable httpd:
    service.dead:
      - name: httpd
      - enable: False
  
  {% endif %}
  
  ```
  
  Then in pillar I created a file named "saltminion1.sls" in the "security" directory.
  
  ```
  security:
    options:
      allow_httpd: 'True'
  
  ```
  
  I modified pillar/top.sls to add this section...
  
  ```
  prod:
    'saltminion1.local':
      - match: nodegroup
      - security.saltminion1
  
  ```
  
  The thought is that it should pick up the "True" in the saltminion1.sls pillar file, and allow httpd. What it's actually doing is the "else" section of the state, and killing the service.
  
  **Tags:** salt, pillar
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-12. Score: 1
  
  It looks like you are on the right approach here, the issue seems to be one of debugging. I have found this [jinja2 live templating engine](https://cryptic-cliffs-32040.herokuapp.com/) to be helpful with debugging in the past.
  
  In python (on which jinja is based) ["True" is a keyword](https://problemsolvingwithpython.com/04-Data-Types-and-Variables/04.02-Boolean-Data-Type/) which is probably getting interpreted as a boolean [data type](https://en.wikipedia.org/wiki/Data_type). This would be in contrast to the string, integer, or floating point (decimal) data types which are a C/C++ concept that python inherits since it's interpreter is written in C. Python tries to be agnostic to data type and variables are not [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing). This appears to be a situation in which the lack of strong typing may be getting you into some hot water.
  
  In your boolean logic in the "if" block says to only render the section if `allow_httpd` is a string - but it isn't! It's a boolean! This is likely the error. Instead, you may wish to try simply:
  
  ```
  {% if allow_httpd %}
  Make sure httpd service is running:
    service.enabled:
      - name: httpd
  
  {% else %}
  
  stop and disable httpd:
    service.dead:
      - name: httpd
      - enable: False
  
  {% endif %}
  
  ```
  
  I'd also recommend you remove your quotes in the pillar data if you use this approach.
  
  
  ---
  
  