From 733d40a77c34998bf3bad3969bcd335b21baba44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannig=20Perr=C3=A9?= Date: Wed, 5 Aug 2015 10:49:41 +0200 Subject: [PATCH] When value does not exist, return default value instead of stopping ansible with an exception. --- lib/ansible/runner/lookup_plugins/ini.py | 6 ++++-- test/integration/test_lookup_properties.yml | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner/lookup_plugins/ini.py b/lib/ansible/runner/lookup_plugins/ini.py index 002dda09089..ebfed750c2d 100644 --- a/lib/ansible/runner/lookup_plugins/ini.py +++ b/lib/ansible/runner/lookup_plugins/ini.py @@ -43,9 +43,11 @@ class LookupModule(object): # Retrieve all values from a section using a regexp if is_regexp: return [v for k, v in self.cp.items(section) if re.match(key, k)] + value = None # Retrieve a single value - value = self.cp.get(section, key) - if value == None: + try: + value = self.cp.get(section, key) + except ConfigParser.NoOptionError, e: return dflt return value diff --git a/test/integration/test_lookup_properties.yml b/test/integration/test_lookup_properties.yml index dcd5eb698b2..4e085c14978 100644 --- a/test/integration/test_lookup_properties.yml +++ b/test/integration/test_lookup_properties.yml @@ -26,4 +26,5 @@ with_items: [ 'value_section', 'other_section' ] - name: "Reading unknown value" set_fact: - value2_section2: "{{lookup('ini', 'value2 section=section1 file=lookup.ini')}}" + unknown: "{{lookup('ini', 'value2 default=unknown section=section1 file=lookup.ini')}}" + - debug: var=unknown