Skip to content

Instantly share code, notes, and snippets.

@kvaps
Last active July 17, 2019 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvaps/c16af57b57bfcd55afc3b4ccdd3cc287 to your computer and use it in GitHub Desktop.
Save kvaps/c16af57b57bfcd55afc3b4ccdd3cc287 to your computer and use it in GitHub Desktop.
ansible raw module with locking
---
- hosts: localhost
gather_facts: no
tasks:
- raw: uname -a
args:
lock_file: '/tmp/asd'
--- a/raw.py
+++ b/raw.py
@@ -18,6 +18,8 @@
__metaclass__ = type
from ansible.plugins.action import ActionBase
+import fcntl
+import time
class ActionModule(ActionBase):
@@ -39,6 +41,17 @@
return result
executable = self._task.args.get('executable', False)
+
+ lock_file = self._task.args.get('lock_file', False)
+ if lock_file:
+ x = open(lock_file, 'w+')
+ while True:
+ try:
+ fcntl.flock(x, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ break
+ except IOError as e:
+ time.sleep(0.1)
+
result.update(self._low_level_execute_command(self._task.args.get('_raw_params'), executable=executable))
result['changed'] = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment