onchanges_any is running all the time

17 views
Skip to first unread message

brad.v...@gmail.com

unread,
Jun 4, 2024, 8:43:41 AMJun 4
to Salt-users
We have a grub.sls file:

`remove_ipv6_disable:
  file.replace:
    - name: /etc/default/grub
    - pattern: '^(GRUB_CMDLINE_LINUX.*)ipv6.disable=[01](.*)$'
    - repl: \\1\2'

{% set BOOT = '/boot/efi/EFI/redhat/grub.cfg' %}
{% set SLUB = salt['cmd.shell']('grep GRUB_CMDLINE_LINUX / etc/default/grub | grep -c "slub_debug=P"') | int %}
{% if SLUB == 0 %}

slub:
  cmd.run:
    - name: sed -i 's|^\ (GRUB_CMDLINE_LINUX.* \)"|\1slub_debug=P "|'  /etc/default/grub
    - runas: root

{% else %}
{%- do salt.log.error('slub_debug=P already added') -%}
{% endif %}

update_GRUB_CMDLIN:
  file.replace:
    - name: /etc/default/grub
    - pattern: 'rd.lvm.lv=vg_sys/lv_swap '
    - repl: ''

remove_crashkernel:
  file.replace:
    - name: /etc/default/grub
    - pattern: '^(GRUB_CMDLINE_LINUX.*)crashkernel=auto(.*)$'
    - repl: '\1\2'
    - onlyif: grep -q 'crashkernel=auto'  /etc/default/grub

add_boot_time_audit:
  file.replace:
    - name: /etc/default/grub
    - pattern: '^(GRUB_CMDLINE_LINUX(.(?!audit=1))*)(.audit=1)?(.*")$'
    - repl: '\1 audit=1 \4'
    - unless: grep audit=1 /etc/default/grub

/etc/grub2.cfg:
  file.symlink:
    - target: {{ BOOT }}
    - force: True
    - backupname: /etc/grub2.cfg.bak
    - user: root
    - group: root

make_config_if_changed:
  cmd.run:
    - name: /usr/sbin/grub2-mkconfig -o {{ BOOT }}
    - onchanges_any:
      - file: remove_ipv6_disable
      - file: slub
      - file: update_GRUB_CMDLINE
      - file: remove_crashkernel
      - file: add_boot_time_audit

The problem is the grub2-mkconfig is running every time even if no changes are made in any of the referenced stanzas.  I want to OR the results of each of those stanzas.  If any one changes, then run the grub2-mkconfig.  It is running even if nothing changes.

Kevin Landreth (cRacKErjACkmACK)

unread,
Jun 4, 2024, 9:07:36 PMJun 4
to Salt-users
Stanza `slub` runs every time with no requisites because it's using cmd.run . You should use `onchanges_in` in `slub` because it's already enclosed in the jinja conditional. You also want to remove `file: slub` from the `make_config_if_changed` as well as it will result in an error instead of rebuilding the config conditionally.

slub:
  cmd.run:
    - name: sed -i 's|^\ (GRUB_CMDLINE_LINUX.* \)"|\1slub_debug=P "|'  /etc/default/grub
    - runas: root
    - onchanges_in:
      - make_config_if_changed

brad.v...@gmail.com

unread,
Jun 5, 2024, 4:51:30 AMJun 5
to Salt-users
Thanks.  I will give that a try.

brad.v...@gmail.com

unread,
Jun 7, 2024, 10:52:52 AMJun 7
to Salt-users
I got rid of the set SLUB and if SLUB jinja wrapper and changed to:

slub:
  file.replace:
    - name: /etc/default/grub
    - pattern: '^(GRUB_CMDLINE_LINUX.*)"$'
    - repl: '\1 slub_debug=P "'
    - count: 1
    - append_if_not_found: True
    - onlyif: grep GRUB_CMDLINE_LINUX /etc/default/grub | grep -v slub_debug

and that works to add if not there.  If it is, and none of the other requisites are true, then the grub2-mkconfig is not run.  Thanks for the suggestion!
Reply all
Reply to author
Forward
0 new messages