当条件未使用include语句进行评估时

时间:2014-08-18 12:39:43

标签: variables include ansible prompt

我有tikitaka3.yml(主yml文件)和tikitaka3a.yml(要包含的剧本)。

我提示用户输入一个变量,然后在任务部分我调用它,如下所示:

---
- 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条件。

为什么会这样?

1 个答案:

答案 0 :(得分:1)

当您包含Ansible任务文件时,它会将when:条件附加到所有包含的任务。这意味着即使when:条件为假,您也会看到显示的任务,但会跳过所有任务。

上面代码的一个问题是targetenv.stdout,这是一个具有正确格式的工作版本:

- 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