我正在尝试运行一个相对简单的Ansible playbook来通过Vagrant配置Ubuntu VM。大致它遵循3个步骤:
前两个步骤很好,但第三个我正在努力。使用其他剧本作为参考(rbenv和nvm)我创建了以下基本剧本:
---
- hosts: all
vars:
user: joe_bloggs
ruby_version: 2.1.5
tasks:
#
# System
#
- name: Update apt cache
sudo: yes
apt: update_cache=yes cache_valid_time=86400
- name: Upgrade existing system packages
sudo: yes
apt: upgrade=dist
- name: Install essential system packages
sudo: yes
apt: name={{ item }} state=latest
with_items:
- git
- curl
- openssl
- build-essential
- name: Add user
sudo: yes
user: name={{user}} shell=/bin/bash groups=sudo
#
# rbenv
#
- name: Install rbenv | Clone repo
sudo: yes
sudo_user: "{{ user }}"
git: repo=https://github.com/sstephenson/rbenv.git dest=~/.rbenv accept_hostkey=yes update=yes
- name: Install rbenv | Create plugins directory
sudo: yes
sudo_user: "{{ user }}"
file: path=~/.rbenv/plugins/ mode=0755 state=directory
- name: Install rbenv | Install ruby-build plugin
sudo: yes
sudo_user: "{{ user }}"
git: repo=git://github.com/sstephenson/ruby-build.git dest=~/.rbenv/plugins/ruby-build accept_hostkey=yes
- name: Install rbenv | Add path to profile
sudo: yes
sudo_user: "{{ user }}"
lineinfile: line='export PATH="$HOME/.rbenv/bin:$PATH"' regexp="\$HOME\/\.rbenv\/bin:\$PATH" dest=~/.bashrc
- name: Install rbenv | Enable shims in profile
sudo: yes
sudo_user: "{{ user }}"
lineinfile: line='eval "$(rbenv init -)"' regexp="rbenv init \-" dest=~/.bashrc
- name: Install Ruby | Install version
sudo: yes
sudo_user: "{{ user }}"
shell: rbenv install {{ ruby_version }} executable=/bin/bash
- name: Install Ruby | Set default version and rehash
sudo: yes
sudo_user: "{{ user }}"
shell: rbenv global {{ ruby_version }} && rbenv rehash executable=/bin/bash
运行上面的剧本我遇到一个错误:
stderr: /bin/bash: rbenv: command not found
是否有一种优雅的方式通过Ansible作为用户运行命令,使用它们加载的shell和环境变量?
我尝试添加-i
sudo标志和sudo -iu {{user}} rbenv install ...
之类的命令,但我还没有运气。
我可以通过指定rbenv
可执行文件的完整路径来运行Ruby安装,但该技术并不总是合适的(某些工具可能不提供单个可执行文件)。
答案 0 :(得分:0)
如果您使用的是ansible 1.4或更高版本,则可以使用remote_user
参数执行此操作。无论是在剧本级还是在任务级别。
---
- hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: yourname
答案 1 :(得分:0)
我尝试使用Ansible 2.1在一个流浪盒上使用rbenv安装ruby-2.2.3时遇到了同样的问题。
rbenv安装在名为rails
的ubuntu用户身上,该用户没有sudo权限但是当ansible连接到我的ubuntu盒子时,它会丢失env。在/home/rails/.bashrc中定义的$ PATH它只是默认为ansible_env.PATH
假设您已按照以下步骤安装了rbenv:
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
要使路径正常工作,我需要手动设置它,所以对于我所做的特定任务:
- name: install rubies
become_user: rails
environment:
PATH: "{{ ansible_env.PATH }}:/home/rails/.rbenv/shims:/home/rails/.rbenv/bin "
shell: '{{ rbenv_shell }} -lc "rbenv install {{ item[0] }}"'
with_together
- "{{ rubies }}"
- "{{ ruby_installed.results }}"
...
这会将你的rbenv命令的路径附加到ansible_env.PATH查找并使其按预期工作。