在单个条件下对多个 ansible 任务进行分组
Group multiple ansible tasks under a single conditional
我正在使用 ansible 作为系统编排工具。首先,让我描述一下我的最终目标。
我想创建一两个剧本,从网上抓取 solr,自行设置,然后根据某些配置启动索引。如果到目前为止有任何听起来可疑,请在这里阻止我。
现在,如果没有必要,我不想抓住 solr tarball,所以我目前有一个看起来像这样的 task/play。
- stat: path="path to solr home/solr/bin/init script"
register: solr_script
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
- name: start solr
shell: "path to solr home/solr/bin/solr init script" start
sudo: yes
我试图检查是否已经下载了 solr init 脚本,然后再去获取它。
当我按原样 运行 这个脚本时,我得到了错误,
multiple actions specified in task: 'shell' and 'getting solr'
这似乎是合理的,也许格式有误?我试过这个
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
出现语法错误。
在ansible文档页面上,我看到了这一点信息。
Note that if you have several tasks that all share the same conditional statement, you can affix the conditional to a task include statement as below. All the tasks get evaluated, but the conditional is applied to each and every task:
这似乎是我需要的。
但后来他们又用这个例子跟进了。
- include: tasks/sometasks.yml
when: "'reticulating splines' in output"
我不是很明白"include",这个例子似乎也没有说明我想要的。因此,如果我最初的假设是正确的并且这确实是我想要获得 solr 的方式,我将如何编写一个有条件地执行一组任务的可靠任务。
这是无效的:
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
一个任务由一个名称(可选)、一个 task/module 和 when
、with
等修饰符组成。您不能有多个任务(在本例 shell
& get_url
) 在一个任务中。因此,您需要将其分解为多项任务:
- name: make solr directory
file: path=/path/to/download/dir"
state=directory
- name: download solr
get_url: url=<url> dest=/path/to/download/dir
- name: unpack solr
shell: tar xvzf ...
等等...
要有条件地执行一系列任务,您可以执行以下操作:
您可以使用问题中提到的 include
语句,这样您就可以对该包含文件中的所有任务应用条件。
您可以创建一个执行所有任务的角色,然后再次使用条件来调用它(或者只将该角色应用于您希望应用该角色的主机等)
您可以为每个任务单独添加 when
个子句。
您可以使用 shell
和 creates
或 removes
等其他模块的参数,这些模块将查找是否存在要使用的文件判断是否执行任务
在 Ansible 2.0 中可能是这样的:
- block:
- shell: mkdir -p "path to download dir"
- get_url: url="download url" dest= "path to download dir"
- shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
- shell: rm -rf "path to download dir"
when: solr_script.stat.exists == False
希望对您有所帮助。
对于 ansible 2.x 使用其他接受的答案。您可以使用 ansible 1.9.x 使用 include
语句来执行此操作。
首先为此创建两个 .yml
文件,main.yml
和 solr.yml
在您当前的角色目录中。
您的 main.yml
应如下所示:
- stat: path="path to solr home/solr/bin/init script"
register: solr_script
- include: solr.yml
when: solr_script.stat.exists == False
在 solr.yml
文件中包含您想要 运行 用于 main.yml
中包含条件的所有任务:
- shell: mkdir -p "path to download dir"
- get_url: url="download url" dest= "path to download dir"
- shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
- shell: rm -rf "path to download dir"
有关 include statements 的更多信息。希望这是有道理的,干杯!
我正在使用 ansible 作为系统编排工具。首先,让我描述一下我的最终目标。
我想创建一两个剧本,从网上抓取 solr,自行设置,然后根据某些配置启动索引。如果到目前为止有任何听起来可疑,请在这里阻止我。
现在,如果没有必要,我不想抓住 solr tarball,所以我目前有一个看起来像这样的 task/play。
- stat: path="path to solr home/solr/bin/init script"
register: solr_script
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
- name: start solr
shell: "path to solr home/solr/bin/solr init script" start
sudo: yes
我试图检查是否已经下载了 solr init 脚本,然后再去获取它。
当我按原样 运行 这个脚本时,我得到了错误,
multiple actions specified in task: 'shell' and 'getting solr'
这似乎是合理的,也许格式有误?我试过这个
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
出现语法错误。
在ansible文档页面上,我看到了这一点信息。
Note that if you have several tasks that all share the same conditional statement, you can affix the conditional to a task include statement as below. All the tasks get evaluated, but the conditional is applied to each and every task:
这似乎是我需要的。
但后来他们又用这个例子跟进了。
- include: tasks/sometasks.yml
when: "'reticulating splines' in output"
我不是很明白"include",这个例子似乎也没有说明我想要的。因此,如果我最初的假设是正确的并且这确实是我想要获得 solr 的方式,我将如何编写一个有条件地执行一组任务的可靠任务。
这是无效的:
- name: getting solr
when: solr_script.stat.exists == False
shell: mkdir -p "path to download dir"
get_url: url="download url" dest= "path to download dir"
shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
shell: rm -rf "path to download dir"
一个任务由一个名称(可选)、一个 task/module 和 when
、with
等修饰符组成。您不能有多个任务(在本例 shell
& get_url
) 在一个任务中。因此,您需要将其分解为多项任务:
- name: make solr directory
file: path=/path/to/download/dir"
state=directory
- name: download solr
get_url: url=<url> dest=/path/to/download/dir
- name: unpack solr
shell: tar xvzf ...
等等...
要有条件地执行一系列任务,您可以执行以下操作:
您可以使用问题中提到的
include
语句,这样您就可以对该包含文件中的所有任务应用条件。您可以创建一个执行所有任务的角色,然后再次使用条件来调用它(或者只将该角色应用于您希望应用该角色的主机等)
您可以为每个任务单独添加
when
个子句。您可以使用
shell
和creates
或removes
等其他模块的参数,这些模块将查找是否存在要使用的文件判断是否执行任务
在 Ansible 2.0 中可能是这样的:
- block:
- shell: mkdir -p "path to download dir"
- get_url: url="download url" dest= "path to download dir"
- shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
- shell: rm -rf "path to download dir"
when: solr_script.stat.exists == False
希望对您有所帮助。
对于 ansible 2.x 使用其他接受的答案。您可以使用 ansible 1.9.x 使用 include
语句来执行此操作。
首先为此创建两个 .yml
文件,main.yml
和 solr.yml
在您当前的角色目录中。
您的 main.yml
应如下所示:
- stat: path="path to solr home/solr/bin/init script"
register: solr_script
- include: solr.yml
when: solr_script.stat.exists == False
在 solr.yml
文件中包含您想要 运行 用于 main.yml
中包含条件的所有任务:
- shell: mkdir -p "path to download dir"
- get_url: url="download url" dest= "path to download dir"
- shell: tar zxvf "path to download dir/tgz file" -C "path to solr home"
- shell: rm -rf "path to download dir"
有关 include statements 的更多信息。希望这是有道理的,干杯!