<p>Your <em>regexp</em> can't work because greedy <code>.+</code> in front of the lookahead assertion will consume the line till the very end. The next problem is that you require at least one space at the beginning of the line <code>\s+</code>.</p>
<p>Try this</p>
<pre><code>        regexp: '^(?!.* pci=noaer.*)(\s*kernel /vmlinuz.*)$'
        replace: '\1 pci=noaer'
</code></pre>
<p>It's important to understand that <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html#parameter-regexp" rel="nofollow noreferrer"><em>regexp</em></a> in Ansible module <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html#ansible-builtin-replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression" rel="nofollow noreferrer"><em>replace</em></a> uses MULTILINE and does not use DOTALL. Quoting:</p>
<blockquote>
<p>Uses MULTILINE mode, which means ^ and $ match the beginning and end of the file, as well as the beginning and end respectively of each line of the file.
Does not use DOTALL, which means the . special character matches any character except newlines.</p>
</blockquote>
<hr>
<sup>
<p>If it does not work below is a brute force solution. Given the testing file</p>
<pre class="lang-sh prettyprint-override"><code>shell&gt; cat grub.cfg
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3
</code></pre>
<p>get the lines</p>
<pre class="lang-yaml prettyprint-override"><code>    - command: cat grub.cfg
      register: grub
      check_mode: false
</code></pre>
<p>For simplicity, I used <em>command</em> but you might want to use <em>fetch</em>, <em>slurp</em>, or any other suitable module instead.</p>
<p>Test it. In debug, iterate the matched lines, split the properties of the line, and find the missing properties</p>
<pre class="lang-yaml prettyprint-override"><code>    - debug:
        msg:
          - &quot;{{ item }}&quot;
          - &quot;{{ missing }}&quot;
      loop: &quot;{{ grub.stdout_lines|select('match', match) }}&quot;
      loop_control:
        label: &quot;{{ line.1 }}&quot;
      when: missing|length &gt; 0
      vars:
        match: '^\s*kernel /vmlinuz.+$'
        line: &quot;{{ item.split() }}&quot;
        missing: &quot;{{ props|difference(line) }}&quot;
        props:
          - pci=noaer
</code></pre>
<p>gives</p>
<pre class="lang-yaml prettyprint-override"><code>ok: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64) =&gt; 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
  - - pci=noaer
ok: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64) =&gt; 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
  - - pci=noaer
ok: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64) =&gt; 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
  - - pci=noaer
ok: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64) =&gt; 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3
  - - pci=noaer
</code></pre>
<p>If the configuration seems to be working use the module <em>replace</em> in check mode</p>
<pre class="lang-yaml prettyprint-override"><code>    - replace:
        path: grub.cfg
        regexp: &quot;{{ item }}&quot;
        replace: &quot;{{ (line + missing)|join(' ') }}&quot;
      loop: &quot;{{ grub.stdout_lines|select('match', match) }}&quot;
      loop_control:
        label: &quot;{{ line.1 }}&quot;
      when: missing|length &gt; 0
      vars:
        match: '^\s*kernel /vmlinuz.+$'
        line: &quot;{{ item.split() }}&quot;
        missing: &quot;{{ props|difference(line) }}&quot;
        props:
          - pci=noaer
</code></pre>
<p>gives</p>
<pre class="lang-yaml prettyprint-override"><code>--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
-kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3 pci=noaer

changed: [localhost] =&gt; (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64)
</code></pre>
<p>Run the play if this is what you want.</p>
<hr>
<p>In case of grub2 you should modify <code>GRUB_CMDLINE_LINUX</code> or <code>GRUB_CMDLINE_LINUX_DEFAULT</code> in /etc/default/grub and notify handler <code>update-grub</code>. For example</p>
<pre class="lang-yaml prettyprint-override"><code>tasks:
  - name: Configure /etc/default/grub
    ansible.builtin.lineinfile:
      dest: /etc/default/grub
      regexp: '^\s*{{ item.var }}\s*=(.*)$'
      line: &quot;{{ item.var }}={{ item.value }}&quot;
      backup: true
    loop:
      - var: GRUB_CMDLINE_LINUX_DEFAULT
        value: &quot;quiet splash pci=noaer&quot;
    notify: update grub

