在循环中使用with_items的Ansible 2条件 [英] Ansible 2 conditionals using with_items in loop
问题描述
我试图根据布尔值的存在,根据任务中条件检查的结果来运行一些Ansible命令,并试图将其束缚起来以使其正常工作.
I'm trying to run a few Ansible commands depending on the outcome of a conditional check in my tasks based on the presence of a boolean and am tying myself up in knots trying to get it to work.
期望的结果如下,并且应该为hosts
数组中的每个项目运行 :
The desired outcome is as follows, and should be run for each item in the hosts
array:
- 检查
lynchburg
变量是true
还是false
- 如果
lynchburg
变量是true
:- 设置文件夹结构(如果尚不存在)
- 从随附的模板文件创建
gulpfile.js
(如果尚不存在)
- Check if the
lynchburg
variable istrue
orfalse
- If the
lynchburg
variable istrue
:- Setup the folder structure if it doesn't already exist
- Create
gulpfile.js
from the included template file if it doesn't already exist
- 不设置文件夹结构
- 不要创建
gulpfile.js
从下面包含的代码中,我看到以下结果:
From the code included below, I'm seeing the following results:
- 当
lynchburg
为true
时,设置按预期进行 第一次以及以后的任何重新配置也可以作为 预期的. - 当
lynchburg
为false
时,会创建inc
文件夹结构 ,但是不会创建gulpfile.js
.
- When
lynchburg
istrue
, the provision runs through as expected the first time around and any subsequent reprovisions also work as expected. - When
lynchburg
isfalse
, theinc
folder structure is created, howevergulpfile.js
isn't.
任务应运行的主要循环如下:
The main loop the task should run through is the following:
vhosts: - site_name: sample lynchburg: true
,任务如下:
# Create variable to check whether Lynchburg has already been installed - name: Check if Lynchburg assets folders have been previously created stat: path: /var/www/{{ item.site_name }}/inc with_items: "{{ vhosts }}" when: item.lynchburg == true register: lynchburg_assets - name: Check if Lynchburg gulpfile.js has been previously created stat: path: /var/www/{{ item.site_name }}/gulpfile.js with_items: "{{ vhosts }}" when: item.lynchburg == true register: lynchburg_gulpfile - name: Create inc folder with_items: "{{ lynchburg_assets.results }}" file: path: /var/www/{{ item.item.site_name }}/inc state: directory when: item.stat.isdir is undefined and item.item.lynchburg == true - name: Create scss folder with_items: "{{ lynchburg_assets.results }}" file: path: /var/www/{{ item.item.site_name }}/inc/scss state: directory when: item.stat.isdir is undefined and item.item.lynchburg == true - name: Create js folder with_items: "{{ lynchburg_assets.results }}" file: path: /var/www/{{ item.item.site_name }}/inc/js state: directory when: item.stat.isdir is undefined and item.item.lynchburg == true - name: Create img folder with_items: "{{ lynchburg_assets.results }}" file: path: /var/www/{{ item.item.site_name }}/inc/img state: directory when: item.stat.isdir is undefined and item.item.lynchburg == true - name: Create fonts folder with_items: "{{ lynchburg_assets.results }}" file: path: /var/www/{{ item.item.site_name }}/inc/fonts state: directory when: item.stat.isdir is undefined and item.item.lynchburg == true - name: Create gulpfile.js with_items: "{{ lynchburg_gulpfile.results }}" template: src: gulpfile.js dest: /var/www/{{ item.item.site_name }}/gulpfile.js when: item.stat.exists is defined and item.stat.exists == false and item.item.lynchburg == true
NB 如果还有其他方法可以轻松地创建完整的文件夹结构,而无需运行五个不同的任务-每个文件夹/子文件夹一个-有人可以提出建议,那也将不胜感激!
N.B. If there's any other way to create the full folder structure easily without running five different tasks - one for each folder/subfolder - and someone could advise what that is, that'd also be appreciated!
推荐答案
当
lynchburg
为false
时,会创建inc
文件夹结构 ,但是不会创建gulpfile.js
.When
lynchburg
isfalse
, theinc
folder structure is created, howevergulpfile.js
isn't.我知道如何解决它,但我还不知道如何解释它:
I know how to solve it, I don't know (yet) how to explain it:
在条件中更改表达式的顺序:
Change the order of expressions in the conditional:
when: item.item.lynchburg == true and item.stat.isdir is undefined
由于某些原因:
For some reason:
when: dummy.key is undefined and false
和
when: false and dummy.key is undefined
给出不同的结果.
仅当检查顶级变量(
dummy is undefined
)时,它才能按预期工作.It works as expected only when checking the top-level variable (
dummy is undefined
).这篇关于在循环中使用with_items的Ansible 2条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!