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

Check file mode when verifying file to determine whether something needs to change

This commit is contained in:
Jon Ellis 2022-06-07 22:19:22 +01:00
parent ed3bb0b646
commit 3d864070cb

View file

@ -109,13 +109,15 @@ EXAMPLES = '''
''' '''
import os import os
import subprocess import stat
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
class Sudoers(object): class Sudoers(object):
FILE_MODE = 0o440
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
@ -137,7 +139,7 @@ class Sudoers(object):
with open(self.file, 'w') as f: with open(self.file, 'w') as f:
f.write(self.content()) f.write(self.content())
os.chmod(self.file, 0o440) os.chmod(self.file, self.FILE_MODE)
def delete(self): def delete(self):
if self.check_mode: if self.check_mode:
@ -150,7 +152,12 @@ class Sudoers(object):
def matches(self): def matches(self):
with open(self.file, 'r') as f: with open(self.file, 'r') as f:
return f.read() == self.content() content_matches = f.read() == self.content()
current_mode = oct(os.stat(self.file).st_mode & 0o777)
mode_matches = current_mode == oct(self.FILE_MODE)
return content_matches and mode_matches
def content(self): def content(self):
if self.user: if self.user: