2013-11-09 37 views
7

這裏是我的劇本負責從源下載和建設的紅寶石部分:Ansible:定義當命令沒有運行(從源代碼構建紅寶石)

vars: 
    ruby_version: '2.0.0-p247' 
    ruby_url: 'ftp://ftp.ruby-lang.org//pub/ruby/2.0/ruby-{{ ruby_version }}.tar.gz' 

    tasks: 

    - name: Ensure ruby dependencies are installed 
    apt: pkg=$item state=installed update-cache=yes 
    sudo: yes 
    with_items: 
     - build-essential 
     - git-core 
     - libreadline6-dev 
     - libyaml-dev 
     - libsqlite3-dev 
     - sqlite3 
     - libgdbm-dev 
     - libncurses5-dev 
     - libtool 
     - bison 
     - libffi-dev 
     - libdigest-hmac-perl 
     - unzip 
     - zlib1g 
     - zlib1g-dev 

    - name: Ensure the ruby source is downloaded 
    get_url: url=$ruby_url dest=/home/vagrant/ruby-{{ ruby_version }}.tar 

    - name: Ensure the ruby source is extracted 
    command: tar -zxf /home/vagrant/ruby-$ruby_version.tar creates=/home/vagrant/ruby-{{ ruby_version }} 

    - name: Ensure ruby is installed 
    command: $item chdir=/home/vagrant/ruby-$ruby_version 
    with_items: 
     - ./configure 
     - make 
     - sudo make install 

的最後一個任務(配置化妝並進行安裝)在每個配置中運行。

我可以檢查ruby是否已經安裝了正確的版本ruby -v。我如何在我的劇本中定義這是執行此步驟的條件?

另外,有沒有辦法讓這成爲整個任務列表的一個條件,而不僅僅是最後一個?

回答

13

您可以使用register任務參數將ruby -v的結果保存到變量中,然後使用when任務參數來有條件地執行任務。

你的例子則是這個樣子:

vars: 
    ruby_version: '2.0.0-p247' 
    ruby_url: 'ftp://ftp.ruby-lang.org//pub/ruby/2.0/ruby-{{ ruby_version }}.tar.gz' 

    tasks: 

    - name: get currently installed ruby version 
    command: ruby -v 
    register: result 
    ignore_errors: True 

    - name: Ensure ruby dependencies are installed 
    apt: pkg={{ item }} state=installed update-cache=yes 
    sudo: yes 
    with_items: 
     - build-essential 
     - git-core 
     - libreadline6-dev 
     - libyaml-dev 
     - libsqlite3-dev 
     - sqlite3 
     - libgdbm-dev 
     - libncurses5-dev 
     - libtool 
     - bison 
     - libffi-dev 
     - libdigest-hmac-perl 
     - unzip 
     - zlib1g 
     - zlib1g-dev 
    when: result.rc !=0 or result.stdout.split()[1] != ruby_version 

    - name: Ensure the ruby source is downloaded 
    get_url: url={{ ruby_url }} dest=/home/vagrant/ruby-{{ ruby_version }}.tar 
    when: result.rc !=0 or result.stdout.split()[1] != ruby_version 

    - name: Ensure the ruby source is extracted 
    command: tar -zxf /home/vagrant/ruby-{{ ruby_version }}.tar creates=/home/vagrant/ruby-{{ ruby_version }} 
    when: result.rc !=0 or result.stdout.split()[1] != ruby_version 

    - name: Ensure ruby is installed 
    command: $item chdir=/home/vagrant/ruby-{{ ruby_version }} 
    with_items: 
     - ./configure 
     - make 
     - sudo make install 
    when: result.rc !=0 or result.stdout.split()[1] != ruby_version 

您還可以將任務到一個單獨的文件(例如,install_ruby_from_source.yaml),然後有條件地包含文件:

vars: 
    ruby_version: '2.0.0-p247' 
    ruby_url: 'ftp://ftp.ruby-lang.org//pub/ruby/2.0/ruby-{{ ruby_version }}.tar.gz' 

    tasks: 

    - name: get currently installed ruby version 
    command: ruby -v 
    register: result 
    ignore_errors: True 

    - include: /path/to/install_ruby_from_source.yaml 
    when: result.rc !=0 or result.stdout.split()[1] != ruby_version 
+1

而如果ruby沒有安裝呢? – kovpack

0

下面是我使用,以保持在一個變量的條件是什麼,因此它可以在任何地方使用:

vars: 
    touch_file: /opt/setup.touch 
tasks: 
- stat: path={{touch_file}} 
    register: touch 
- set_fact: once_a_day="{{(touch.stat.exists == false) or ((ansible_date_time.epoch|int - touch.stat.atime|int) > (3600 * 24))}}" 
- debug: msg="once_a_day={{once_a_day}}" 

- include: include/setup.yml 
    when: once_a_day