EN VI

Ansible lineinfile - add substring only if missing?

2024-03-11 19:30:07
How to Ansible lineinfile - add substring only if missing

I'm trying to disable transparent_hugepage in the kernel. Which according to this article means adding the substring transparent_hugepage=never to the GRUB_CMDLINE_LINUX variable in /etc/default/grub.

My attempt at doing that looks like:

- name: disable transparent_hugepage
  ansible.builtin.lineinfile:
    path: /etc/default/grub
    regexp: '^(GRUB_CMDLINE_LINUX="[^"]+)"'
    line: '\g<1> transparent_hugepage=never"'
    backrefs: yes

That however adds the substring every time ansible runs, not only when it is missing.

i.e.

# grep CMD /etc/default/grub
GRUB_CMDLINE_LINUX="resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap rhgb quiet transparent_hugepage=never transparent_hugepage=never"

Surely there must be a better way to handle that than trying to do something fancy with regex negative lookahead?

I tried the lookahead approach using:

regexp: '^(GRUB_CMDLINE_LINUX="[^"]+)(?! transparent_hugepage=never)"'

but it behaves the same.

Solution:

Surely there must be a better way to handle that ...

To ensure that THP is disabled before other services starts, one should create a service file which disables THP during boot up. This is the recommend approach by certain Open Source Projects, one in example is MongoDB - Disable Transparent Huge Pages (THP).

Placing a service file

[Unit]
Description=Disable Transparent Huge Pages (THP)

[Service]
Type=simple
ExecStart=/bin/sh -c "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"

[Install]
WantedBy=multi-user.target

on a Remote Node via

- name: Create a service file in order to disable THP automatically
  copy: 
    src: disable-thp.service
    dest: /etc/systemd/system/disable-thp.service

than, a task will do the job

- name: Make sure Transparent Huge Pages (THP) are disabled
  systemd:
    state: started
    enabled: yes
    name: disable-thp

Double checking on CLI result into an output of

systemctl status disable-thp
○ disable-thp.service - Disable Transparent Huge Pages (THP)
     Loaded: loaded (/etc/systemd/system/disable-thp.service; enabled; preset: disabled)
     Active: inactive (dead) since Thu 2024-03-01 10:00:00 CET; 11 days ago
   Duration: 39ms
    Process: 884 ExecStart=/bin/sh -c echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && ...
   Main PID: 884 (code=exited, status=0/SUCCESS)
        CPU: 4ms

cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

cat /sys/kernel/mm/transparent_hugepage/defrag
always defer defer+madvise madvise [never]

Further Readings

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login