I’m trying to use Ansible’s lineinfile module to add a new parameter to an existing log_format directive in an nginx configuration file. However, I’m encountering difficulties in getting the module to work as expected.
Here’s my nginx configuration file excerpt:
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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
I want it to change it to:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
And here’s the Ansible task I’m using:
- name: Add string between groups of regex in nginx.conf
ansible.builtin.lineinfile:
path: nginx.conf
backrefs: yes
regexp: '(log_format[^;]*?)(;)'
line: '1 my_string 2'
Despite trying different regex patterns and configurations, the file remains unchanged, and Ansible simply returns “ok” without making any changes. I’ve validated the regex using online tools like regex101, and it appears to match the desired portion of the file accurately.
PS: I have tried other regex too, but none of them seem to work. Eg:
(log_formats*S*;)
(log_formats*S*;)
Jatin Goyal I is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.