1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Replace use of subprocess.Popen with module.run_command

This commit is contained in:
Jon Ellis 2022-06-06 22:35:31 +01:00
parent 8c16daf766
commit a76f2d175e

View file

@ -117,6 +117,8 @@ from ansible.module_utils.common.text.converters import to_native
class Sudoers(object):
def __init__(self, module):
self.module = module
self.check_mode = module.check_mode
self.name = module.params['name']
self.user = module.params['user']
@ -162,14 +164,10 @@ class Sudoers(object):
return "{owner} ALL={runas}{nopasswd} {commands}\n".format(owner=owner, runas=runas_str, nopasswd=nopasswd_str, commands=commands_str)
def validate(self):
# fork to visudo
content = bytes(self.content(), 'utf-8')
check_command = ['visudo', '-c', '-f', '-']
rc, stdout, stderr = self.module.run_command(check_command, data=self.content())
check = subprocess.Popen(check_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = check.communicate(input=content)
if check.returncode != 0:
if rc != 0:
raise Exception('Failed to validate sudoers rule:\n{stdout}'.format(stdout=stdout))
def run(self):