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:

          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.

On 11/1/23 2:41 PM, Narmada Karthika wrote:
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;

}

On Wed, Nov 1, 2023 at 11:38 AM Todd Lewis <uto...@gmail.com> wrote:

    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.)

    On 11/1/23 2:27 PM, Narmada Karthika wrote:
          - 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


    On Wed, Nov 1, 2023 at 11:22 AM Narmada Karthika
    <chittur...@gmail.com> wrote:

        yes, I modified the playbook to my data accordingly ..

        On Wed, Nov 1, 2023 at 11:20 AM Todd Lewis <uto...@gmail.com>
        wrote:

            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.


            On 11/1/23 2:02 PM, Narmada Karthika wrote:
            Thankyou, I tried the above playbook, there is no syntax
            error but it is not changing anything.

            On Tuesday, October 31, 2023 at 8:35:32 PM UTC-7 Todd
            Lewis wrote:

                Something like this, perhaps:

                     - 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'


                On 10/31/23 6:12 PM, Narmada Karthika wrote:
                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.

-- You received this message because you are
                subscribed to the Google Groups "Ansible Project"
                group.
                To unsubscribe from this group and stop receiving
                emails from it, send an email to
                ansible-proje...@googlegroups.com.
                To view this discussion on the web visit
                
https://groups.google.com/d/msgid/ansible-project/d00e73ae-965d-4a24-9843-8cc37b06010fn%40googlegroups.com
                
<https://groups.google.com/d/msgid/ansible-project/d00e73ae-965d-4a24-9843-8cc37b06010fn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- Todd

-- You received this message because you are subscribed to
            the Google Groups "Ansible Project" group.
            To unsubscribe from this group and stop receiving emails
            from it, send an email to
            ansible-project+unsubscr...@googlegroups.com.
            To view this discussion on the web visit
            
https://groups.google.com/d/msgid/ansible-project/e80cfd8b-6342-4f84-a0fe-08175e35d62dn%40googlegroups.com
            
<https://groups.google.com/d/msgid/ansible-project/e80cfd8b-6342-4f84-a0fe-08175e35d62dn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- Todd

-- You received this message because you are subscribed to
            the Google Groups "Ansible Project" group.
            To unsubscribe from this group and stop receiving emails
            from it, send an email to
            ansible-project+unsubscr...@googlegroups.com.
            To view this discussion on the web visit
            
https://groups.google.com/d/msgid/ansible-project/56f93cbd-82ae-44b8-b919-867867c122d4%40gmail.com
            
<https://groups.google.com/d/msgid/ansible-project/56f93cbd-82ae-44b8-b919-867867c122d4%40gmail.com?utm_medium=email&utm_source=footer>.



-- // Thanks,
        Narmada Karthika Chitturi
        //



-- // Thanks,
    Narmada Karthika Chitturi
    //
-- You received this message because you are subscribed to the
    Google Groups "Ansible Project" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to ansible-project+unsubscr...@googlegroups.com.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/ansible-project/CABdF8S3PGSgqhDoVgUxEc1Cs1iDqGj3i4k340%3D2WfivJ%3Dzyy6g%40mail.gmail.com
    
<https://groups.google.com/d/msgid/ansible-project/CABdF8S3PGSgqhDoVgUxEc1Cs1iDqGj3i4k340%3D2WfivJ%3Dzyy6g%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- Todd

-- You received this message because you are subscribed to the Google
    Groups "Ansible Project" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to ansible-project+unsubscr...@googlegroups.com.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/ansible-project/6bf06968-e2e5-4eb8-aec5-3da215984117%40gmail.com
    
<https://groups.google.com/d/msgid/ansible-project/6bf06968-e2e5-4eb8-aec5-3da215984117%40gmail.com?utm_medium=email&utm_source=footer>.



--
// Thanks,
Narmada Karthika Chitturi
//

--
Todd

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bf33f825-ca79-4dd2-8101-8722568f65b1%40gmail.com.

Reply via email to