Verified Commit 4677412e authored by Florian Pritz's avatar Florian Pritz
Browse files

dbscripts: Add module for staging directory creation



Signed-off-by: Florian Pritz's avatarFlorian Pritz <bluewind@xinu.at>
parent b1cf08b9
[defaults]
inventory = hosts
library = library
remote_tmp = $HOME/.ansible/tmp
transport = smart
remote_port = 22
......
#!/usr/bin/python
import errno
import grp
import os
import pwd
from stat import *
# simple module that creates many directories for users
# initially created for dbscripts to create staging directories in the user homes
def main():
module = AnsibleModule(
argument_spec = dict(
permissions = dict(required=True),
users = dict(required=True, type='list'),
group = dict(required=True),
directories = dict(required=True, type='list'),
pathtmpl = dict(required=True),
),
supports_check_mode=True,
)
users = module.params['users']
directories = module.params['directories']
permissions = int(module.params['permissions'], 8)
pathtmpl = module.params['pathtmpl']
group = module.params['group']
gid = grp.getgrnam(group).gr_gid
changed = 0
changed_dirs = []
for user in users:
uid = pwd.getpwnam(user).pw_uid
for dirname in directories:
path = pathtmpl.format(**{"user": user, "dirname": dirname})
permissions_incorrect = True
dirmode = None
if os.path.exists(path):
stat = os.stat(path)
dirmode = oct(stat.st_mode & 0777)
diruid = stat.st_uid
dirgid = stat.st_gid
permissions_incorrect = diruid != uid or dirgid != gid
if not os.path.isdir(path) or dirmode != oct(permissions) or permissions_incorrect:
changed += 1
changed_dirs.append(path)
if not module.check_mode:
try:
try:
os.mkdir(path, permissions)
except OSError as ex:
if not (ex.errno == errno.EEXIST and os.path.isdir(path)):
raise
except Exception as e:
module.fail_json(path=path, msg='There was an issue creating %s as requested: %s' % (path, str(e)))
os.chmod(path, permissions)
os.chown(path, uid, gid)
module.exit_json(changed=changed > 0, msg="%s directories changed" % (changed), changed_dirs=changed_dirs)
from ansible.module_utils.basic import AnsibleModule
if __name__ == '__main__':
main()
......@@ -57,10 +57,12 @@
tags: ['archusers']
- name: create staging directories in user homes
file: path=/home/{{item[0]}}/staging/{{item[1]}} state=directory owner={{item[0]}} group=users mode=0755
with_nested:
- "{{arch_users}}"
- ['core', 'extra', 'testing', 'staging', 'community', 'community-staging', 'community-testing', 'multilib', 'multilib-staging', 'multilib-testing']
dbscripts_mkdirs:
pathtmpl: '/home/{user}/staging/{dirname}'
permissions: 755
directories: ['', 'core', 'extra', 'testing', 'staging', 'community', 'community-staging', 'community-testing', 'multilib', 'multilib-staging', 'multilib-testing']
users: "{{arch_users.keys()}}"
group: users
tags: ["archusers"]
- name: create dbscripts paths
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment