Joined January 2017
·

jdevora

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={{extra
    path}}"
    regexp=''
    insertafter=EOF
    when: checkIfPATHIsHere.changed == false

  • name: add {{extrapath}} to the PATH
    lineinfile:
    dest=/etc/environment
    state=present
    backrefs=no
    regexp="PATH=(["])((?!.?{{extra
    path}}).?)(["])$"
    line="PATH=\1\2:{{extra_path}}\3"
    when: checkIfPATHIsHere.changed

Achievements
1 Karma
0 Total ProTip Views