If statement 在一个条件下对多个ansible任务进行分组
我将其用作系统编排工具。首先,让我描述一下我的最终目标 我想创建一个或两个playbook,它将从web上抓取,进行自我设置,然后根据一些配置启动索引。如果到目前为止这一切听起来有任何可疑之处,请在这里阻止我 现在,如果我没有必要的话,我不想抓住这个机会,所以我现在有一个任务/游戏,看起来像这样If statement 在一个条件下对多个ansible任务进行分组,if-statement,solr,task,conditional,ansible,If Statement,Solr,Task,Conditional,Ansible,我将其用作系统编排工具。首先,让我描述一下我的最终目标 我想创建一个或两个playbook,它将从web上抓取,进行自我设置,然后根据一些配置启动索引。如果到目前为止这一切听起来有任何可疑之处,请在这里阻止我 现在,如果我没有必要的话,我不想抓住这个机会,所以我现在有一个任务/游戏,看起来像这样 - stat: path="path to solr home/solr/bin/init script" register: solr_script - name: getting solr
- 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"
我不太理解“包含”这个词,这个例子似乎也没有说明我想要什么。因此,如果我最初的假设是正确的,并且这确实是我想要获得solr的方式,那么我将如何编写一个有条件地执行一组任务的ansible任务。这是无效的:
- 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"
一个任务由一个名称(可选)、一个任务/模块和诸如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 ...
-名称:make solr目录
文件:path=/path/to/download/dir“
state=目录
-名称:下载solr
获取url:url=dest=/path/to/download/dir
-名称:解包solr
壳牌:柏油xvzf。。。
等等
要有条件地执行一系列任务,您可以做许多事情:
include
语句,这将允许您将条件应用于该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
中有您想要为include条件运行的所有任务:
- 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"
更多内容。希望有意义,干杯!谢谢你的善意建议。我基本上还是坚持了“当“关于我所有任务的条款,这让我觉得很烦人,但最终还是奏效了。虽然我不知道创建/删除,但这似乎是我想要的,不需要额外检查文件是否存在。接受你的答案,你的列表应该能够帮助其他人。这是可能的