``` hello ansible experts, I am creating this simple playbook roles where I am . . .

Parth Pareek:

hello ansible experts,

I am creating this simple playbook roles where I am defining some key-value pair variable in loop and using it in regex to find and replace the variable values in text file but ended up with getting error because of item.key I am using in regex. Can you please help me out here ?

- name: 'replacing existing env'
  become: yes
  become_user: root
  loop:
    - key: 'secret_env='
      value: 'secret_env=test'
    - key: 'kube_env='
      value: 'kube_env=testing'
    - key: 'NEW_ENV='
      value: 'NEW_ENV=ADDING'
  replace:
    path: /etc/foo/.env
    regexp: ".*{{ item.key }}(.*)"
    present: yes
    replace: "{{ item.value }}"
- name: 'adding env'
  lineinfile:
    path: /etc/foo/.env
    line: '{{ item.value }}'
  register: cat
  tags: env_execute

- debug: var=cat.stdout_lines
  tags: env_execute

Al West:
Remove the present lines in your playbook. It is isn’t valid. Also in your adding env task you don’t pass any items.

Parth Pareek:
it is working fine in adding env… it is only showing error in regex where I have to pass the loop variables

Al West:
Please refer to:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html
present is not a valid token. I ran modified version of you code and it worked.

Al West:

- name: "Regex"
  hosts: localhost
  connection: local
  tasks:
  - name: 'replacing existing env'
    become: yes
    become_user: root
    loop:
      - key: 'secret_env='
        value: 'secret_env=test'
      - key: 'kube_env='
        value: 'kube_env=testing'
      - key: 'NEW_ENV='
        value: 'NEW_ENV=ADDING'
    replace:
      path: /etc/foo/.env
      regexp: ".*{{ item.key }}(.*)"
      replace: "{{ item.value }}"

  - name: 'print env'
    debug:
      msg: ".env is {{lookup('file', '/etc/foo/.env') }}"

Parth Pareek:
I am still getting this error after removing Present.
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/etc/ansible/roles/update_env/tasks/main.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n replace: \"{{ item.value }}\"\n- name: 'adding env'\n ^ here\n"}

Parth Pareek:
@Al West can you check here it is showing this error in name: ‘adding env’ where I am using item.value

Al West:
I didn’t get an error. Double check the code I pasted.

Parth Pareek:
it is the another half of the code which you did not pasted

Parth Pareek:

name: 'adding env'
  lineinfile:
    path: /etc/foo/.env
    line: '{{ item.value }}'

here I am getting an error

Parth Pareek:
it says The error was: 'item' is undefined\

Al West:
Yes the item is not defined

Al West:
you can’t reuse items between tasks

Parth Pareek:
@Al West how can I use the same loop for both the tasks ?

Al West:
And the dictionary to a variable and call it in both tasks.

Parth Pareek:
@Al West it worked, thanks a lot for your help.