3

What specific syntax must be changed below in order for Ansible to successfully add /home/another-user/bin to the $PATH variable on a RHEL 7 VM?

Current Version of Playbook:

The playbook we are using sets a variable for the /home/another-user/bin and then uses the variable to add a line to /etc/environment as follows:

vars:
  extra_path: /home/another-user/bin
vars_files:
  - myVars.yaml
#Other stuff

- name: Update /etc/environment for newly loaded apps
  lineinfile:
    dest: /etc/environment
    line: "export PATH={{ extra_path }}:$PATH"
    owner: root
  become: true
  become_user: root
  vars:
    ansible_become_password: "{{ root_pass_word }}"

BAD RESULT

The problem is that the above example is destroying all existing entries in the $PATH so that basic commands like cat and others are no longer recognized:

[my-user@ip-10-0-0-171 ~]$ echo $PATH
/home/another-user/bin:$PATH:/usr/local/sbin:/usr/sbin:/home/my-user/.local/bin:/home/my-user/bin
[my-user@ip-10-0-0-171 ~]$ cat /etc/environment
-bash: cat: command not found
[my-user@ip-10-0-0-171 ~]$

I did find other examples using regular expressions here and on Stack Overflow, but those examples causes the same problem of command not found by munging the /etc/environment in one way or another.

1
  • Your $PATH is not defined in the first place. No idea why. As a workaround add :/bin:/usr/bin to it - cat is placed in some of these paths. Oct 2, 2020 at 1:50

2 Answers 2

4

The /etc/environment file is not evaluated as a shell script. To quote from its man page (rearranged for clarity):

The configuration files contain a list of "KEY=VALUE" environment variable assignments, separated by newlines.

The right hand side of these assignments may reference previously defined environment variables, using the "${OTHER_KEY}" and "$OTHER_KEY" format.

...

No other elements of shell syntax are supported.

So, you have an export command in the inserted line, which is invalid for the /etc/environment file. Change from:

line: "export PATH={{ extra_path }}:$PATH"

to

line: "PATH={{ extra_path }}:$PATH"

and it should work correctly.


Something else to check here is the new path you're adding, which seems to be /home/another-user/bin. User's home directories are usually protected with a 700 permission, so the contents would only be visible to the owner (i.e. the user corresponding to that home directory). /etc/environment applies to all users, but is the new path configuration relevant for all users? Something to consider looking into.

1

I found the accepted answer does not work on Centos7. Usually I find RHEL7 and Centos7 have the same functionality at this level.

/etc/environment was empty and was behaving exactly as described in this Ubuntu doc (yes I double checked and the VM is definitely Centos7). It was not expanding the $PATH variable and that caused the path to break as it did not like the literal $ in it.

I found the recommended solution from that doc of adding a script to /etc/profile.d/ does work as expected i.e.

echo 'export PATH=$PATH:/some/other/path' > /etc/profile.d/my_path.sh

or using the ansible copy module

- name: Add to PATH
  copy:
    content: "export PATH=$PATH:/some/other/path"
    dest: "/etc/profile.d/my_path.sh"

As I don't have any RHEL7 VMs I am unable to test if this solution also works there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .