Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Last active December 3, 2019 19:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save carlessanagustin/af38e4585951dc32a320 to your computer and use it in GitHub Desktop.
Save carlessanagustin/af38e4585951dc32a320 to your computer and use it in GitHub Desktop.
updating PATH with ansible - system wide

Option 1

- name: compile sources
  shell:
    coffee -o lib -c src 
    chdir=${mysourcedir}
  environment:
    PATH: $PATH:/opt/node/bin

Option 2

- name: add {{extra_path}} to path
  lineinfile:
    dest: /etc/environment
    state: present
    backrefs: yes
    regexp: 'PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
    line: "PATH=\1\2:{{extra_path}}\3"
@ajweitz
Copy link

ajweitz commented Jun 12, 2019

How do you use it with an actual path?
I tried using it, and ansible has trouble processing all the \1\2 expressions

@hasufell
Copy link

Same as @ajweitz ...doesn't work:

ERROR! Syntax Error while loading YAML.
  found unknown escape character

The offending line appears to be:

        regexp: 'PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
        line: "PATH=\1\2:{{extra_path}}\3"
                    ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

@cristianorfaria
Copy link

Same as @ajweitz ...doesn't work:

ERROR! Syntax Error while loading YAML.
  found unknown escape character

The offending line appears to be:

        regexp: 'PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
        line: "PATH=\1\2:{{extra_path}}\3"
                    ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
  • name: add {{extra_path}} to path
    lineinfile:
    dest: /etc/environment
    state: present
    backrefs: yes
    regexp: 'PATH=(["])((?!.?{{extra_path}}).?)(["])$'
    line: "PATH=\1\2:{{extra_path}}\3"```

You need escaped back slash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment