jinja compound if and order of precedence

58 views
Skip to first unread message

brad.v...@gmail.com

unread,
Apr 3, 2023, 10:36:44 AM4/3/23
to Salt-users
I'm have a state file in which I want to disable NetworkManager for all RHEL/CentOS 7 servers and all RHEL 8 servers which do not have a host name starting with "X."  I'm not sure of the syntax to establish precedence.  I was trying:

{%- if grains['osmajorrelease'] == 7 or ( if grains['osmajorrelease'] == 8 and if not grains['host'].startswith('X') ) -%}

Does anyone see a problem with that, or know of a better way?

Mahesh Reddy

unread,
Apr 3, 2023, 11:06:05 AM4/3/23
to salt-...@googlegroups.com
I'd use os_family=redhat rather than specifying the version. This is a unique value for CentOS and RedHat and if you have custom grains for those hosts I'd use not [custom_grain]=value. 

if custom_grain name is <env name>
custom grains: salt-call grains.setvals "{'env': 'Test'}" && salt-call grains.get env


then the final code will be 
{% if grains['os_family'] = 'redhat' not grains['env'] = 'Test' %}



--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/413e6a34-8419-4c72-ac86-0951ec08885dn%40googlegroups.com.


--
Sincerely,

Mahesh
9100030600

Phipps, Thomas

unread,
Apr 3, 2023, 11:21:19 AM4/3/23
to salt-...@googlegroups.com

sorry, that did not answer the question at all. from the question the user wants all version 7 systems and only version 8 systems that do not start with x. this has nothing to do with testing if redhat.

personally i would stop trying to match inside jinja and break this into smaller files that get used by the top targeting.

but barring that

{% if salt['match.compound']('G@osmajorrelease:7 or ( G@osmajorrelease:8 and not G@host:X* )') %}


brad.v...@gmail.com

unread,
Apr 3, 2023, 11:21:28 AM4/3/23
to Salt-users
I was thinking of simplifying the compoundness with nesting.  Using this instead:

{%- if grains['osmajorrelease'] == 7 -%}
{%-   if grains['osmajorrelease'] == 8 -%}
{%-     if not grains['host'].startswith('X') -%}

NetworkManager:
  service.dead:
    - enable: False

{%-     endif -%}
{%-   endif -%}
{%- endif -%}

I think that will get what I want.  In the process of testing.

Phipps, Thomas

unread,
Apr 3, 2023, 11:26:14 AM4/3/23
to salt-...@googlegroups.com
that won't work. osmajorversion can not be both 7 and 8. but with that nesting they would have to be.

brad.v...@gmail.com

unread,
Apr 3, 2023, 11:57:41 AM4/3/23
to Salt-users
{% if salt['match.compound']('G@osmajorrelease:7 or ( G@osmajorrelease:8 and not G@host:X* )') %}

works great!   Thanks so much for the help!
Reply all
Reply to author
Forward
0 new messages