Last active
July 17, 2019 11:33
-
-
Save kvaps/c16af57b57bfcd55afc3b4ccdd3cc287 to your computer and use it in GitHub Desktop.
ansible raw module with locking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: no | |
tasks: | |
- raw: uname -a | |
args: | |
lock_file: '/tmp/asd' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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