how to verify all parameters are set and different from space in a csv file ?

51 views
Skip to first unread message

fanvalt

unread,
Sep 27, 2016, 5:44:40 AM9/27/16
to Ansible Project
Hello,

I read a csv file with the lookup command with a loop on the items:

   tasks:
     - set_fact:
         "{{ item.name }}": "{{ lookup('csvfile', '{{ item.csvvar }} file=vars/fvaltat.csv delimiter=; col=1') }}"
       with_items:
         - { name: 'KARAFHTTPTIMEOUT', csvvar: 'TOTO' }
         - { name: 'dbType', csvvar: 'DBTYPE' }

How to check the variable does exist and is different from space
I tried without success a fail ... when :
when: item.0 is not defined
when: item.name is not defined
when: item.name =='[]'
I tried to use a register variable but I cannot find the way to test the register variable content.

Thanks for your help

fanvalt

unread,
Sep 27, 2016, 6:10:13 AM9/27/16
to Ansible Project
The purpose is that the playbook fails if some parameters are not defined in the csv file

fanvalt

unread,
Sep 27, 2016, 6:44:53 AM9/27/16
to Ansible Project
I understand the |default([]) can be used to run a loop only if the item is not an empty list, 
But I can't find a way to proceed to quit the playbook if one parameter is missing:

vars: booleen: False
- set_facts:
         booleen: True
  with_items:
         - "{{ KARAFHTTPTIMEOUT|default([]) }}"
         - "{{ dbType|default([]) }}"

if all parameters are empty, booleen is False and I can use fail: ... when booleen = False
But if only one parameter is missing, booleen is true.

Thanks for helping: my purpose is to verify that all parameters are set in a csv file and to fail of one is missing.

fanvalt

unread,
Sep 27, 2016, 7:08:36 AM9/27/16
to Ansible Project
I found a "bypass" way to proceed, but any suggestion is welcome:
     - set_fact:
         compte: "{{ compte|int +1 }} "
       with_items:
         - "{{ KARAFHTTPTIMEOUT|default([]) }}"
         - "{{ dbType }}"
     - fail:
         msg: "{{ 2 - compte|int}} parameters are missing"
       when: compte != 2

fanvalt

unread,
Sep 27, 2016, 7:27:42 AM9/27/16
to Ansible Project
correcting the when clause: 
when: compte|int != 2

fanvalt

unread,
Sep 27, 2016, 7:57:23 AM9/27/16
to Ansible Project
Finally this is still not OK as this bypass solution only works for ampty parameters.
if parameters are set but not intialized, or set to space, this is not ok






Le mardi 27 septembre 2016 11:44:40 UTC+2, fanvalt a écrit :

fanvalt

unread,
Sep 27, 2016, 8:38:56 AM9/27/16
to Ansible Project
I had before counting for the parameters set:
- name: Si les parametres existent mais non renseignes, on les declare empty
  set_fact:
    "{{ item }}" : '[]'
  with_items:
    - KARAFHTTPTIMEOUT
    - dbType
  when: "{{ item }} is none or {{ item }}|trim == ''"
Reply all
Reply to author
Forward
0 new messages