# Expend the text in the end of a special line in a file

**URL:** <https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219>\
**Category:** Get Help\
**Tags:** ini, grub\
**Created:** [December 17, 2024, 9:51pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219 "2024-12-17T21:51:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 17, 2024, 9:51pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/1 "2024-12-17T21:51:27Z")

</div>

Hi Folks,  
I am new here, can someone give me a hand to show me how to add the text/items to the special place in the a file?  
Here is the line what I have from /etc/default/grub now:  
//////////////////  
GRUB\_CMDLINE\_LINUX=“nofb quiet splash=quiet crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/OS-swap rd.lvm.lv=OS/root rd.lvm.lv=OS/swap rd.lvm.lv=OS/usr audit=1 audit\_backlog\_limit=8129”  
/////////////////  
After the ansible code run, I need insert transparent\_hugepages=never into the line as following  
//////////////////  
GRUB\_CMDLINE\_LINUX=“nofb quiet splash=quiet crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/OS-swap rd.lvm.lv=OS/root rd.lvm.lv=OS/swap rd.lvm.lv=OS/usr audit=1 audit\_backlog\_limit=8129 transparent\_hugepages=never”  
//////////////////

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 17, 2024, 10:07pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/2 "2024-12-17T22:07:50Z")

</div>

Hi,  
I’d probably use the lineinfile module with a regexp.

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 17, 2024, 10:33pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/3 "2024-12-17T22:33:51Z")

</div>

* * *

- name: Disable the transparent hugepages for RHEL9 servers  
hosts: all  
become: true

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 17, 2024, 10:35pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/4 "2024-12-17T22:35:26Z")

</div>

And the code is running, but no update done for the grub file.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 17, 2024, 11:28pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/5 "2024-12-17T23:28:55Z")

</div>

Sorry, regexp is completely wrong,  
this: ^ means beginning of the line, you don’t have any line beginning with audit\_backlog\_limit…

try this

```auto
        regexp: "^(.+audit_backlog_limit=8129)"
        line: \1 transparent_hugepages=never"

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 18, 2024, 11:08am UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/6 "2024-12-18T11:08:51Z")

</div>

Since `/etc/default/grub` is a ini / conf file I’d suggest using the [community.general.ini\_file module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) to edit it.

Note that the two Ansible ini file filters can’t be used since they [don’t support ini files without section headers](https://github.com/ansible-collections/community.general/issues/7889), however the [community.general.jc filter](https://docs.ansible.com/ansible/latest/collections/community/general/jc_filter.html) with the [ini parser](https://kellyjonbrazil.github.io/jc/docs/parsers/ini) can be used to read ini files without section headers.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 18, 2024, 11:21am UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/7 "2024-12-18T11:21:32Z")

</div>

As far as I understand that fits only if GRUB\_CMDLINE\_LINE value post installation is the same for every machine, but since you could want to install a particular system in a different way resulting in different GRUB\_CMD\_LINE value, the risk is that you override system specific settings.  
Unless there is a way (that tbh I can’t see) with the module you mention to append the string transparent\_hugepages=never to the option, instead of overwriting the whole value.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 18, 2024, 11:23am UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/8 "2024-12-18T11:23:34Z")

</div>

Read the existing value, append what is needed and then write the change is an approach that should work, something like this?

```yaml
- name: Slurp /etc/default/grub
  ansible.builtin.slurp:
    src: /etc/default/grub
  register: grub_b64encoded

- name: Decode the base64 encoded version of /etc/default/grub and set a variable
  ansible.builtin.set_fact:
    grub_contents: "{{ grub_b64encoded['content'] | b64decode | community.general.jc('ini') }}"

- name: Debug grub_contents.GRUB_CMDLINE_LINUX
  ansible.builtin.debug:
    var: grub_contents.GRUB_CMDLINE_LINUX

- name: Update /etc/default/grub
  community.general.ini_file:
    path: /etc/default/grub
    option: GRUB_CMDLINE_LINUX
    value: "{{ grub_contents.GRUB_CMDLINE_LINUX }} transparent_hugepages=never"
    mode: "0644"
    backup: true
  when: ( "transparent_hugepages=never" not in grub_contents.GRUB_CMDLINE_LINUX | split(' ') )

