count number of commas in a variable

36 views
Skip to first unread message

brad.v...@gmail.com

unread,
Nov 30, 2022, 9:45:10 AM11/30/22
to Salt-users
I'm not sure of the syntax of the jinja length filter.  I'm setting a variable to all of the installed kernel packages.  I want to check if there are more than two.  The variable is a comma separated list of the installed kernels.  I want to check of there are more than 'x' commas in that list.  Maybe length isn't the correct filter?

```{% set KERN = salt.pkg.version('kernel') %}
{% if KERN|length > 2 %}```

I know that is not correct.  It is just counting the number of characters.  Any suggestions?

Aaron Greengrass

unread,
Nov 30, 2022, 9:57:26 AM11/30/22
to salt-...@googlegroups.com
A suggestion, use something like 

{% KERNLIST = kern.split(',') %}
{% if KERNlist|length > 2 %}

That should convert your string into a list of strings, and length should then return the number of list items.

------ Original Message ------
To "Salt-users" <salt-...@googlegroups.com>
Date 11/30/2022 6:45:10 AM
Subject [salt-users] count number of commas in a variable

--
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/22846b5b-9fad-4b72-acce-17c86025cd76n%40googlegroups.com.

brad.v...@gmail.com

unread,
Nov 30, 2022, 10:03:32 AM11/30/22
to Salt-users
That has it:

{% set KERNLIST = KERN.split(',') %}
{% if KERNLIST|length > 2 %}

that works!   Thanks!

Phipps, Thomas

unread,
Nov 30, 2022, 10:48:24 AM11/30/22
to salt-...@googlegroups.com
so, not to nit pick but if you are just trying to count the commas splitting on comma means your length count will be one higher. you should -1 the length.

brad.v...@gmail.com

unread,
Nov 30, 2022, 11:45:02 AM11/30/22
to Salt-users
You are correct.  I changed the length to > 1.  We just want two kernels installed.  :)

brad.v...@gmail.com

unread,
Nov 30, 2022, 12:49:19 PM11/30/22
to Salt-users
Problem now is that all of the jinja is rendered before any salt stanzas are run.  I have:

{% if grains['manufacturer'].startswith('HP') %}
{% set KERN = salt.pkg.version('kernel') %}
{% set KERNLIST = KERN.split(',') %}
{% if KERNLIST|length > 1 %}

remove_older_kernel_versions:
  cmd.run:
    - name: /bin/package-cleanup -y --oldkernels --count=2

{% set KERN = salt.pkg.version('kernel') %}
{% endif %}
{% set DEVEL = salt.pkg.version('kernel-devel') %}
{% if grains['num_gpus'] > 1 %}
{% if KERN == DEVEL %}

print-kernel-info:
  cmd.run:
    - name: echo "kernel = {{ KERN }} - kernel-devel = {{ DEVEL }}"

{% else %}
{% set KERNLIST = KERN.split(',') %}
{% if DEVEL == KERNLIST[0] %}

kernel-devel2:
  pkg.installed:
    - name: kernel-devel
    - version: {{ KERNLIST[0] }}

{% endif %}
{% endif %}
{% else %}
{% if DEVEL is defined %}
{% if DEVEL|length %}

remove-devel-kernel:
  pkg.removed:
    - name: kernel-devel

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

I want the KERN variable reset after the extra kernels are removed and the DEVEL likewise.  They are all getting set before any salt stanzas are run.  I'm thinking about removing the package-cleanup section to a separate salt state and then just adding some code before this section to make sure it is run before this state?  Does that sound like the correct approach?

Phipps, Thomas

unread,
Nov 30, 2022, 3:25:35 PM11/30/22
to salt-...@googlegroups.com
i mean the best solution is to set installonly_limit to the number of kernels to keep depending on the machine kind in /etc/yum.conf and let yum do its job.

Brad Van Orden

unread,
Nov 30, 2022, 4:09:06 PM11/30/22
to salt-...@googlegroups.com
I do have that, but I need to:

1.  Remove all kernels beyond two on the hardware servers.
2.  Remove the kernel-devels from all hardware servers without Nvidia GPUs.

So, I want to test and see if this server has more than two kernel versions.  If so, reduce that.  Then, check if this server has Nvidia GPUs.  If so, check and see if both devel versions of the kernel are installed to match the two installed kernel versions.  If not a GPU server, remove any kernel-devel packages.

Regards,

Brad



You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/OMu2FsF3xjc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/CAPaX09hD-jYjSEEX%3D%3DnJis0r_fTOswR-OfmX%3DNmy8K6oPVdgrA%40mail.gmail.com.

Phipps, Thomas

unread,
Nov 30, 2022, 4:28:04 PM11/30/22
to salt-...@googlegroups.com
n it sounds like you need to get into orchestration. it is commonly used to get around the strict ordering of the render engines.

brad.v...@gmail.com

unread,
Dec 6, 2022, 5:47:04 AM12/6/22
to Salt-users
orchestration sounds interesting, but involves purchasing a package which will not happen just to satisfy this one problem.  I removed limiting the number of kernels out of this state.  I created a one time state to check the number of installed kernels and run the package cleanup application if needed.  My problem right now is trying to determine if the servers with a GPU have the kernel-devel versions installed to correspond to the installed kernel versions.  I attempted:

{% set KERN = salt.pkg.version('kernel') %}
{% set DEVEL = salt.pkg.version('kernel-devel') %}
{% if KERN == DEVEL %}

assuming that the comma separated lists would appear in the same order.  They don't.  One one server I got:

KERN = '3.10.0-1160.66.1.el7,3.10.0-1160.71.1.el7'
DEVEL-' 3.10.0-1160.71.1.el7, 3.10.0-1160.66.1.el7'

which really are equivalent but not strictly equal.  I tried sending to sort, but it sorted each by every single character.  :)  Not exactly what I wanted.  I guess I'll have to split on the commas and then see if each part appears in the other string?  Let me give that a try.

brad.v...@gmail.com

unread,
Dec 6, 2022, 7:52:36 AM12/6/22
to Salt-users
I got around this with:

{% set KERN = salt.pkg.version('kernel') %}
{% set DEVEL = salt.pkg.version('kernel-devel') %}
{% set KERNLIST = KERN.split(',') %}
{% if KERNLIST[0] not in DEVEL %}
{# KERNLIST[0]-devel not installed #}
{% endif %}
{% if KERNLIST[1] not in DEVEL %}
{# KERNLIST[1]-devel not installed #}
{% endif %}

and that is working.  Thanks for the help folks!

Phipps, Thomas

unread,
Dec 6, 2022, 11:49:18 AM12/6/22
to salt-...@googlegroups.com
I'm not sure where you heard you need to purchase something to use orchestration in salt. but that is completely untrue. the only part of salt that costs money is the enterprise gui interface. and professional support.



Brad Van Orden

unread,
Dec 7, 2022, 9:26:42 AM12/7/22
to salt-...@googlegroups.com
Just someone I work with said it would cost money.  I'll look into it in more detail.

Thanks!


Reply all
Reply to author
Forward
0 new messages