title: How to insert a block of lines in my httpd.conf using Ansible?
body: <p>I am trying to add a block of lines in my <code>httpd.conf</code> file.</p>
  
  <p>Here is how my httpd.conf file looks like before </p>
  
  <pre><code>NameVirtualHost *:80
  &lt;VirtualHost *:80&gt;
          Header edit* Location ^http://* https://
  
          AllowEncodedSlashes On
          #Properly process encoded slash character for Dispatchers
          AllowEncodedSlashes NoDecode
  
          &lt;Directory /&gt;
                  &lt;IfModule disp_apache2.c&gt;
                          ModMimeUsePathInfo On
                          SetHandler dispatcher-handler
                  &lt;/IfModule&gt;
  
                  Options FollowSymLinks
                  AllowOverride None
  
                  # Insert filter
                  SetOutputFilter DEFLATE
  
                  # Don't compress images
                  SetEnvIfNoCase Request_URI \
                  \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  
                  # Make sure proxies don't deliver the wrong content
                  Header append Vary User-Agent env=!dont-vary
                  # BIGBEAR-1328 prevent clickjacking
                  Header always append X-Frame-Options SAMEORIGIN
                  SetEnvIf X-Forwarded-Proto "https" HTTPS=on
                  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
  
          &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>This is how i expect my final file to look like</p>
  
  <p>After Execution:</p>
  
  <pre><code>NameVirtualHost *:80
  &lt;VirtualHost *:80&gt;
          RewriteEngine on
          RewriteCond %{HTTP:X-Forwarded-Proto} !https
          RewriteCond %{REQUEST_URI} !^/dispatcher/invalidate.cache
          RewriteRule !/eagle/check https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
          LogLevel info rewrite:info
          Header edit* Location ^http://* https://
  
          AllowEncodedSlashes On
          #Properly process encoded slash character for Dispatchers
          AllowEncodedSlashes NoDecode
  
          &lt;Directory /&gt;
                  &lt;IfModule disp_apache2.c&gt;
                          ModMimeUsePathInfo On
                          SetHandler dispatcher-handler
                  &lt;/IfModule&gt;
  
                  Options FollowSymLinks
                  AllowOverride None
  
                  # Insert filter
                  SetOutputFilter DEFLATE
  
                  # Don't compress images
                  SetEnvIfNoCase Request_URI \
                  \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  
                  # Make sure proxies don't deliver the wrong content
                  Header append Vary User-Agent env=!dont-vary
                  # BIGBEAR-1328 prevent clickjacking
                  Header always append X-Frame-Options SAMEORIGIN
                  SetEnvIf X-Forwarded-Proto "https" HTTPS=on
                  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
  
          &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>Here is the sample playbook that i used.</p>
  
  <pre><code>- name: Add section in the conf file
    blockinfile:
      path: /tmp/apache-conf.txt
      block: |
        RewriteEngine on
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteCond %{REQUEST_URI} !^/dispatcher/invalidate.cache
        RewriteRule !/eagle/check https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
        LogLevel info rewrite:info
        insertafter: &lt;VirtualHost *:80&gt;
    tags: test
  </code></pre>
  
  <p>But when i run my playbook, the file is not modified as expected.</p>
  
  <p>Here is what happens to the file. Blocks are added after the closing brackets of virtual hosts.</p>
  
  <p>New File after playbook execution</p>
  
  <pre><code>NameVirtualHost *:80
  &lt;VirtualHost *:80&gt;
          Header edit* Location ^http://* https://
  
          AllowEncodedSlashes On
          #Properly process encoded slash character for Dispatchers
          AllowEncodedSlashes NoDecode
  
          &lt;Directory /&gt;
                  &lt;IfModule disp_apache2.c&gt;
                          ModMimeUsePathInfo On
                          SetHandler dispatcher-handler
                  &lt;/IfModule&gt;
  
                  Options FollowSymLinks
                  AllowOverride None
  
                  # Insert filter
                  SetOutputFilter DEFLATE
  
                  # Don't compress images
                  SetEnvIfNoCase Request_URI \
                  \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  
                  # Make sure proxies don't deliver the wrong content
                  Header append Vary User-Agent env=!dont-vary
                  # BIGBEAR-1328 prevent clickjacking
                  Header always append X-Frame-Options SAMEORIGIN
                  SetEnvIf X-Forwarded-Proto "https" HTTPS=on
                  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
  
          &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  # BEGIN ANSIBLE MANAGED BLOCK
  RewriteEngine on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteCond %{REQUEST_URI} !^/dispatcher/invalidate.cache
  RewriteRule !/eagle/check https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
  LogLevel info rewrite:info
  insertafter: &lt;VirtualHost *:80&gt;
  # END ANSIBLE MANAGED BLOCK
  </code></pre>
  
  <p>Now how do I use the regex correctly to accomplish what I want, i.e. to insert the lines immediately after <code>&lt;VirtualHost *:80&gt;</code> and a tab appended before each line of the block.</p>