# Lineinfile matches one regex but not another

**URL:** <https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [April 11, 2024, 8:09pm UTC](https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912 "2024-04-11T20:09:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwelliott28](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@jwelliott28](https://forum.ansible.com/u/jwelliott28)\
**Post date:** [April 11, 2024, 8:09pm UTC](https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912/1 "2024-04-11T20:09:27Z")

</div>

Working on creating a playbook to enforce CIS Benchmarks for Apache 2.4 on Red Hat Enterprise Linux 9. On control 5.1 it suggests adding “Options None” to the element. Config would look like this:

\<Directory /\>  
Options None  
Other Directives  
Etc  
\</Directory\>

In example 1 below, it functions as I anticipate how adding a line with lineinfile would go. The next control suggests adding the same directive “Options None” to the document root directory element, with this as the end result:

\<Directory “/var/www/html”\>  
Options None  
Other directive  
Etc  
\</Directory\>

Example 2 shows my code and I am not getting the result that I expected. The major difference between example 1 and 2 insertafter regex is the quotation marks around the directory in example 2. I have tried different regex patterns trying to match it but I’m not having any luck. Am I missing something? I suppose in the end I can replace \<Directory “/var/www/html”\> with \<Directory /var/www/html\>, but I’d like to know what is causing the hang up here.

- Version(s) of the relevant software tools, libraries, collections etc
  - Ansible Core: 2.14.5
  - Python 3.9
  - ansible.builtin.lineinfile

Example 1 - Working

```auto
  ansible.builtin.lineinfile:
    path: /etc/httpd/conf/httpd.conf
    line: " Options None"
    state: present
    insertafter: <Directory />

```

Example 2 - Not working

```auto
  ansible.builtin.lineinfile:
    path: /etc/httpd/conf/httpd.conf
    line: " Options None"
    state: present
    insertafter: <Directory "/var/www">

```

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 12, 2024, 5:30am UTC](https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912/2 "2024-04-12T05:30:45Z")

</div>

Hi, your regex in `insertafter` is correct, quotes can be used as quotes without any escaping. But there is a common misunderstanding about `ansible.builtin.lineinfile` module.

Simply, this module does nothing if the specified `line` already exists in the file. It does not matter _where_ the line is placed, even if `insertafter` is specified.  
The parameter `insertafter` is to indicate the place to insert line if no line is found.

Therefore, this module is not good choice for the tasks that insert the same line in multiple places.

I believe the line ` Options None` always exists in the file on your second task, since the exact same line is added by the first task, or other directives in the same file already has the same options. I believe this is why your second task does nothing.

I don’t know your requirements in detail, but I suggest you to use `ansible.builtin.copy` or `ansible.builtin.template` instead.

---

<div class="post-metadata">

**Author:** ![jwelliott28](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@jwelliott28](https://forum.ansible.com/u/jwelliott28)\
**Post date:** [April 12, 2024, 11:51am UTC](https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912/3 "2024-04-12T11:51:54Z")

</div>

100% agree. I continued to test yesterday after posting and just put in some gibberish for the fun of it and it appeared where I expected it to. I am thinking along the same lines that this task is better suited for something like a template as there are several lines where “Option None” in the configuration file is recommended by CIS. Appreciate the input.

---

<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:** [April 12, 2024, 12:17pm UTC](https://forum.ansible.com/t/lineinfile-matches-one-regex-but-not-another/4912/4 "2024-04-12T12:17:00Z")

</div>

Templating or copying Apache config files is the way to go — they are not valid SGML and I’m unaware of any parsers for them — I don’t think they can be generated any other way, however there is a danger that one can end up with [a complicate template…](https://git.coop/webarch/users/-/blob/master/templates/apache.conf.j2?ref_type=heads)

**EDIT:** there is an [23 year old CPAN parser](https://metacpan.org/release/NWIGER/Apache-ConfigFile-1.18/view/ConfigFile.pm) but it is not complete:

> Currently `LogFormat` and any other directive with embedded quotes, even if escaped, are not handled correctly… there is no configuration “inheritance”… the order of context blocks is not maintained…
