# blockinfile help

**URL:** <https://forum.ansible.com/t/blockinfile-help/38559>\
**Category:** Ansible Project\
**Created:** [October 31, 2023, 10:12pm UTC](https://forum.ansible.com/t/blockinfile-help/38559 "2023-10-31T22:12:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [October 31, 2023, 10:12pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/1 "2023-10-31T22:12:15Z")

</div>

Hello,  
I am trying to update nginx file, I need to add 4 more lines before the ; I tried blockinfile, lineinfile, replace and sed but I am unable to get the required output  
ex: the below one is some part of my nginx file,  
log\_format main '$remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

I need to add **'$request\_time ’ '$upstream\_response\_time ’ ‘$upstream\_connect\_time’ ‘$upstream\_header\_time’ to the existing line before the ;**  
**blockinfile is somewhat worked but the nginx file end up having ; after** ‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’  
my desired output is  
log\_format main '$remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’ **'$request\_time ’ '$upstream\_response\_time ’ ‘$upstream\_connect\_time’ ‘$upstream\_header\_time’** ;

someone please help me with a solution, I have lot of servers across multiple environments which cannot be done manually.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 1, 2023, 3:35am UTC](https://forum.ansible.com/t/blockinfile-help/38559/2 "2023-11-01T03:35:15Z")

</div>

Something like this, perhaps:

```auto
    - name: Do the a.b.replace thing
      ansible.builtin.replace:
        path: narmada-0.txt
        regexp: '^log_format +main +[^;]+;\n'
        replace: 'log_format main {{ bits | map("quote") | join("\n ") }};\n'
      vars:
        bits:
          - '$remote_addr - $remote_user [$time_local] "$request" '
          - '$status $body_bytes_sent "$http_referer" '
          - '"$http_user_agent" "$http_x_forwarded_for"'
          - '$request_time '
          - '$upstream_response_time '
          - '$upstream_connect_time'
          - '$upstream_header_time'

```

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [November 1, 2023, 6:02pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/3 "2023-11-01T18:02:27Z")

</div>

Thankyou, I tried the above playbook, there is no syntax error but it is not changing anything.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 1, 2023, 6:20pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/4 "2023-11-01T18:20:18Z")

</div>

Then, um, fix what’s wrong. (?) I’d be more specific, but you didn’t give me much to work with. Post the task and relevant variables.  
Note the “path:” I used on my localhost isn’t what you need.  
Also, I didn’t add the those extra data you want to add:

> **'$request\_time ’ '$upstream\_response\_time ’ ‘$upstream\_connect\_time’ ‘$upstream\_header\_time’**

it should be obvious where that would go, but if not, post with some details - tasks, variables, and output - and we’ll help you get it working.  
One last thing if you aren’t already doing it: use “-D” or “–diff” on the command line to see the changes.

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [November 1, 2023, 6:22pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/5 "2023-11-01T18:22:19Z")

</div>

yes, I modified the playbook to my data accordingly …

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [November 1, 2023, 6:27pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/6 "2023-11-01T18:27:43Z")

</div>

- name: replace log\_format settings on nginx  
ansible.builtin.replace:  
path: /nginx/nginx.conf  
regexp: ‘^log\_format +main +[^;]+;\n’  
replace: ‘log\_format main {{ bits | map(“quote”) | join("\n ") }};\n’  
vars:  
bits:
- '$remote\_addr - $remote\_user [$time\_local] “$request” ’
- '$status $body\_bytes\_sent “$http\_referer” ’
- ‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’
- '$request\_time ’
- '$upstream\_response\_time ’
- ‘$upstream\_connect\_time’
- ‘$upstream\_header\_time’

ansible-playbook -i inventory nginxupdate.yml

PLAY [update nginx.config] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [replace log\_format settings on nginx] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [servername]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
servername :ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 1, 2023, 6:38pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/7 "2023-11-01T18:38:18Z")

</div>

Add “-vv” and “–diff” to your command line.

Can you show us the relevant portion of the file /nginx/nginx.conf so we can try to reproduce? (And you really have that in the root? I’d expect /etc or /opt or /opt/local before /nginx, but, whatever.)

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [November 1, 2023, 6:41pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/8 "2023-11-01T18:41:23Z")

</div>

yes the path is /etc/nginx/nginx.conf, in my earlier email I removed that.  
this is the portion from the existing nginx file  
http {  
log\_format main '$remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

access\_log /var/log/nginx/access.log main;

}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 1, 2023, 6:56pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/9 "2023-11-01T18:56:54Z")

</div>

There’s the problem, and also why it’s so important to include all the relevant details up front.

The regexp starts with “^log\_format”, i.e. it’s expecting the ‘l’ of “log” to be in the first column, but you’ve got leading white space before “log\_format”. Change the regexp line thus:

```auto
          regexp: '^\s*log_format +main +[^;]+;\n'

```

‘\s\*’ is zero or more white space characters.

You’ll probably want to add some spaces or tabs to the front of the “replace:” string too.

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [November 1, 2023, 10:11pm UTC](https://forum.ansible.com/t/blockinfile-help/38559/10 "2023-11-01T22:11:33Z")

</div>

oh man, this is awesome it worked like a charm, you saved lot of my time

log\_format main '$remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’  
'$request\_time ’  
'$upstream\_response\_time ’  
‘$upstream\_connect\_time’  
‘$upstream\_header\_time’;
