When condition not working in a list

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 }}”