# When condition not working in a list

**URL:** <https://forum.ansible.com/t/when-condition-not-working-in-a-list/31410>\
**Category:** Ansible Project\
**Created:** [October 3, 2019, 7:44am UTC](https://forum.ansible.com/t/when-condition-not-working-in-a-list/31410 "2019-10-03T07:44:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [October 3, 2019, 7:44am UTC](https://forum.ansible.com/t/when-condition-not-working-in-a-list/31410/1 "2019-10-03T07:44:58Z")

</div>

Hi,

I have yaml file as below. I parse the component and version and put the parsed value to a file by creating a list

deliveries:

- releases:
- latest:
- tmoAa:  
file\_name: CXP\_902\_7801\_28.1.0\_20191001-024646.tar  
md5: f08a50fcb3ec79c87ec870195568826e  
product\_name: tmoAa  
product\_number: CXP 902 7801  
release\_candidate: RC28  
version: 28.1.0
- permission:  
file\_name: CXP\_902\_8059\_27.0.0\_20190904-084538.tar  
md5: c92d543887c332eaaa594dcca598c07a  
product\_name: permission  
product\_number: CXP 902 8059  
release\_candidate: RC27  
version: 27.0.0
- eiil:  
file\_name: CXP\_902\_8881\_19.0.0\_20180914-142119.tar  
md5: 539f67d4eae511bbff524ec61848eaaf  
product\_name: captcha  
product\_number: CXP 902 8881  
release\_candidate: RC19  
version: 19.0.0

Now when the component(eiil) is present in my above file my playbook works fine but when the component is not present my playbook fails. Kindly help.

- hosts: localhost  
vars:  
job\_id: 43  
eiil\_component\_dest: “/tmp/{{ job\_id }}-eiil.txt”

tasks:

- name: clean up files  
file:  
path: “{{ item }}”  
state: absent  
with\_items:

- “{{ eiil\_component\_dest }}”

- name: display job id  
debug:  
msg: “{{ job\_id }}”

- name: set file as variable  
include\_vars:  
file: “{{ INV\_FILE }}”  
name: x

- name: Set Fact for the Drop name  
set\_fact:  
drop\_name: “{{ x[‘deliveries’][0][‘releases’][0].keys()[0] }}”  
with\_dict: “{{ x }}”

- name: Set fact for Pcakage  
set\_fact:  
listvalue: “{{ x[‘deliveries’][0][‘releases’][0][drop\_name] }}”

- name: Authorize Module Component name and its respective version  
set\_fact:  
authorize: “authorize:{{ item.value.version }}”  
when: item.key is defined and item.key == ‘tmoAa’  
with\_dict: “{{ listvalue }}”

- name: Permission Module Component name and its respective version  
set\_fact:  
permission: “permission:{{ item.value.version }}”  
when: item.key is defined and item.key == ‘permission’  
with\_dict: “{{ listvalue }}”

- name: EIIL Module Component name and its respective version  
set\_fact:  
eiil: “eiil:{{ item.value.version }}”  
when: item.key is defined and item.key == ‘EIIL\_adaptor’  
with\_dict: “{{ listvalue }}”

- name: Create an empty list variable for EIIL Component  
set\_fact:  
EIIL\_Record: 

- name: Append string to list  
set\_fact:  
EIIL\_Record: “{{ EIIL\_Record + [item] }}”  
with\_items:

- “{{ eiil | default(None) }}”  
when: item != None item == “”

- name: Create EIIL items list  
set\_fact:  
my\_joined\_list1: “{{ EIIL\_Record | join(‘,’) }}”  
with\_items:

- “{{ EIIL\_Record }}”

- name: Create EIIL Components  
set\_fact:  
eiil\_c: “‘{"components":"{{ my\_joined\_list1 }}"}’”

- name: Copy the EIIL Components to a file  
copy:  
dest: “{{ eiil\_component\_dest }}”  
content: “{{ eiil\_c }}”