handlers:
  - name: update grub
    ansible.builtin.command:
      cmd: &gt;
        /usr/sbin/update-grub
</code></pre>
<p>Fit the configuration data to your needs.</p>
</sup>

CC BY-SA 4.0
2021-08-03T22:20:33.200Z
I appreciate you taking the time to answer.  That might work if it were a grub2 system, and every system I was dealing with was expected to have the same kernel configuration parameters. But doesn't really apply in my case, unfortunately.  I'll try to keep this solution in mind for my grub2 systems, though, as it could have a good use there.
ahuie
CC BY-SA 4.0
2021-08-04T08:33:57.030Z
I've added a *regexp* solution.
Vladimir Botka
CC BY-SA 4.0
2021-08-09T15:32:03.370Z
Thank you. That solution appears to work perfectly!  And I appreciate the guidance on how to do testing and debugging.  That will definitely help going forward!
ahuie
CC BY-SA 4.0
2021-08-03T21:56:43.713Z
2021-08-04T09:15:12.947Z
2021-08-04T09:15:12.947Z
Vladimir Botka
<p>I'm trying to solve a problem where I want to add a setting 'pci=noaer' to all kernel definitions in /boot/grub/grub.conf, but only if the setting doesn't exist.  I've managed to get the basic functionality to work with the replace module, in that the module WILL add the setting to the relevant lines, but the problem is in NOT adding the setting to lines where it already exists. In other words, if I run the playbook once, it'll add the setting as needed, but every other run of the playbook after that adds another instance of the setting to each line.  The following is an example of the line I want to update and the regexp and replace lines I currently have configured to update it:</p>
<p>Line to change:<br />
<code>kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3</code></p>
<p>After the playbook runs the above line should look like:<br />
<code>kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3 pci=noaer</code></p>
<p>Relevant replace module parameters:</p>
<pre><code>regexp: '(^\s+kernel /vmlinuz.+(?!pci=noaer).*$)'  
replace: '\1 pci=noaer'
</code></pre>
<p>I've tried using the \b sequences to clearly define the boundaries of pci=noaer (e.g. (?!\bpci=noaer\b)), but with no luck.  I've tested this a bit on regex101 but haven't really been able to definitively understand why the negative lookahead isn't doing what it's supposed to be doing (looking for cases where the pci=noaer string isn't there) - or how I'm using it within the replace module. I've had similar issues with negative lookaheads/lookbehinds in lineinfile, and I'm perfectly willing to admit this is total user error, given that I'm fairly new to ansible and python, and my regex skills are fairly limited thus far.  But I've searched and searched and searched and I can't seem to find any good indication of why the negative lookaround sequences don't work the way I expect them to within these modules.  Could this be a case of not properly managing the greediness of my regex configuration?</p>
<p>For what it's worth, I have seen that lookahead/lookbehind might have issues in some of the 2.x versions of Ansible, so perhaps this is a version problem?  I'm running Ansible 2.6.20 with an underlying Python version of 2.6.6. If this is indeed an issue with my version, is there a way around it without having to update to something more recent?</p>
<p>A bit of additional information: This is being done across multiple CentOS 6-based systems where I don't feel like I can necessarily expect the kernel lines in the grub.conf file to be the same from one system to the next (perhaps they should be, but that's a topic for a different discussion).  That's why the fairly non-specific regex. Also, since it's CentOS 6, there is no equivalent of update-grub or grub2-mkconfig for that OS. It's just a matter of editing the /etc/grub.conf or /boot/grub/grub.conf file and rebooting.</p>

