How to get ansible to update PATH

Viewed 2264

is there a way to do:

export PATH=$PATH:/new/path/to/bin

in Ansible, if possible without using shell or command.

I've tried this

 - name: Add another bin dir to system-wide $PATH.
  copy:
    dest: /etc/profile.d/custom-path.sh
    content: 'PATH=$PATH:{{ my_custom_path_var }}'

That I got from: https://www.jeffgeerling.com/comment/reply/2799

But it doesn't work as PATH results in:

\$PATH:/new/path/to/bin

Breaking the system's PATH.

Thanks!

2 Answers

Using shell or command would be:

  - name: Add pm2 to PATH
    shell: echo "PATH=$PATH:/new/path/to/bin" > /etc/environment
    become: true

But I'd still prefer an option that doesn't use shell/command.

Solution without using shell module instead of

    content: 'PATH=$PATH:{{ my_custom_path_var }}'

Use

    content: 'export PATH=$PATH:{{ my_custom_path_var }}'
Related