Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用Ansible将二进制文件添加到路径_Bash_Path_Ansible_Elixir_Ansible Playbook - Fatal编程技术网

Bash 使用Ansible将二进制文件添加到路径

Bash 使用Ansible将二进制文件添加到路径,bash,path,ansible,elixir,ansible-playbook,Bash,Path,Ansible,Elixir,Ansible Playbook,我正在尝试使用Ansible安装编程语言的版本管理器 以下是我用于此目的的剧本: - name: Kiex Installation hosts: web gather_facts: false remote_user: deployer tasks: - shell: \curl -sSL https://raw.githubusercontent.com/taylor/kiex/master/install | bash -s - name: Add Kiex

我正在尝试使用Ansible安装编程语言的版本管理器

以下是我用于此目的的剧本:

- name: Kiex Installation
  hosts: web
  gather_facts: false
  remote_user: deployer
  tasks:
    - shell: \curl -sSL https://raw.githubusercontent.com/taylor/kiex/master/install | bash -s
    - name: Add Kiex Bin to Path
      lineinfile:
        dest: /home/deployer/.bashrc
        regexp: '^test -s'
        line: '[[ -s "$HOME/.kiex/scripts/kiex" ]] && source "$HOME/.kiex/scripts/kiex"'
    - name: Reload Path
      shell: source /home/deployer/.bashrc
      args:
        executable: /bin/bash
    - shell: echo $PATH
      register: pathul
    - debug:
        var: pathul

- name: Elixir Installation
  hosts: web
  gather_facts: false
  remote_user: deployer
  tasks:
    - shell: echo $PATH
      register: pathul
    - debug:
        var: pathul
    - name: Install Elixir Version
      command: /home/deployer/.kiex/bin/kiex list
      args:
        executable: /bin/bash
        chdir: /home/deployer/
    - name: Set Elixir Version as Default
      shell: kiex default 1.4
Kiex的安装是成功的,如果我登录到远程机器,我就可以使用
Kiex
命令简单地运行它。我可以这样做,因为我在“~/.kiex/scripts/kiex”中获取了二进制文件。当我回显
$PATH
变量时,它会在其中显示kiex二进制文件路径
/home/deployer/.kiex/bin

$ echo $PATH
/home/deployer/.kiex/bin:/home/deployer/.kiex/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
然而,
kiex
kiex列表
甚至
/home/deoployer/.kiex/bin/kiex列表
Elixir安装播放中失败,显示以下消息:

TASK [Set Elixir Version as Default] *******************************************
fatal: [local-web-2]: FAILED! => {"changed": true, "cmd": "kiex default 1.4", "delta": "0:00:00.002042", "end": "2017-01-26 22:13:32.898082", "failed": true, "rc": 127, "start": "2017-01-26 22:13:32.896040", "stderr": "/bin/sh: 1: kiex: not found", "stdout": "", "stdout_lines": [], "warnings": []}
另外,通过ansible注册回显路径结果的
pathul
变量不包含
/home/deployer/.kiex/bin

"stdout": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

如何通过Ansible使
kiex
命令正常工作?

只需使用完整的绝对路径,就像您在
Install Elixir Version
任务中尝试的那样,但请注意,您在示例和发布的解释中都有一个打字错误:

[]即使是
/home/deoployer/.kiex/bin/kiex列表
[]也会失败[s]

它应该是
deployer
,就像在第一个剧本中一样,而不是
deployer

如果您提供了正确的路径,Ansible没有其他理由会因“kiex:notfound”消息而失败


关于其他任务的说明:

  • 引用《曼巴什》:

    当启动非登录shell的交互式shell时,bash从
    ~/.bashrc
    读取并执行命令(如果该文件存在)

    因此,当您使用Ansible执行任务时,甚至不会读取您的
    ~/.bashrc
    ,因为它不是交互式会话

    例如,这就是为什么
    pathul
    变量不包含在
    ~/.bashrc
    中应用的更改

  • 以下两个任务分别运行bash进程。第一项任务中的环境对第二项任务的环境没有影响:

    - name: Reload Path
      shell: source /home/deployer/.bashrc
      args:
        executable: /bin/bash
    - shell: echo $PATH
      register: pathul
    

我的手册中确实有部署器。打字错误就在stackoverflow示例中。关于~/.bashrc没有被阅读,我确实怀疑它有问题。我应该把
[[-s“$HOME/.kiex/scripts/kiex”]]和&source“$HOME/.kiex/scripts/kiex”放在哪里?
?我不知道。这取决于操作系统和发行版。我建议使用完整路径。我使用UbuntuXenial。我重复一遍:对于问题中显示的内容,请使用完整路径。我不知道你打算用长生不老药做什么,也不知道你打算如何使用它。
- name: Reload Path
  shell: source /home/deployer/.bashrc
  args:
    executable: /bin/bash
- shell: echo $PATH
  register: pathul