3

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
jmartori
  • 384
  • 1
  • 5
  • 14
  • Your `PATH` probably doesn't need to be `export`:ed again; the system already does that, and then it will stay exported. See also https://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export – tripleee Aug 17 '23 at 06:50

2 Answers2

1

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.

jmartori
  • 384
  • 1
  • 5
  • 14
0

Solution without using shell module instead of

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

Use

    content: 'export PATH=$PATH:{{ my_custom_path_var }}'
Farfax
  • 3
  • 1