can not source ~/.bashrc file with ansible

10,935

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."

Share:
10,935

Related videos on Youtube

Ajeet Khan
Author by

Ajeet Khan

Hello Folks, I am a DevOps Engineer currently working at webengage. I write blogs, make screencast trying to help newbies to get started with stuff with the aim - "Keep Learning & Keep Sharing"

Updated on September 15, 2022

Comments

  • Ajeet Khan
    Ajeet Khan over 1 year

    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

    • Karoly Horvath
      Karoly Horvath over 8 years
      How do you "know" it's not getting loaded? Post something that reproduces the problem.
    • Sebastian Stigler
      Sebastian Stigler over 8 years
      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.
  • chucksmash
    chucksmash about 8 years
    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