title: Append a string at the end of regex matched line using Ansible
body: <p>Wherever I find the tar command in a file I wish to append <code>2&gt;/dev/null</code> at the end of the same line. </p>
  
  <p>Thus, if  /tmp/test.sh file has <code>tar -cf test.tar /myfolder</code> entry then it should get changed to <code>tar -cf test.ta /myfolder  2&gt;/dev/null</code></p>
  
  <p>Below is my playbook for the same:</p>
  
  <pre><code> - name: Suppress tar errors
       replace:
         path: "/tmp/test.sh"
         regexp: 'tar *'
         replace: '\1 &gt;2/dev/null'
  </code></pre>
  
  <p>However i get the below error:</p>
  
  <pre><code>TASK [Suppress tar errors] **********************************************************************************************************************************************
  An exception occurred during task execution. To see the full traceback, use -vvv. The error was: sre_constants.error: invalid group reference
  fatal: [10.9.9.126]: FAILED! =&gt; {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 102, in &lt;module&gt;\n  File \"&lt;stdin&gt;\", line 94, in _ansiballz_main\n  File \"&lt;stdin&gt;\", line 40, in invoke_module\n  File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\n    fname, loader, pkg_name)\n  File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\n    mod_name, mod_fname, mod_loader, pkg_name)\n  File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\n    exec code in run_globals\n  File \"/tmp/ansible_replace_payload_FUlN7y/ansible_replace_payload.zip/ansible/modules/files/replace.py\", line 302, in &lt;module&gt;\n  File \"/tmp/ansible_replace_payload_FUlN7y/ansible_replace_payload.zip/ansible/modules/files/replace.py\", line 272, in main\n  File \"/usr/lib64/python2.7/re.py\", line 162, in subn\n    return _compile(pattern, flags).subn(repl, string, count)\n  File \"/usr/lib64/python2.7/re.py\", line 275, in filter\n    return sre_parse.expand_template(template, match)\n  File \"/usr/lib64/python2.7/sre_parse.py\", line 802, in expand_template\n    raise error, \"invalid group reference\"\nsre_constants.error: invalid group reference\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
  </code></pre>
  
  <p>I even tried the below:</p>
  
  <pre><code>   replace: '\g\1 &gt;2/dev/null'
     replace: '\g&lt;1&gt; &gt;2/dev/null'
     replace: '\1 &gt;2/dev/null'
  </code></pre>
  
  <p>But none of them helped. </p>