|
@@ -0,0 +1,81 @@
|
|
|
+#!/usr/bin/python
|
|
|
+
|
|
|
+import os
|
|
|
+from operator import xor
|
|
|
+
|
|
|
+def main():
|
|
|
+ module = AnsibleModule(
|
|
|
+ argument_spec = dict(
|
|
|
+ path = dict(required=True),
|
|
|
+ base = dict(default=""),
|
|
|
+ p_enabled = dict(default='enabled'),
|
|
|
+ p_available = dict(default='available'),
|
|
|
+ name = dict(required=True),
|
|
|
+ suffix= dict(default='.conf'),
|
|
|
+ state = dict(default='present', choices=['present', 'absent']),
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ # path: /etc/apache2 or /etc/apache2/conf-
|
|
|
+ # base: conf- or ""
|
|
|
+ # name: FILENAME or FILENAME.conf
|
|
|
+ path = module.params.get('path')
|
|
|
+ base = module.params.get('base')
|
|
|
+ p_enabled = module.params.get('p_enabled')
|
|
|
+ p_available = module.params.get('p_available')
|
|
|
+ name = module.params.get('name')
|
|
|
+ suffix = module.params.get('suffix')
|
|
|
+ state = module.params.get('state') == 'present'
|
|
|
+
|
|
|
+ # pre-checks
|
|
|
+ if os.path.isdir(path):
|
|
|
+ p_available = os.path.join(path, base + p_available)
|
|
|
+ p_enabled = os.path.join(path, base + p_enabled)
|
|
|
+ else:
|
|
|
+ p_available = path + p_available
|
|
|
+ p_enabled = path + p_enabled
|
|
|
+
|
|
|
+ for p in [p_available, p_enabled]:
|
|
|
+ if not os.path.isdir(p):
|
|
|
+ module.fail_json(msg="Directory %s not found" % p)
|
|
|
+
|
|
|
+ # define internal variables
|
|
|
+ src = os.path.join(p_available, name + suffix)
|
|
|
+ dest = os.path.join(p_enabled, name + suffix)
|
|
|
+
|
|
|
+ if not (os.path.isfile(src) or os.path.islink(src)):
|
|
|
+ module.fail_json(msg="Source not found")
|
|
|
+
|
|
|
+ dest_exists=True
|
|
|
+ if not (os.path.isfile(dest) or os.path.islink(dest)):
|
|
|
+ dest_exists=False
|
|
|
+
|
|
|
+ # if there's no difference, no change, exit "ok"
|
|
|
+ if not xor(state, dest_exists):
|
|
|
+ module.exit_json()
|
|
|
+
|
|
|
+ if state:
|
|
|
+ # try to create symlink
|
|
|
+ try:
|
|
|
+ os.symlink(os.path.relpath(src, p_enabled), dest)
|
|
|
+ except:
|
|
|
+ module.fail_json(msg="Could not create symlink")
|
|
|
+ else:
|
|
|
+ # test, if symlink
|
|
|
+ if not os.path.islink(dest):
|
|
|
+ module.fail_json(msg="Destination is not a symlink")
|
|
|
+
|
|
|
+ # try to remove symlink
|
|
|
+ try:
|
|
|
+ os.unlink(dest)
|
|
|
+ except:
|
|
|
+ module.fail_json(msg="Could not remove symlink")
|
|
|
+
|
|
|
+ module.exit_json(changed=True)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+from ansible.module_utils.basic import *
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|
|
|
+
|