gpt4 book ai didi

ruby - 可靠的 : define when a command doesn't have to run (building ruby from source)

转载 作者:数据小太阳 更新时间:2023-10-29 06:57:29 25 4
gpt4 key购买 nike

这是我的剧本中负责从源代码下载和构建 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 -v 检查 ruby​​ 是否已经安装了正确的版本。我如何在我的剧本中定义这是运行此步骤的条件?

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

最佳答案

可以使用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

关于ruby - 可靠的 : define when a command doesn't have to run (building ruby from source),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19878066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com