[2.7] fix error checking if netns exists (#47397) (#47441)

* [2.7] fix error checking if netns exists (#47397)

This patch fixes an error that occurs when attempting to see if the
netns already exists on the remote device.  This change will now execute
`ip netns list` and check if the desired namespace is in the output.

Signed-off-by: Peter Sprygada <psprygada@ansible.com>
(cherry picked from commit 299a5e4)

Co-authored-by: Peter Sprygada <privateip@users.noreply.github.com>

* Add changelog entry
This commit is contained in:
Nathaniel Case 2018-10-23 10:16:10 -04:00 committed by Toshio Kuratomi
parent b5a887aa5e
commit 238939b523
2 changed files with 10 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
bugfixes:
- Fixes an error that occurs when attempting to see if the netns already exists
on the remote device. This change will now execute ``ip netns list`` and check
if the desired namespace is in the output.

View file

@ -61,6 +61,7 @@ RETURN = '''
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text
class Namespace(object):
@ -77,9 +78,10 @@ class Namespace(object):
def exists(self):
'''Check if the namespace already exists'''
rtc, out, err = self._netns(['exec', self.name, 'ls'])
if rtc != 0:
self.module.fail_json(msg=err)
rc, out, err = self.module.run_command('ip netns list')
if rc != 0:
self.module.fail_json(msg=to_text(err))
return self.name in out
def add(self):
'''Create network namespace'''