```

The above could be improved / made more flexible by doing things like using a list of value pairs to add if not present rather than hard coding the values needed.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 18, 2024, 11:29am UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/9 "2024-12-18T11:29:36Z")

</div>

Well yes, that would involve also checking if the value\_to\_append is already present.  
If there is a module that solves a problem with just one task, I go for it.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 18, 2024, 11:47am UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/10 "2024-12-18T11:47:12Z")

</div>

Sounds like a bit complicated when you can just

```auto
    - name: grub
      ansible.builtin.lineinfile:
        path: /etc/default/grub
        regexp: ^(.+audit_backlog_limit=8129)"$
        line: '\1 transparent_hugepages=never"'
        backrefs: true
        state: present

```

But I appreciate we can have slightly different approaches to achieve the same result. I just tend to simplify if I can.

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 4:30pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/11 "2024-12-18T16:30:12Z")

</div>

Thanks you guys reply, I will try the solution you guys suggested and update you.

What I try to implement is we have some special RHEL9 servers need to disable Transparent Hugepages due to the application requirement.

I try to write an ansible playbook to implement this so others if they want to configure it, just run the playbook and get the thing done. There will be the cli to run after the /etc/default/grub updated.

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 4:31pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/12 "2024-12-18T16:31:31Z")

</div>

Yes, I agree with you.  
As operation team, the simpler, the better.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 18, 2024, 4:34pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/13 "2024-12-18T16:34:09Z")

</div>

My suggestion will work if the existing `GRUB_CMDLINE_LINE` has items in a different order, it doesn’t depend on `audit_backlog_limit=8129` appearing at the end of the line, this might, or might not matter to you 🤷 (it doesn’t matter to Grub!).

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 5:09pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/14 "2024-12-18T17:09:48Z")

</div>

@chris Yes, agree with your comments.

Just try to do as simpler as possible.

@matteoclc BTW, the code you suggested need to be updated; otherwise, after we run the code, the output will be: 1 transparent\_hugepages=never" instead of what I want: GRUB\_CMDLINE\_LINUX=“nofb quiet splash=quiet crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/OS-swap rd.lvm.lv=OS/root rd.lvm.lv=OS/swap rd.lvm.lv=OS/usr audit=1 audit\_backlog\_limit=8192 transparent\_bugepage=never”.

---

<div class="post-metadata">

**Author:** ![matteoclc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matteoclc/32/7020_2.png) [@matteoclc](https://forum.ansible.com/u/matteoclc)\
**Post date:** [December 18, 2024, 6:34pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/15 "2024-12-18T18:34:13Z")

</div>

Strange, doesn’t happen on my system, try omitting the quotes:

````auto
    - name: grub
      ansible.builtin.lineinfile:
        path: /etc/default/grub
        regexp: ^(.+audit_backlog_limit=8129)"$
        line: \1 transparent_hugepages=never"
        backrefs: true
        state: present```
````

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 7:19pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/16 "2024-12-18T19:19:27Z")

</div>

@matteoclc It works now. it is the quotes annoyed the playbook.  
Thanks a lot for the help!!!

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 7:22pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/17 "2024-12-18T19:22:15Z")

</div>

@chris I do not have the community.general.ini\_file loaded in my ansible server, will see if I have the time to load the module and test your solution.  
Thanks for your guide

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 18, 2024, 7:25pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/18 "2024-12-18T19:25:29Z")

</div>

You might have a old version of the [community.general collection](https://galaxy.ansible.com/ui/repo/published/community/general/) that doesn’t have it?

You would also need to have [jc](https://pypi.org/project/jc/) installed on the Ansible controller to use the [community.general.jc filter](https://docs.ansible.com/ansible/latest/collections/community/general/jc_filter.html).

---

<div class="post-metadata">

**Author:** ![yx84](https://avatars.discourse-cdn.com/v4/letter/y/2bfe46/32.png) [@yx84](https://forum.ansible.com/u/yx84)\
**Post date:** [December 18, 2024, 7:58pm UTC](https://forum.ansible.com/t/expend-the-text-in-the-end-of-a-special-line-in-a-file/39219/19 "2024-12-18T19:58:42Z")

</div>

@chris Thanks a lot! Will check it out.
