无法使用Ansible创建source.bashrc

无法使用Ansible创建source.bashrc,ansible,Ansible,我可以通过ssh连接到远程主机并执行source/home/username/.bashrc-一切正常。 但是,如果我这样做: - name: source bashrc sudo: no action: command source /home/username/.bashrc 我得到: failed: [hostname] => {"cmd": ["source", "/home/username/.bashrc"], "failed": true, "rc": 2} msg

我可以通过ssh连接到远程主机并执行
source/home/username/.bashrc
-一切正常。 但是,如果我这样做:

- name: source bashrc
  sudo: no
  action: command source /home/username/.bashrc
我得到:

failed: [hostname] => {"cmd": ["source", "/home/username/.bashrc"], "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

我不知道我做错了什么…

所以
命令将只运行可执行文件<代码>源代码
本身不是可执行文件。(这是一个内置的shell命令)。 是否有任何原因需要
源代码
一个完整的环境变量

在Ansible中包含环境变量还有其他方法。例如,
环境
指令:

- name: My Great Playbook
  hosts: all
  tasks:
    - name: Run my command
      sudo: no
      action: command <your-command>
      environment:
          HOME: /home/myhome

-名称:source bashrc
苏多:没有
shell:source/home/username/.bashrc&&

在这些情况下,一旦Ansible步骤运行,shell实例/环境将终止。

您有两个选项可以将source与Ansible一起使用。一种是使用“shell:”命令和/bin/sh(ansible默认值)。“source”在/bin/sh中被称为“.”。因此您的命令是:

- name: source bashrc
  sudo: no   
  shell: . /home/username/.bashrc && [the actual command you want run]
注意,您必须在寻源之后运行一个命令。bashrc b/c每个ssh会话都是不同的-每个ansible命令都在单独的ssh事务中运行

第二个选项是强制Ansible shell使用bash,然后可以使用“source”命令:


最后,我要指出的是,如果您使用的是Ubuntu或类似的软件,那么您可能需要实际地获取“/etc/profile”,这更完全地模拟了本地登录。

我知道这个答案来得太晚了,但我已经看到足够多的代码,您可以使用sudo选项
-I
因此:

如文件所述

The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell.  This means that login-specific
               resource files such as .profile or .login will be read by the shell.  If a command is specified, it is passed to the shell for execution via the shell's -c option.
               If no command is specified, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the shell.  It also initializes
               the environment to a minimal set of variables, similar to what is present when a user logs in.  The Command environment section below documents in detail how the -i
               option affects the environment in which a command is run.

嗯,我尝试了列出的答案,但在安装ruby时,这些答案对我不起作用。我必须从
/root/.bash\u profile

PATH=$PATH:$HOME/bin:$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin
export PATH
eval "$(rbenv init -)"
最后,我想到了这个

- shell: sudo su - root -c 'rbenv install -v {{ ruby_version }}'
任何命令都可以使用它

- shell: sudo su - root -c 'your command'

我发现最好的解决方案是:

- name: Source .bashrc
  shell: . .bashrc
  become: true
您可以通过添加(默认值:root)来更改用户:


更多信息请点击此处:

正确的方法应该是:

- hosts: all
  tasks:
    - name: source bashrc file
      shell: "{{ item }}"
      with_items:
         - source ~/.bashrc
         - your other command

注意:这是在
ansible 2.0.2
版本中进行的测试

我在尝试让VirtualVwrapper在Ubuntu服务器上工作时遇到了同样的问题。我是这样使用Ansible的:

- name: Make virtual environment
  shell: source /home/username/.bashrc && makevirtualenv virenvname
  args:
    executable: /bin/bash
但是源命令不起作用

最终我发现.bashrc文件的顶部有几行代码,当Ansible调用时,这些代码会阻止源代码工作:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

我在.bashrc中注释掉了这些行,之后一切都按预期进行了。

我用ansible 2.4.1.0尝试了上面的所有选项,在另外两行之前没有人能工作,下面是重新生成案例的细节

$ cat ~/.bash_aliases 
alias ta="echo 'this is test for ansible interactive shell'";
这是ansible测试:

这就是结果:

$ ansible-playbook testInteractiveBash.yml 
 [WARNING]: Could not match supplied host pattern, ignoring: all

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Check the basic string operations] ************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Test Interactive Bash Failure] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "ta", "delta": "0:00:00.001341", "end": "2018-10-31 10:11:39.485897", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.484556", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using Source] ***********************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "source ~/.bash_aliases && ta", "delta": "0:00:00.002769", "end": "2018-10-31 10:11:39.588352", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.585583", "stderr": "/bin/bash: ta: command not found", "stderr_lines": ["/bin/bash: ta: command not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using .] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ". ~/.bash_aliases && ta", "delta": "0:00:00.001425", "end": "2018-10-31 10:11:39.682609", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.681184", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using /bin/bash -ci] ****************************************************************************************************************************************
changed: [127.0.0.1]

