The only issue is that it won't work if the file is empty of there isn't line for the PATH already in it, in that case, it WON'T add the line.
I fixed it like this:
name: Checks if the environment file already has an entry for the PATH
replace:
dest=/etc/environment
regexp="PATH=(.*)"
replace="PATH=\1"
register: checkIfPATHIsHere
name: Add a PATH entry with {{extrapath}} if the PATH is not already defined
lineinfile:
dest=/etc/environment
state=present
line="PATH={{extrapath}}"
regexp=''
insertafter=EOF
when: checkIfPATHIsHere.changed == false
name: add {{extrapath}} to the PATH
lineinfile:
dest=/etc/environment
state=present
backrefs=no
regexp="PATH=(["])((?!.?{{extrapath}}).?)(["])$"
line="PATH=\1\2:{{extra_path}}\3"
when: checkIfPATHIsHere.changed
The only issue is that it won't work if the file is empty of there isn't line for the PATH already in it, in that case, it WON'T add the line.
I fixed it like this:
name: Checks if the environment file already has an entry for the PATH
replace:
dest=/etc/environment
regexp="PATH=(.*)"
replace="PATH=\1"
register: checkIfPATHIsHere
name: Add a PATH entry with {{extrapath}} if the PATH is not already defined
lineinfile:
dest=/etc/environment
state=present
line="PATH={{extrapath}}"
regexp=''
insertafter=EOF
when: checkIfPATHIsHere.changed == false
name: add {{extrapath}} to the PATH
lineinfile:
dest=/etc/environment
state=present
backrefs=no
regexp="PATH=(["])((?!.?{{extrapath}}).?)(["])$"
line="PATH=\1\2:{{extra_path}}\3"
when: checkIfPATHIsHere.changed