having issues with lineinfile insert

I keep getting errors when running an insert line task

the task to insert is

  • name: add line
    lineinfile:
    backup: no
    backrefs: yes
    state: present
    path: “{{ file_path }}”
    regexp: ‘^(\s*)[#]?{{ item.search }}(: )*’
    line: ‘\1{{ item.search }}\2\n\1{{ item.add }}’
    with_items:
  • { search: “{{ line.replace_with }}”, add: “{{ line.line_to_add }}” }

but I keep getting this error

File “/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py”, line 102, in
_ansiballz_main()
File “/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py”, line 94, in ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File “/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py”, line 40, in invoke_module
runpy.run_module(mod_name=‘ansible.modules.lineinfile’, init_globals=None, run_name=‘main’, alter_sys=True)
File “/usr/lib64/python2.7/runpy.py”, line 176, in run_module
fname, loader, pkg_name)
File “/usr/lib64/python2.7/runpy.py”, line 82, in run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File “/usr/lib64/python2.7/runpy.py”, line 72, in run_code
exec code in run_globals
File "/tmp/ansible_lineinfile_payload_D2ShX
/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 577, in
File "/tmp/ansible_lineinfile_payload_D2ShX
/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 568, in main
File "/tmp/ansible_lineinfile_payload_D2ShX
/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 342, in present
File “/usr/lib64/python2.7/re.py”, line 266, in _expand
return sre_parse.expand_template(template, match)
File “/usr/lib64/python2.7/sre_parse.py”, line 800, in expand_template
raise error, “unmatched group”
sre_constants.error: unmatched group
failed: [prod-soi-util-01w.activenetwork.com] (item={‘search’: ‘#image: jenkins/jenkins:2.346.1’, ‘add’: ‘image: jenkins/jenkins:2.346.2’}) => {
“ansible_loop_var”: “item”,
“changed”: false,
“item”: {
“add”: “image: jenkins/jenkins:2.346.2”,
“search”: “#image: jenkins/jenkins:2.346.1”
},
“module_stderr”: “OpenSSH_8.1p1, LibreSSL 2.7.3\r\ndebug1: Reading configuration data /Users/twong/.ssh/config\r\ndebug1: /Users/twong/.ssh/config line 1: Applying options for prod-soi-util-01w.activenetwork.com\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 47: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 23846\r\ndebug3: mux_client_request_session: session request sent\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\nShared connection to prod-soi-util-01w.activenetwork.com closed.\r\n”,
“module_stdout”: “Traceback (most recent call last):\r\n File "/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py", line 102, in \r\n _ansiballz_main()\r\n File "/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py", line 94, in ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File "/home/twong/.ansible/tmp/ansible-tmp-1658251625.2565749-23940-115530077073029/AnsiballZ_lineinfile.py", line 40, in invoke_module\r\n runpy.run_module(mod_name=‘ansible.modules.lineinfile’, init_globals=None, run_name=‘main’, alter_sys=True)\r\n File "/usr/lib64/python2.7/runpy.py", line 176, in run_module\r\n fname, loader, pkg_name)\r\n File "/usr/lib64/python2.7/runpy.py", line 82, in run_module_code\r\n mod_name, mod_fname, mod_loader, pkg_name)\r\n File "/usr/lib64/python2.7/runpy.py", line 72, in run_code\r\n exec code in run_globals\r\n File "/tmp/ansible_lineinfile_payload_D2ShX/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 577, in \r\n File "/tmp/ansible_lineinfile_payload_D2ShX/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 568, in main\r\n File "/tmp/ansible_lineinfile_payload_D2ShX/ansible_lineinfile_payload.zip/ansible/modules/lineinfile.py", line 342, in present\r\n File "/usr/lib64/python2.7/re.py", line 266, in _expand\r\n return sre_parse.expand_template(template, match)\r\n File "/usr/lib64/python2.7/sre_parse.py", line 800, in expand_template\r\n raise error, "unmatched group"\r\nsre_constants.error: unmatched group\r\n”,

^^^

You expect the groups in your regex to always match, but clearly that isn’t the case.