7

这是我的剧本中负责从源代码下载和构建 ruby​​ 的部分:

  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

最后一个任务(配置 make 和 make install)在每次配置时运行。

我可以检查 ruby​​ 是否已经安装了正确的版本ruby -v。如何在我的剧本中定义这是运行此步骤的条件?

另外,有没有办法让这成为整个任务列表的条件,而不仅仅是最后一个?

4

2 回答 2

13

您可以使用registertask 参数将结果保存ruby -v到变量中,然后使用whentask 参数有条件地执行任务。

您的示例将如下所示:

  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
于 2013-11-09T22:28:30.763 回答
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
于 2014-09-03T07:31:41.293 回答