TASK [debug] ****************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": {
        "changed": true, 
        "cmd": "/bin/bash -ic 'ta'", 
        "delta": "0:00:00.414534", 
        "end": "2018-10-31 10:11:40.189365", 
        "failed": false, 
        "rc": 0, 
        "start": "2018-10-31 10:11:39.774831", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "this is test for ansible interactive shell", 
        "stdout_lines": [
            "this is test for ansible interactive shell"
        ]
    }
}

TASK [Test Interactive Bash Using sudo -ui] *********************************************************************************************************************************************
 [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo

fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "sudo -ui hearen ta", "delta": "0:00:00.007906", "end": "2018-10-31 10:11:40.306128", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2018-10-31 10:11:40.298222", "stderr": "sudo: unknown user: i\nsudo: unable to initialize policy plugin", "stderr_lines": ["sudo: unknown user: i", "sudo: unable to initialize policy plugin"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using ssh -tt localhost /bin/bash -ci] **********************************************************************************************************************
hearen@localhost's password: 
changed: [127.0.0.1]

PLAY RECAP ******************************************************************************************************************************************************************************
127.0.0.1                  : ok=8    changed=6    unreachable=0    failed=0  
有两种选择可行:

  • shell:/bin/bash-ic'ta'
  • shell:ssh-ttlocalhost/bin/bash-ci'ta'
    但是这个需要在本地输入密码

我的2美分,我绕过了问题来源
~/.nvm/nvm.sh
进入
~/.profile
,然后使用
sudo-iu
,正如另一个答案中所建议的那样

2018年1月试用vs Ubuntu 16.04.5

- name: Installing Nvm 
  shell: >
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
  args:
    creates: "/home/{{ ansible_user }}/.nvm/nvm.sh"
  tags:
    - nodejs    

- name: Source nvm in ~/.profile
  sudo: yes
  sudo_user: "{{ ansible_user }}"
  lineinfile: >
    dest=~/.profile
    line="source ~/.nvm/nvm.sh"
    create=yes
  tags: 
    - nodejs
  register: output    

- name: Installing node 
  command: sudo -iu {{ ansible_user }} nvm install --lts
  args:
     executable: /bin/bash
  tags:
    - nodejs    

许多响应建议使用source ~/.bashrc,但主要问题是ansible shell不是交互式的,~/.bashrc实现默认情况下会忽略非交互式shell(检查其开头)

我发现,在ssh交互式登录之后,以用户身份执行命令的最佳解决方案是:

- hosts: all
  tasks:
    - name: source user profile file
      #become: yes
      #become_user: my_user  # in case you want to become different user (make sure acl package is installed)
      shell: bash -ilc 'which python' # example command which prints
      register: which_python
    - debug:
      var: which_python
bash:“-i”表示交互式shell,因此不会忽略.bashrc
“-l”表示登录shell,它提供完整的用户配置文件

几乎不错,不幸的是/bin/sh没有源代码。所以
shell source/home/username/.bashrc
变成了
shell/home/username/.bashrc
shell任务采用如下参数:
executable=/usr/bin/bash
,如果可用,它将在bash中运行。另外请注意,此问题已作为Ansible core中的bug/feature请求提交(并由我评论)。但是Ansible关闭了它,说“写一个插件。”呸。你能读懂我的心思吗?你三个月前回答了这个问题,我正想编辑这个
->
源代码
-你马上就这么做了:)我试过
源代码/etc/profile”
-对我没用。这是有效的:
source“~/.profile”
我在.bashrc中定义了一些bash函数,在源代码.bashrc之后。如何执行/调用这些函数?我正在尝试
shell:.~/。bashrc&&nvm安装{{node_version}
它说,
nvm命令找不到
。我该如何解决这个问题?@RaviTezu:我的问题是由于.bashrc中的以下行造成的:#如果不是以交互方式运行,请不要执行任何操作(case$-in I);*)返回;;esac这至少是ubuntu-16.04 xenial64上的一个问题,其中.bashrc不在非交互式shell上运行,这是通过ssh运行命令时的情况。要进行尝试,请在~/.bashrc中设置一些路径并运行(假设您已经在来宾操作系统上设置了转发到22的端口2222):ssh-p2222ubuntu@127.0.0.1“echo$PATH”如果上面的命令没有显示您在.bashrc中设置的路径,则修复.bashrc这一经典方法适用于Ansible
2.2.0.0
。然而,我应该使用
been
been\u方法
been\u用户
来代替它,这很麻烦。。。我想不出这些“方法”参数的组合是否能正常工作。这是大多数
.bashrc
文件的标准头。您可能希望源代码不同的shell文件,或者使用BASH文档中讨论的
BASH_ENV
source
仅在现有shell中运行时才有意义——它在该shell中运行命令,因此只有在存在要更改其状态或配置的现有shell时才有用。当您运行ansible操作时,将创建一个全新的shell,并在该shell中运行一个命令,这样您就不会在
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
$ cat ~/.bash_aliases 
alias ta="echo 'this is test for ansible interactive shell'";
- name: Check the basic string operations
  hosts: 127.0.0.1 
  connection: local

  tasks:
  - name: Test Interactive Bash Failure
    shell: ta
    ignore_errors: True

  - name: Test Interactive Bash Using Source
    shell: source ~/.bash_aliases && ta
    args:
      executable: /bin/bash
    ignore_errors: yes

  - name: Test Interactive Bash Using .
    shell: . ~/.bash_aliases && ta
    ignore_errors: yes

  - name: Test Interactive Bash Using /bin/bash -ci
    shell: /bin/bash -ic 'ta'
    register: result
    ignore_errors: yes

  - debug: msg="{{ result }}"

  - name: Test Interactive Bash Using sudo -ui
    shell: sudo -ui hearen ta
    register: result
    ignore_errors: yes

  - name: Test Interactive Bash Using ssh -tt localhost /bin/bash -ci
    shell: ssh -tt localhost /bin/bash -ci 'ta'
    register: result
    ignore_errors: yes
$ ansible-playbook testInteractiveBash.yml 
 [WARNING]: Could not match supplied host pattern, ignoring: all

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Check the basic string operations] ************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Test Interactive Bash Failure] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "ta", "delta": "0:00:00.001341", "end": "2018-10-31 10:11:39.485897", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.484556", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using Source] ***********************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "source ~/.bash_aliases && ta", "delta": "0:00:00.002769", "end": "2018-10-31 10:11:39.588352", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.585583", "stderr": "/bin/bash: ta: command not found", "stderr_lines": ["/bin/bash: ta: command not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using .] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ". ~/.bash_aliases && ta", "delta": "0:00:00.001425", "end": "2018-10-31 10:11:39.682609", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.681184", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using /bin/bash -ci] ****************************************************************************************************************************************
changed: [127.0.0.1]