CC BY-SA 4.0
2021-08-04T18:42:57.033Z
Could you not add a pre-task/task to perform an egrep for that kernel param and then use the return code in a "when" statement for the actual replace? That way it will only add it when it doesn't find an instance of it in the grub.conf
Brett Levene
CC BY-SA 4.0
2021-08-09T15:47:17.277Z
@BrettLevene Sure, that could work, but it kind of undermines the whole point of using Ansible for things that Ansible should be able to do, does it not?  Yes, there are cases where Ansible can't do what you want it to and you have to resort to external scripts and system calls to get the job done. But in this case the replace and lineinfile modules should be able to do this natively, so I feel obligated to do the work to learn how to use them properly.
ahuie
<ansible><linux><automation><python>
How to add a string to a line when it doesn't exist?
ahuie
# How to add a string to a line when it doesn't exist?

(Asked by: username_0 on 2021-08-03)

I'm trying to solve a problem where I want to add a setting 'pci=noaer' to all kernel definitions in /boot/grub/grub.conf, but only if the setting doesn't exist.  I've managed to get the basic functionality to work with the replace module, in that the module WILL add the setting to the relevant lines, but the problem is in NOT adding the setting to lines where it already exists. In other words, if I run the playbook once, it'll add the setting as needed, but every other run of the playbook after that adds another instance of the setting to each line.  The following is an example of the line I want to update and the regexp and replace lines I currently have configured to update it:
Line to change:
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
After the playbook runs the above line should look like:
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3 pci=noaer
Relevant replace module parameters:
regexp: '(^\s+kernel /vmlinuz.+(?!pci=noaer).*$)'  
replace: '\1 pci=noaer'

I've tried using the \b sequences to clearly define the boundaries of pci=noaer (e.g. (?!\bpci=noaer\b)), but with no luck.  I've tested this a bit on regex101 but haven't really been able to definitively understand why the negative lookahead isn't doing what it's supposed to be doing (looking for cases where the pci=noaer string isn't there) - or how I'm using it within the replace module. I've had similar issues with negative lookaheads/lookbehinds in lineinfile, and I'm perfectly willing to admit this is total user error, given that I'm fairly new to ansible and python, and my regex skills are fairly limited thus far.  But I've searched and searched and searched and I can't seem to find any good indication of why the negative lookaround sequences don't work the way I expect them to within these modules.  Could this be a case of not properly managing the greediness of my regex configuration?
For what it's worth, I have seen that lookahead/lookbehind might have issues in some of the 2.x versions of Ansible, so perhaps this is a version problem?  I'm running Ansible 2.6.20 with an underlying Python version of 2.6.6. If this is indeed an issue with my version, is there a way around it without having to update to something more recent?
A bit of additional information: This is being done across multiple CentOS 6-based systems where I don't feel like I can necessarily expect the kernel lines in the grub.conf file to be the same from one system to the next (perhaps they should be, but that's a topic for a different discussion).  That's why the fairly non-specific regex. Also, since it's CentOS 6, there is no equivalent of update-grub or grub2-mkconfig for that OS. It's just a matter of editing the /etc/grub.conf or /boot/grub/grub.conf file and rebooting.

**Tags:** ansible, linux, automation, python

> **Comments:**
> **username_1**: Could you not add a pre-task/task to perform an egrep for that kernel param and then use the return code in a "when" statement for the actual replace? That way it will only add it when it doesn't find an instance of it in the grub.conf  
> **username_0**: @BrettLevene Sure, that could work, but it kind of undermines the whole point of using Ansible for things that Ansible should be able to do, does it not?  Yes, there are cases where Ansible can't do what you want it to and you have to resort to external scripts and system calls to get the job done. But in this case the replace and lineinfile modules should be able to do this natively, so I feel obligated to do the work to learn how to use them properly.  

---

## 1 answers

### Answer by username_2 on 2021-08-03. Score: 1 | Accepted answer

