我的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安装 2.4.4“,” delta“:” 0:00:00.003186“,” end“:” 2018-09-25 15:43:23.224716“,” msg“:”非零返回码“,” rc“: 127,“开始”: “ 2018-09-25 15:43:23.221530”,“ stderr”:“ // bin / sh:1:rbenv:不 找到“,” 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安装捆绑程序。我将切换到用户“部署”,但是它将检查根用户目录而不是部署用户并给出错误。请参阅下面的YML部分了解错误和错误:
- name: install bundler
shell: gem install bundler
become: true
become_user: deploy
以下是捆绑器的错误:
TASK [install bundler] *********
致命:[host1]:失败! => {“更改”:true,“ cmd”:“宝石安装 捆绑器'',“三角洲”:“ 0:00:02.396195”,“结束”:“ 2018-09-25 16:21:18.703899”,“ msg”:“非零返回码”,“ rc”:1,“开始”: “ 2018-09-25 16:21:16.307704”,“ stderr”:“错误:执行gem时 ...(Gem :: FilePermissionError)\ n您没有写权限 /var/lib/gems/2.3.0目录。“,” stderr_lines“:[”错误: 当执行gem ...((Gem :: FilePermissionError)时“,”您不会 对/var/lib/gems/2.3.0目录具有写权限。“], “ stdout”:“”,“ stdout_lines”:[]}
请帮助我解决此问题。
谢谢。
答案 0 :(得分:0)
那本剧本并没有像您想像的那样做,因为这些任务中的每一项都是有效地 自己的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: true
和become: deploy
一起使用或角色级别,以免在整个任务中重复该文本。
但是,即使您不这样做,最短的成功之路就是将所有这10个步骤分解为一个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:
的企业将为您提供一个战斗机会,使其可以跳过该步骤(如果它已经配置了rbenv
)。当然,您可能会希望更新它,以与您尝试执行的操作更相关的方式保护步骤。