Initial Solution
- hosts: localhost
gather_facts: false
tasks:
- name: 'read each line of the new env var file'
ansible.builtin.set_fact:
new_env: "{{ lookup('ansible.builtin.file', '/tmp/artifacts/.env') | split }}"
- name: 'replace any lines that match the env vars of the new file'
ansible.builtin.lineinfile:
path: /home/user/project/.env
regexp: '^{{ service_name }}='
line: "{{ item }}"
check_mode: true
diff: true
vars:
service_name: "{{ item.split('=')[0] }}"
loop: "{{ new_env }}"
This works because I read the new env var file and split it based on =
character. I use the first instance and call it service_name
in a loop and tell lineinfile
to replace the instance with the updated value.
I am open to better solutions but thought I would keep this solution if there isn’t any