首页 > 解决方案 > 更改用户并安装 ruby

问题描述

我的 Ansible 服务器以 root 用户身份与代理通信,但我需要以不同用户身份安装 ruby​​,即部署。所以我试图切换用户以使用“成为”来安装 ruby​​,但我遇到了问题。似乎当我尝试切换用户并运行命令时,它无法使用部署用户的 .bashrc 文件。下面是我的 YML 文件

---
- hosts: test1
  become: true
  tasks:
  - name: adding node.js repository
    shell: curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
  - name: adding yarn pubkey
    shell: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  - name: adding yarn repo
    shell: echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  - name: update cache
    apt: update_cache=true
  - name: install all the below list of packages
    apt: name={{ item }} update_cache=true
    with_items:
      - git-core
      - curl 
      - zlib1g-dev 
      - build-essential 
      - libssl-dev 
      - libreadline-dev 
      - libyaml-dev 
      - libsqlite3-dev 
      - sqlite3 
      - libxml2-dev 
      - libxslt1-dev 
      - libcurl4-openssl-dev 
      - software-properties-common 
      - libffi-dev 
      - nodejs 
      - yarn
  - name: change to deploy home directory
    shell: cd 
    become: true
    become_user: deploy
  - name: getting repo from git
    shell: git clone https://github.com/rbenv/rbenv.git ~/.rbenv
    become: true
    become_user: deploy
  - name: copy path
    shell: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    become: true
    become_user: deploy
  - name: copy eval
    shell: echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    become: true
    become_user: deploy
  - name: execute shell
    shell: exec $SHELL
    become: true
    become_user: deploy
  - name: ruby repo
    shell: git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
    become: true
    become_user: deploy
  - name: copy paths
    shell: echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
    become: true
    become_user: deploy
  - name: shell execute
    shell: exec $SHELL
    become: true
    become_user: deploy
  - name: install ruby
    shell: rbenv install 2.4.4
    become: true
    become_user: deploy
  - name: set global
    shell: rbenv global 2.4.4
    become: true
    become_user: deploy

我收到以下错误:

TASK [install ruby] ***************

致命:[host1]:失败!=> {“更改”:true,“cmd”:“rbenv install 2.4.4”,“delta”:“0:00:00.003186”,“end”:“2018-09-25 15:43:23.224716”, "msg": "非零返回码", "rc": 127, "start": "2018-09-25 15:43:23.221530", "stderr": "/bin/sh: 1: rbenv: not找到”,“stderr_lines”:[“/bin/sh:1:rbenv:未找到”],“stdout”:“”,“stdout_lines”:[]}

但是当我在命令中手动为 rbenv 提供路径时,它工作正常。如下所示:

- name: install ruby
  shell: /home/deploy/.rbenv/bin/rbenv install 2.4.4
  become: true
  become_user: deploy

你能告诉我为什么它会这样吗?

我还需要使用 gem 安装 bundler。我正在切换到用户“部署”,但它会检查根用户目录而不是部署用户并给出错误。请参阅以下 YML 部分以了解 gem 和错误:

  - name: install bundler
    shell: gem install bundler
    become: true
    become_user: deploy

以下是捆绑器的错误:

TASK [install bundler] *********

致命:[host1]:失败!=> {“更改”:true,“cmd”:“gem install bundler”,“delta”:“0:00:02.396195”,“end”:“2018-09-25 16:21:18.703899”,“msg ": "非零返回码", "rc": 1, "start": "2018-09-25 16:21:16.307704", "stderr": "ERROR: While execution gem ... (Gem:: FilePermissionError)\n 您没有 /var/lib/gems/2.3.0 目录的写入权限。", "stderr_lines": ["错误:执行 gem ... (Gem::FilePermissionError)", "您没有 /var/lib/gems/2.3.0 目录的写入权限。"], "stdout": "", "stdout_lines": []}

请帮我解决问题。

谢谢。

标签: ansible

解决方案


该剧本并没有像您认为的那样做,因为其中每一项任务实际上都是它自己的 ssh 连接。所以这:

- name: change to deploy home directory
  shell: cd 
  become: true
  become_user: deploy
- name: execute shell
  shell: exec $SHELL

... 等价于ssh root@the-host "su deploy -c 'cd; exit'"; ssh root@the-host "su deploy -c 'exec $SHELL; exit'"等等。

您还很想将所有这些运行的任务移动deploy到他们自己的剧本或角色中,并将它们包含在这些任务中become: truebecome: deploy在包含或角色级别上,以防止在所有任务中重复该文本。

但即使你不这样做,通往成功的最短路径是将所有 10 个步骤合并到一个shell: |块中,以挽救你的理智并强化它们都需要在同一个 shell 会话中发生

- name: install ruby using rbenv
  become: true
  become_user: deploy
  shell: |
     set -e  # <-- stop running this script if something fails
     cd $HOME
     export ... # etc etc
  args:
    creates: /home/deploy/.rbenv/versions/2.4.4/bin/ruby

可选地包括该args: creates:业务将使 ansible 有机会跳过该步骤(如果它已经配置rbenv)。当然,您可能希望更新它以以与您尝试执行的操作更相关的方式保护该步骤。


推荐阅读