From 8682cb64b86f828b6174ee9e4a182dd15e47d0c1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 7 Feb 2017 16:24:07 +0100 Subject: [PATCH] module: support a 'ignore_errors' argument It's more idiomatic for ansible then "on_error". 'ignore_errors' can be specified as a module argument. But it can also be specified on a per-profile level, with the intuitive behavior that the per-profile setting overwrites the per-module setting. --- library/network_connections.py | 24 +++++++++++++++++------- library/test_network_connections.py | 12 +++++------- tasks/main.yml | 1 + 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/library/network_connections.py b/library/network_connections.py index af0fe35..5ee440e 100755 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -460,7 +460,7 @@ class ArgValidator_DictConnection(ArgValidatorDict): ArgValidatorStr ('mac'), ArgValidatorStr ('parent'), ArgValidatorInt ('vlan_id', val_min = 0, val_max = 4095, default_value = None), - ArgValidatorStr ('on_error', enum_values = ['continue', 'fail'], default_value = 'fail'), + ArgValidatorBool('ignore_errors', default_value = None), ArgValidator_DictIP(), ], default_value = dict, @@ -481,9 +481,9 @@ class ArgValidator_DictConnection(ArgValidatorDict): if result['state'] == 'present': VALID_FIELDS.remove('wait') elif result['state'] in ['up', 'down']: - VALID_FIELDS = ['name', 'state', 'wait', 'on_error'] + VALID_FIELDS = ['name', 'state', 'wait', 'ignore_errors'] elif result['state'] == 'absent': - VALID_FIELDS = ['name', 'state', 'on_error'] + VALID_FIELDS = ['name', 'state', 'ignore_errors'] elif result['state'] == 'wait': VALID_FIELDS = ['state', 'wait'] else: @@ -1182,8 +1182,9 @@ class NMUtil: class _AnsibleUtil: ARGS = { - 'provider': { 'required': True, 'default': None, 'type': 'str' }, - 'connections': { 'required': False, 'default': None, 'type': 'list' }, + 'ignore_errors': { 'required': False, 'default': False, 'type': 'str' }, + 'provider': { 'required': True, 'default': None, 'type': 'str' }, + 'connections': { 'required': False, 'default': None, 'type': 'list' }, } ARGS_CONNECTIONS = ArgValidator_ListConnections() @@ -1283,8 +1284,17 @@ class _AnsibleUtil: def log(self, idx, severity, msg, warn_traceback = False): self.run_results[idx]['log'].append((severity, msg)) - if severity == LogLevel.ERROR and self.connections[idx]['on_error'] != 'continue': - self.fail_json('error: %s' % (msg), warn_traceback = warn_traceback) + if severity == LogLevel.ERROR: + # ignore_errors can be specified per profile. In absense of a + # per-profile setting, a global parameter is consulted. + ignore_errors = self.connections[idx]['ignore_errors'] + if ignore_errors is None: + try: + ignore_errors = Util.boolean(self.params['ignore_errors']) + except: + ignore_errors = None + if not ignore_errors: + self.fail_json('error: %s' % (msg), warn_traceback = warn_traceback) def _complete_kwargs(self, kwargs, traceback_msg = None): if 'warnings' in kwargs: diff --git a/library/test_network_connections.py b/library/test_network_connections.py index 056533b..19a017d 100755 --- a/library/test_network_connections.py +++ b/library/test_network_connections.py @@ -130,8 +130,7 @@ class TestValidator(unittest.TestCase): 'mac': None, 'master': None, 'vlan_id': None, - 'on_error': - 'fail', + 'ignore_errors': None, 'interface_name': None, 'slave_type': None, }, @@ -139,7 +138,7 @@ class TestValidator(unittest.TestCase): 'name': '5', 'state': 'up', 'wait': 90, - 'on_error': 'fail', + 'ignore_errors': None, } ], n.AnsibleUtil.ARGS_CONNECTIONS.validate([ @@ -171,8 +170,7 @@ class TestValidator(unittest.TestCase): 'mac': None, 'master': None, 'vlan_id': None, - 'on_error': - 'fail', + 'ignore_errors': None, 'interface_name': None, 'slave_type': None, 'wait': 90, @@ -194,7 +192,7 @@ class TestValidator(unittest.TestCase): { 'name': '5', 'state': 'absent', - 'on_error': 'fail', + 'ignore_errors': None, } ], n.AnsibleUtil.ARGS_CONNECTIONS.validate([ @@ -230,7 +228,7 @@ class TestValidator(unittest.TestCase): 'master': None, 'name': '5', 'parent': None, - 'on_error': 'fail', + 'ignore_errors': None, 'slave_type': None, 'state': 'present', 'type': 'ethernet', diff --git a/tasks/main.yml b/tasks/main.yml index 2a26409..8ae5fbb 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -15,6 +15,7 @@ - name: Configure networking connection profiles network_connections: provider: "{{ network.provider | default(network_provider) | mandatory }}" + ignore_errors: "{{ network.ignore_errors | default(omit) }} " connections: "{{ network.connections | default([]) }}" - name: Re-test connectivity