当条件没有用include语句评估时 [英] when condition not being evaluated with include statement
问题描述
我有 tikitaka3.yml
(主yml文件)和 tikitaka3a.yml
(要包含的剧本) )。
I have a tikitaka3.yml
(main yml file) and a tikitaka3a.yml
(playbook to be included).
我提示用户输入变量,然后在任务部分我调用它,如下所示:
I prompt the user for a variable, and then in the tasks section I call it, like so:
---
- hosts: all
vars:
khan:
# contents: "{{ lookup('file', '/home/imran/Desktop/tobefetched/file1.txt') }}"
vars_prompt:
- name: targetenv
prompt: 1.)EPC 2.)CLIENTS 3)TESTERS
private: False
default: "1"
gather_facts: no
tasks:
- name: Inlude playbook tikitaka3a
include: /home/khan/Desktop/playbooks/tikitaka3a.yml target=umar
when: targetenv.stdout|int < 2 #this statement has no effect
#when: targetenv == 1 #Neither does this statement
#when: targetenc == "1" #and neither does this statement have affect
#- name: stuff n stuff # This task will give an error if not commented
# debug: var=targetenv.stdout
include语句总是生效,没有评估when条件。
The include statement always comes into affect, without the when condition ever being evaluated.
为什么会发生这种情况?
Why is this happening?
推荐答案
当您包含Ansible任务文件时,它会在以下时间附加:
所有包含任务的条件。这意味着即使在时:
条件为假,您将看到显示的任务,但是将跳过所有任务。
When you include an Ansible task file it will attach the when:
condition to all included tasks. This means that you will see the tasks displayed even when the when:
condition is false though all tasks will be skipped.
上面代码的一个问题是 targetenv.stdout
,这是一个工作版本,格式正确:
One problem with your code above is targetenv.stdout
, here is a working version with proper formatting:
- hosts: all
gather_facts: no
vars_prompt:
- name: targetenv
prompt: 1.)EPC 2.)CLIENTS 3)TESTERS
private: False
default: "1"
tasks:
- name: Inlude playbook tikitaka3a
include: roles/test/tasks/tikitaka3a.yml target=umar
when: targetenv|int < 2
这篇关于当条件没有用include语句评估时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!