TASK [debug] ****************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": {
        "changed": true, 
        "cmd": "/bin/bash -ic 'ta'", 
        "delta": "0:00:00.414534", 
        "end": "2018-10-31 10:11:40.189365", 
        "failed": false, 
        "rc": 0, 
        "start": "2018-10-31 10:11:39.774831", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "this is test for ansible interactive shell", 
        "stdout_lines": [
            "this is test for ansible interactive shell"
        ]
    }
}

TASK [Test Interactive Bash Using sudo -ui] *********************************************************************************************************************************************
 [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo

fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "sudo -ui hearen ta", "delta": "0:00:00.007906", "end": "2018-10-31 10:11:40.306128", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2018-10-31 10:11:40.298222", "stderr": "sudo: unknown user: i\nsudo: unable to initialize policy plugin", "stderr_lines": ["sudo: unknown user: i", "sudo: unable to initialize policy plugin"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Test Interactive Bash Using ssh -tt localhost /bin/bash -ci] **********************************************************************************************************************
hearen@localhost's password: 
changed: [127.0.0.1]

PLAY RECAP ******************************************************************************************************************************************************************************
127.0.0.1                  : ok=8    changed=6    unreachable=0    failed=0  
- name: Installing Nvm 
  shell: >
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
  args:
    creates: "/home/{{ ansible_user }}/.nvm/nvm.sh"
  tags:
    - nodejs    

- name: Source nvm in ~/.profile
  sudo: yes
  sudo_user: "{{ ansible_user }}"
  lineinfile: >
    dest=~/.profile
    line="source ~/.nvm/nvm.sh"
    create=yes
  tags: 
    - nodejs
  register: output    

- name: Installing node 
  command: sudo -iu {{ ansible_user }} nvm install --lts
  args:
     executable: /bin/bash
  tags:
    - nodejs    
- hosts: all
  tasks:
    - name: source user profile file
      #become: yes
      #become_user: my_user  # in case you want to become different user (make sure acl package is installed)
      shell: bash -ilc 'which python' # example command which prints
      register: which_python
    - debug:
      var: which_python