Not possible to source .bashrc with Ansible

后端 未结 10 1607
耶瑟儿~
耶瑟儿~ 2020-12-02 08:19

I can ssh to the remote host and do a source /home/username/.bashrc - everything works fine. However if I do:

- name: source bashrc
  sudo: no
          


        
10条回答
  •  醉酒成梦
    2020-12-02 08:40

    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.

提交回复
热议问题