|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
#coding: utf-8 -*-
|
|
|
|
|
# Copyright: (c) 2016 Krzysztof Magosa <krzysztof@magosa.pl>
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# Copyright: (c) 2016, Krzysztof Magosa <krzysztof@magosa.pl>
|
|
|
|
|
# Copyright: (c) 2017, Ansible Project
|
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
|
@ -11,7 +12,6 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
|
'status': ['preview'],
|
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
|
---
|
|
|
|
|
module: tempfile
|
|
|
|
|
@ -28,23 +28,18 @@ options:
|
|
|
|
|
state:
|
|
|
|
|
description:
|
|
|
|
|
- Whether to create file or directory.
|
|
|
|
|
required: false
|
|
|
|
|
choices: [ "file", "directory" ]
|
|
|
|
|
choices: [ directory, file ]
|
|
|
|
|
default: file
|
|
|
|
|
path:
|
|
|
|
|
description:
|
|
|
|
|
- Location where temporary file or directory should be created. If path is not specified default system temporary directory will be used.
|
|
|
|
|
required: false
|
|
|
|
|
default: null
|
|
|
|
|
prefix:
|
|
|
|
|
description:
|
|
|
|
|
- Prefix of file/directory name created by module.
|
|
|
|
|
required: false
|
|
|
|
|
default: ansible.
|
|
|
|
|
suffix:
|
|
|
|
|
description:
|
|
|
|
|
- Suffix of file/directory name created by module.
|
|
|
|
|
required: false
|
|
|
|
|
default: ""
|
|
|
|
|
notes:
|
|
|
|
|
- For Windows targets, use the M(win_tempfile) module instead.
|
|
|
|
|
@ -73,17 +68,19 @@ path:
|
|
|
|
|
from os import close
|
|
|
|
|
from tempfile import mkstemp, mkdtemp
|
|
|
|
|
from traceback import format_exc
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
|
argument_spec = dict(
|
|
|
|
|
state = dict(default='file', choices=['file', 'directory']),
|
|
|
|
|
path = dict(default=None),
|
|
|
|
|
prefix = dict(default='ansible.'),
|
|
|
|
|
suffix = dict(default='')
|
|
|
|
|
)
|
|
|
|
|
argument_spec=dict(
|
|
|
|
|
state=dict(type='str', default='file', choices=['file', 'directory']),
|
|
|
|
|
path=dict(type='path'),
|
|
|
|
|
prefix=dict(type='str', default='ansible.'),
|
|
|
|
|
suffix=dict(type='str', default=''),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
@ -91,19 +88,20 @@ def main():
|
|
|
|
|
handle, path = mkstemp(
|
|
|
|
|
prefix=module.params['prefix'],
|
|
|
|
|
suffix=module.params['suffix'],
|
|
|
|
|
dir=module.params['path']
|
|
|
|
|
dir=module.params['path'],
|
|
|
|
|
)
|
|
|
|
|
close(handle)
|
|
|
|
|
elif module.params['state'] == 'directory':
|
|
|
|
|
path = mkdtemp(
|
|
|
|
|
prefix=module.params['prefix'],
|
|
|
|
|
suffix=module.params['suffix'],
|
|
|
|
|
dir=module.params['path']
|
|
|
|
|
dir=module.params['path'],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=True, path=path)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json(msg=to_native(e), exception=format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|
|
|
|
|
|