mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-12 15:45:20 +01:00
lock fstab with ansible's FileLock to avoid race condition
This commit is contained in:
parent
ceddb849b8
commit
6181b4d784
2 changed files with 106 additions and 88 deletions
|
|
@ -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).
|
||||||
|
|
@ -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.six import iteritems
|
||||||
from ansible.module_utils._text import to_bytes, to_native
|
from ansible.module_utils._text import to_bytes, to_native
|
||||||
from ansible.module_utils.parsing.convert_bool import boolean
|
from ansible.module_utils.parsing.convert_bool import boolean
|
||||||
|
from ansible.module_utils.common.file import FileLock, LockTimeout
|
||||||
|
|
||||||
|
|
||||||
def write_fstab(module, lines, path):
|
def write_fstab(module, lines, path):
|
||||||
|
|
@ -763,6 +764,14 @@ def main():
|
||||||
name = module.params['path']
|
name = module.params['path']
|
||||||
changed = False
|
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':
|
if state == 'absent':
|
||||||
name, changed = unset_mount(module, args)
|
name, changed = unset_mount(module, args)
|
||||||
|
|
||||||
|
|
@ -863,6 +872,9 @@ def main():
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg='Unexpected position reached')
|
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
|
# If the managed node is Solaris, convert the boot value type to Boolean
|
||||||
# to match the type of return value with the module argument.
|
# to match the type of return value with the module argument.
|
||||||
if platform.system().lower() == 'sunos':
|
if platform.system().lower() == 'sunos':
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue