# lookup and default value

**URL:** <https://forum.ansible.com/t/lookup-and-default-value/22511>\
**Category:** Ansible Project\
**Created:** [September 8, 2016, 12:40pm UTC](https://forum.ansible.com/t/lookup-and-default-value/22511 "2016-09-08T12:40:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 8, 2016, 12:40pm UTC](https://forum.ansible.com/t/lookup-and-default-value/22511/1 "2016-09-08T12:40:33Z")

</div>

Hello,

Is there a way to set a default value when using the lookup module for a csv file.

Case 1:

Here is the csv file:

OPENHRPORT;;;;;;

I would like to set the ‘1’ value if the value in the csv file is not specified:

vars:

- port: “{{ lookup(‘csvfile’, ‘OPENHRPORT file=vars/test.csv delimiter=; col=1’) | default(‘1’) }}”  
tasks:
- debug: msg=“port openhr {{ port }}”

But it returns the port varibale set to blank or unset, I would like it set to 1

“msg”: "port openhr "

Case 2:  
the csv file has only one key set:

OPENHRPORT;;;;;;

And I want to lookup at another key and set a default value if the key is not present in the csv file:

vars:

- port: “{{ lookup(‘csvfile’, ‘OPDFT file=vars/test.csv delimiter=; col=1’) | default(‘1’) }}”  
tasks:
- debug: msg=“port openhr {{ port }}”

But it returns in the list bracket , I would expect it set to 1

“msg”: “port openhr ”

Could you give me any help on this lookup module and default value ?

Regards,

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 8, 2016, 1:23pm UTC](https://forum.ansible.com/t/lookup-and-default-value/22511/2 "2016-09-08T13:23:12Z")

</div>

I found out how to bypass the issue for case 1 by using:

- set\_fact: port=‘1’  
when: port is not defined or port ==‘’

But it does not bypass the issue in case 2.  
if port is , empty list, what value of port should I test ?

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 8, 2016, 1:25pm UTC](https://forum.ansible.com/t/lookup-and-default-value/22511/3 "2016-09-08T13:25:58Z")

</div>

I did find out the way to bypass for the 2 cases:

- set\_fact: port=‘1’  
when: port.0 is not defined or port ==‘’

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 22, 2016, 7:20am UTC](https://forum.ansible.com/t/lookup-and-default-value/22511/4 "2016-09-22T07:20:45Z")

</div>

I cannot proceed that way when using a with\_items loop.  
I want to read parameters from a .csv file, and if the parameter does not exist , I want to set it to space.  
i guess the issue is that instead of comparing the parameter value to an empty list, it does test if the parameter name is empty.  
Where am I doing wrong ?

- set\_fact:  
“{{ item.name }}”: “{{ lookup(‘csvfile’, ‘{{ item.csvvar }} file=vars/{{ ansible\_user }}.csv delimiter=; col={{ numinst }}’) }}”  
with\_items:

- { name: ‘KARAFHTTPTIMEOUT’, csvvar: ‘KARAFHTTPTIMEOUT’ }

- { name: ‘dbType’, csvvar: ‘DBTYPE’ }

- name: if empty set it to space  
set\_fact:  
“{{ item }}”: " "  
with\_items:

- KARAFHTTPTIMEOUT

- dbType

when: item.0 is not defined

Regards
