0

Alright, I'm using salt to manage my grub.conf boot options (most importantly the isolated CPU's) in linux, and have a basic formula to accomplish that:

{% set cpu1 = (grains['num_cpus']-1) %}
{% set cpu2 = (grains['num_cpus']-2) %}
{% set cpu3 = (grains['num_cpus']-3) %}
{% set cpu4 = (grains['num_cpus']-4) %}
{% set cpu5 = (grains['num_cpus']-5) %}
{% set cpus = cpu5|string + ',' + cpu4|string + ',' + cpu3|string + ',' + cpu2|string + ',' + cpu1|string %}
{% set bootoptions = 'nosoftlockup isolcpus=' + cpus + ' intel_idle.max_cstate=0 idle=poll processor.max_cstate=0 mce=ignore_ce crashkernel=auto audit=1' %}

insert-options:
  file.replace:
    - name: /boot/grub/grub.conf
    - pattern: (kernel \/vmlinuz-.* quiet)(.*)
    - repl: '\1 {{ bootoptions }}'

The only problem with this is that I have to have a manual string of cpu's, which means if say I wanted to add an additional cpu to the list, or remove one, I have to write additional string bits, or remove string bits.

What would be ideal is something along the lines of:

{% for x in range(grains['num_cpus']-5,grains['num_cpus']-1) %}
{% set cpus = cpus + ',' + x|string %}
{% endfor %}

I've tried two methods of this, the first was written just as above, that gives me an error that cpus isn't defined because the first instance is still trying to set itself to itself, which is understandable.

My second attempt added an out of loop declaration to get things started:

{% set cpus = (grains['num_cpus']-6) %}
{% for x in range(grains['num_cpus']-5,grains['num_cpus']-1) %}
{% set cpus = cpus + ',' + x|string %}
{% endfor %}

but that ended up making cpus ONLY contain the value of the out of loop declared value.

Is there a way to make this loop process work?

It would make a huge difference, especially since then I could easily change the offset numbers (-1,-5) to pillar items to give me easily customized values for different servers that may need different numbers of isolated CPUs.

Gravy
  • 770
  • 1
  • 5
  • 17

1 Answers1

1

I wouldn't recommend using Jinja2 for somehow complex tasks. Rather choose a suitable renderer for the task to keep the code clear and still readable. You can choose e.g. Python language instead of default Jinja2 + YAML to get things done:

#!py

def run():
  config = {}
  cpus = ''

  for x in range(__grains__['num_cpus'] - 5, __grains__['num_cpus'] - 1 ):
    cpus += ',' + str(x)

  config['echo ' + str(cpus)] = {
    'cmd': [
      'run'
    ]
  }

  return config
dsmsk80
  • 5,757
  • 17
  • 22