Khan
Khan

Reputation: 474

when condition not being evaluated with include statement

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

The include statement always comes into affect, without the when condition ever being evaluated.

Why is this happening?

Upvotes: 0

Views: 290

Answers (1)

jarv
jarv

Reputation: 5596

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.

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

Upvotes: 1

Related Questions