lock fstab with ansible's FileLock to avoid race condition

This commit is contained in:
quidame 2021-03-30 06:13:44 +02:00
parent ceddb849b8
commit 6181b4d784
2 changed files with 106 additions and 88 deletions

View file

@ -0,0 +1,6 @@
---
bugfixes:
- mount - fix a race condition that might result in unknown/unexpected fstab
state when running the module on several inventory hostnames adressing the
same host. Lock it with ansible.module_utils.common.file.FileLock.lock_file()
(https://github.com/ansible-collections/ansible.posix/issues/115).

View file

@ -193,6 +193,7 @@ from ansible_collections.ansible.posix.plugins.module_utils.mount import ismount
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_bytes, to_native
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.module_utils.common.file import FileLock, LockTimeout
def write_fstab(module, lines, path):
@ -763,6 +764,14 @@ def main():
name = module.params['path']
changed = False
lock_timeout = 2
lock_tempdir = '/tmp'
lock_message = 'another instance of the module holds the %s lock' % os.path.basename(args['fstab'])
_fstab = FileLock()
try:
with _fstab.lock_file(args['fstab'], lock_tempdir, lock_timeout):
if state == 'absent':
name, changed = unset_mount(module, args)
@ -863,6 +872,9 @@ def main():
else:
module.fail_json(msg='Unexpected position reached')
except LockTimeout:
module.fail_json(msg=lock_message)
# If the managed node is Solaris, convert the boot value type to Boolean
# to match the type of return value with the module argument.
if platform.system().lower() == 'sunos':