0

I am trying to append several environment variables to the .bashrc file on a server:

- name: append environment variables to the end of .bashrc file
  blockinfile:
    dest: "/home/username/.bashrc"
    insertafter: EOF
    block: |
      # environment variable exports
      export something="something";

Running the above results in the following error:

fatal: [ip-address]: FAILED! => {"changed": false, "msg": "Path /home/username/.bashrc does not exist !", "rc": 257}

If I change the path from /home/username/.bashrc to ~/.bashrc the playbook is successful, however when I log into the server with the relevant user, the .bashrc has not been updated.

Ansible version: 2.5.1 Server OS: Ubuntu 16.04

Is there another file I should be using instead of .bashrc to append environment variables to for the specified user (which is the same user that I am running the ansible-playbook as)?

The goal is to add the environment variables and then in a separate task, source the file to make the environment variables available permanently for that user.

darkpool
  • 13,822
  • 16
  • 54
  • 89
  • Are you aware that a `source` command as you have written above has no effect whatsoever? You also don't even mention which task fails, and in which task you change the path. This qualifies to close as unclear, but equally likely as a dup of [this question](https://stackoverflow.com/q/22256884/2947502), so please decide. – techraf Apr 20 '18 at 22:24
  • It is the first command that fails with the specified error. ie: The first task cannot find the specified .bashrc file. I have edited the question to remove the second task for clarification. – darkpool Apr 21 '18 at 04:55

1 Answers1

2

After several hours of debugging and searching for a solution, it turned out that the main deploy.yml file had a setting connection: local which was causing every task to execute on the local machine instead of the remote server. This is what was resulting in the file not found errors.

I am also now using ~/.profile to store the environment variables instead of ~/.bashrc which appears to be the preferred location.

darkpool
  • 13,822
  • 16
  • 54
  • 89