Your regexp can't work because greedy .+ in front of the lookahead assertion will consume the line till the very end. The next problem is that you require at least one space at the beginning of the line \s+.
Try this
        regexp: '^(?!.* pci=noaer.*)(\s*kernel /vmlinuz.*)$'
        replace: '\1 pci=noaer'

It's important to understand that regexp in Ansible module replace uses MULTILINE and does not use DOTALL. Quoting:

Uses MULTILINE mode, which means ^ and $ match the beginning and end of the file, as well as the beginning and end respectively of each line of the file.
Does not use DOTALL, which means the . special character matches any character except newlines.



If it does not work below is a brute force solution. Given the testing file
shell> cat grub.cfg
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

get the lines
    - command: cat grub.cfg
      register: grub
      check_mode: false

For simplicity, I used command but you might want to use fetch, slurp, or any other suitable module instead.
Test it. In debug, iterate the matched lines, split the properties of the line, and find the missing properties
    - debug:
        msg:
          - "{{ item }}"
          - "{{ missing }}"
      loop: "{{ grub.stdout_lines|select('match', match) }}"
      loop_control:
        label: "{{ line.1 }}"
      when: missing|length > 0
      vars:
        match: '^\s*kernel /vmlinuz.+$'
        line: "{{ item.split() }}"
        missing: "{{ props|difference(line) }}"
        props:
          - pci=noaer

gives
ok: [localhost] => (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64) => 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
  - - pci=noaer
ok: [localhost] => (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64) => 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
  - - pci=noaer
ok: [localhost] => (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64) => 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
  - - pci=noaer
ok: [localhost] => (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64) => 
  msg:
  - kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3
  - - pci=noaer

If the configuration seems to be working use the module replace in check mode
    - replace:
        path: grub.cfg
        regexp: "{{ item }}"
        replace: "{{ (line + missing)|join(' ') }}"
      loop: "{{ grub.stdout_lines|select('match', match) }}"
      loop_control:
        label: "{{ line.1 }}"
      when: missing|length > 0
      vars:
        match: '^\s*kernel /vmlinuz.+$'
        line: "{{ item.split() }}"
        missing: "{{ props|difference(line) }}"
        props:
          - pci=noaer

gives
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
-kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] => (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] => (item=/vmlinuz-2.6.32-279.22.1.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3 pci=noaer
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3

changed: [localhost] => (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64)
--- before: grub.cfg
+++ after: grub.cfg
@@ -1,4 +1,4 @@
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro recovery root=/dev/sda3
 kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro root=/dev/sda3
-kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3
+kernel /vmlinuz-2.6.32-279.22.2.el6.x86_64 ro recovery root=/dev/sda3 pci=noaer

changed: [localhost] => (item=/vmlinuz-2.6.32-279.22.2.el6.x86_64)

Run the play if this is what you want.

In case of grub2 you should modify GRUB_CMDLINE_LINUX or GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub and notify handler update-grub. For example
tasks:
  - name: Configure /etc/default/grub
    ansible.builtin.lineinfile:
      dest: /etc/default/grub
      regexp: '^\s*{{ item.var }}\s*=(.*)$'
      line: "{{ item.var }}={{ item.value }}"
      backup: true
    loop:
      - var: GRUB_CMDLINE_LINUX_DEFAULT
        value: "quiet splash pci=noaer"
    notify: update grub

handlers:
  - name: update grub
    ansible.builtin.command:
      cmd: >
        /usr/sbin/update-grub

Fit the configuration data to your needs.

> **Comments:**
> **username_0**: I appreciate you taking the time to answer.  That might work if it were a grub2 system, and every system I was dealing with was expected to have the same kernel configuration parameters. But doesn't really apply in my case, unfortunately.  I'll try to keep this solution in mind for my grub2 systems, though, as it could have a good use there.  
> **username_2**: I've added a *regexp* solution.  
> **username_0**: Thank you. That solution appears to work perfectly!  And I appreciate the guidance on how to do testing and debugging.  That will definitely help going forward!  

---

