which is overwriting the production file which implies copy might not be the best solution. Can patch the file without regex gymnastics or do I have to resort to lineinfile module?
- 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
There are several great ways to read, write and modify INI files using Ansible, I linked to them in this comment, .ini / .conf / .env file lookup, fliters and modules:
@chris
Thanks for updating, I first thought about treating it as an ini file, but I’m a bit obsessed with a solution that doesn’t use community.general as much as possible