使用Ansible设置默认的rvm gemset?

时间:2016-05-24 16:19:54

标签: rvm ansible ansible-playbook osx-elcapitan ubuntu-16.04

使用:Mac OS X 10.11.5,ansible 2.0.1.0,Python 2.7.11。 Ubuntu Xenial LTS(Vagrant 1.8.1,VirtualBox版本5.0.16 r105871)作为遥控器。

我正在尝试使用Ansible将新的gemset设置为默认值:

    ---
    # file: roles/ruby-gemset/tasks/main.yml

    - name: RVM | check if the bwv gemset needs to be set default.
      command: rvm list gemsets
      register: gemset_needs_an_update
      environment:
        PATH: "{{ rvm_path }}:{{ ansible_env.PATH }}"
        GEM_HOME: "/usr/local/rvm/gems/ruby-{{ ruby_version }}"
        GEM_PATH: "/usr/local/rvm/gems/ruby-{{ ruby_version }}@bwv:/usr/local/rvm/gems/ruby-{{ ruby_version }}@global"
      changed_when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

    - name: RVM | Create and use ruby-{{ ruby_version }}@bwv.
      command: "rvm use ruby-{{ ruby_version }}@bwv --create"
      environment:
        PATH: "{{ rvm_path }}:{{ ansible_env.PATH }}"
        GEM_HOME: "/usr/local/rvm/gems/ruby-{{ ruby_version }}@bwv"
        GEM_PATH: "/usr/local/rvm/gems/ruby-{{ ruby_version }}@bwv:/usr/local/rvm/gems/ruby-{{ ruby_version }}@global"
      when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

    - name: RVM | Use ruby-{{ ruby_version }}@bwv gemset as default.
      command: "rvm use ruby-{{ ruby_version }}@bwv --default"
      environment:
        PATH: "{{ rvm_path }}:{{ ansible_env.PATH }}"
        GEM_HOME: "/usr/local/rvm/gems/ruby-{{ ruby_version }}@bwv"
        GEM_PATH: "/usr/local/rvm/gems/ruby-{{ ruby_version }}@bwv:/usr/local/rvm/gems/ruby-{{ ruby_version }}@global"
      when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

我们的想法是编写一个Ansible playbook,将一个可用的ruby gemset安装到远程Ubuntu服务器上。但由于某种原因,默认情况根本没有改变。相反,这种情况一再发生:

    TASK [ruby-gemset : RVM | check if the bwv gemset needs to be set default.] ****
    changed: [xenial64]

    TASK [ruby-gemset : RVM | Create and use ruby-2.3.1@bwv.] **********************
    changed: [xenial64]

    TASK [ruby-gemset : RVM | Use ruby-2.3.1@bwv gemset as default.] ***************
    changed: [xenial64]

是否可以使用Ansible以某种方式完成远程服务器?我意识到那些环境线只能帮助命令传递而不会失败,但现在几乎没有做任何事情。

2 个答案:

答案 0 :(得分:0)

至少这似乎有效:

    ---
    # file: roles/ruby-gemset/tasks/main.yml

    - name: RVM | check if the bwv gemset needs to be set default.
      become_user: ubuntu
      command: bash -lc "rvm list gemsets"
      register: gemset_needs_an_update
      changed_when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

    - name: RVM | Create and use ruby-{{ ruby_version }}@bwv.
      become_user: ubuntu
      command: bash -lc "rvm use ruby-{{ ruby_version }}@bwv --create"
      when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

    - name: RVM | Use ruby-{{ ruby_version }}@bwv gemset as default.
      become_user: ubuntu
      command: bash -lc "rvm use ruby-{{ ruby_version }}@bwv --default"
      when: gemset_needs_an_update.stdout.find('=> ruby-{{ ruby_version }}@bwv') == -1

答案 1 :(得分:0)

我发现使用[rvm do set][1]命令来帮助解决Ansible产生的最小shell环境。

- name: Set Default Gem Set
  shell: "/usr/local/rvm/bin/rvm {{ use_ruby_version}} do rvm use {{ rvm_default }} --default"
  args:
    chdir: "/home/{{ user_name }}"
  become: yes
  become_user: "{{ user_name }}"
  tags:
    - default_gemset

Ansible Vars:

  use_ruby_version: ruby-2.2.5
  current_gemset: foo
  rvm_default: "{{ use_ruby_version }}@{{ current_gemset }}"

运行Ansible 2.1         Centos 7 / bash