12

I have a list of aliases in a file, .bash_aliases, which is being copied to remote servers with ansible playbook. The file is getting copied to the destination but the .bashrc (which in turns load the .bash_aliases) file is not getting loaded using the following ansible task.

I have tried giving the executable argument

  - name: source the .bashrc file
    shell: source ~/.bashrc
    args:
      executables: "/bin/bash"

Without argument

  - name: source the .bashrc file
    shell: source ~/.bashrc

With raw module

  - name: source the .bashrc file
    raw: source ~/.bashrc

With command module - name: source the .bashrc file command: source ~/.bashrc

Nothing works!!! Any help

Hearen
  • 7,420
  • 4
  • 53
  • 63
Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65
  • 1
    How do you "know" it's not getting loaded? Post something that reproduces the problem. – Karoly Horvath Dec 07 '15 at 13:00
  • @KarolyHorvath because the aliases which I have listed in `.bash_aliases` file are not working. But when manually `source .bashrc` file after logging in to the remote server, aliases works fine. – Ajeet Khan Dec 07 '15 at 13:02
  • That doesn't mean it's not executed. – Karoly Horvath Dec 07 '15 at 13:03
  • @KarolyHorvath Than how come the aliases are not working after running the playbook with the above listed module? – Ajeet Khan Dec 07 '15 at 13:07
  • `bash -c 'a=5; echo $a'; echo $a` – Karoly Horvath Dec 07 '15 at 13:09
  • @KarolyHorvath What are you trying to explain. please elaborate. – Ajeet Khan Dec 07 '15 at 13:12
  • 2
    I assume that you want to use some aliases in later tasks. If that is the case then you are out of luck because ansible put each task in a **seperate** python script, then copy it to the host and then execute it. Any alias you set in a previous task will not be seen in any following task. – Sebastian Stigler Dec 07 '15 at 13:45
  • @KarolyHorvath I am trying to make permanent aliases and not for a particular ansible session. – Ajeet Khan Dec 07 '15 at 14:05
  • Possible duplicate of [Not possible to source .bashrc with Ansible](http://stackoverflow.com/questions/22256884/not-possible-to-source-bashrc-with-ansible) – Don Branson Mar 15 '16 at 20:10

2 Answers2

9

From reading your comments, you stated that you are trying to make permanate aliases and not for a particular session. Why not create those aliases inside of /etc/profile.d on the machines you need to have those particular aliases on instead of by user?

Also, from another post that popped up when I ran a google search on Ansible specifics as I am no Ansible expert... Not possible to source .bashrc with Ansible (thanks to @chucksmash for the link)

"Steve Midgley

You have two options to use source with ansible. One is with the "shell:" command and /bin/sh (the ansible default). "source" is called "." in /bin/sh. So your command would be:

-name: source bashrc
sudo: no
shell: . /home/username/.bashrc && [the actual command you want run]

Note you have to run a command after sourcing .bashrc b/c each ssh session is distinct - every ansible command runs in a separate ssh transaction.

Your second option is to force Ansible shell to use bash and then you can use the "source" command:\

name: source bashrc
sudo: no   
shell: source /home/username/.bashrc && [the actual command you want run]
args:
  executable: /bin/bash

Finally, I'll note that you may want to actually source "/etc/profile" if you're on Ubuntu or similar, which more completely simulates a local login."

Community
  • 1
  • 1
IT_User
  • 729
  • 9
  • 27
  • 1
    If you look at the URL of the other site you linked, you'll see they are just reproducing StackOverflow content ("...q/stackoverflow/22256884/...". The answer is available [here](http://stackoverflow.com/a/27541856/341510) – chucksmash Mar 08 '16 at 21:12
0

If you expect the variables you set in one Bash instance to be visible in other Bash instances, or in the Ansible instance which ran this Bash script, there is no way to do that; this is inherent to the Unix process model.

What you can do is set a variable and then run whichever tool needs for this value to be set.

bash -c 'var="value" /path/to/other/tool'

This can be arbitrarily complex, though you're probably better off creating a separate external script if the task you need to perform could require anything more than the most trivial debugging.

tripleee
  • 175,061
  • 34
  • 275
  • 318