| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2016, Krzysztof Magosa <[email protected]> |
| 4 |
# Copyright: (c) 2017, Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
--- |
| 12 |
module: tempfile |
| 13 |
version_added: "2.3" |
| 14 |
short_description: Creates temporary files and directories |
| 15 |
description: |
| 16 |
- The M(ansible.builtin.tempfile) module creates temporary files and directories. C(mktemp) command |
| 17 |
takes different parameters on various systems, this module helps to avoid troubles related to that. |
| 18 |
Files/directories created by module are accessible only by creator. In case you need to make them |
| 19 |
world-accessible you need to use M(ansible.builtin.file) module. |
| 20 |
- For Windows targets, use the M(ansible.windows.win_tempfile) module instead. |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Whether to create file or directory. |
| 25 |
type: str |
| 26 |
choices: [ directory, file ] |
| 27 |
default: file |
| 28 |
path: |
| 29 |
description: |
| 30 |
- Location where temporary file or directory should be created. |
| 31 |
- If path is not specified, the default system temporary directory will be used. |
| 32 |
type: path |
| 33 |
prefix: |
| 34 |
description: |
| 35 |
- Prefix of file/directory name created by module. |
| 36 |
type: str |
| 37 |
default: ansible. |
| 38 |
suffix: |
| 39 |
description: |
| 40 |
- Suffix of file/directory name created by module. |
| 41 |
type: str |
| 42 |
default: "" |
| 43 |
extends_documentation_fragment: action_common_attributes |
| 44 |
attributes: |
| 45 |
check_mode: |
| 46 |
support: none |
| 47 |
diff_mode: |
| 48 |
support: none |
| 49 |
platform: |
| 50 |
platforms: posix |
| 51 |
seealso: |
| 52 |
- module: ansible.builtin.file |
| 53 |
- module: ansible.windows.win_tempfile |
| 54 |
author: |
| 55 |
- Krzysztof Magosa (@krzysztof-magosa) |
| 56 |
notes: |
| 57 |
- O(prefix) and O(suffix) must be file name components and must not contain path separators. |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = """ |
| 61 |
- name: Create temporary build directory |
| 62 |
ansible.builtin.tempfile: |
| 63 |
state: directory |
| 64 |
suffix: build |
| 65 |
|
| 66 |
- name: Create temporary file |
| 67 |
ansible.builtin.tempfile: |
| 68 |
state: file |
| 69 |
suffix: temp |
| 70 |
register: tempfile_1 |
| 71 |
|
| 72 |
- name: Create a temporary file with a specific prefix |
| 73 |
ansible.builtin.tempfile: |
| 74 |
state: file |
| 75 |
suffix: txt |
| 76 |
prefix: myfile_ |
| 77 |
|
| 78 |
- name: Use the registered var and the file module to remove the temporary file |
| 79 |
ansible.builtin.file: |
| 80 |
path: "{{ tempfile_1.path }}" |
| 81 |
state: absent |
| 82 |
when: tempfile_1.path is defined |
| 83 |
""" |
| 84 |
|
| 85 |
RETURN = """ |
| 86 |
path: |
| 87 |
description: Path to created file or directory. |
| 88 |
returned: success |
| 89 |
type: str |
| 90 |
sample: "/tmp/ansible.bMlvdk" |
| 91 |
""" |
| 92 |
|
| 93 |
import os |
| 94 |
from tempfile import mkstemp, mkdtemp |
| 95 |
|
| 96 |
from ansible.module_utils.basic import AnsibleModule |
| 97 |
from ansible.module_utils.common.text.converters import to_native |
| 98 |
|
| 99 |
|
| 100 |
def main(): |
| 101 |
module = AnsibleModule( |
| 102 |
argument_spec=dict( |
| 103 |
state=dict(type='str', default='file', choices=['file', 'directory']), |
| 104 |
path=dict(type='path'), |
| 105 |
prefix=dict(type='str', default='ansible.'), |
| 106 |
suffix=dict(type='str', default=''), |
| 107 |
), |
| 108 |
) |
| 109 |
|
| 110 |
prefix = module.params['prefix'] |
| 111 |
suffix = module.params['suffix'] |
| 112 |
|
| 113 |
if prefix and prefix != os.path.basename(prefix): |
| 114 |
module.fail_json(msg=f"prefix must be a valid file name component, got {prefix!r}") |
| 115 |
if suffix and suffix != os.path.basename(suffix): |
| 116 |
module.fail_json(msg=f"suffix must be a valid file name component, got {suffix!r}") |
| 117 |
|
| 118 |
try: |
| 119 |
if module.params['state'] == 'file': |
| 120 |
handle, path = mkstemp( |
| 121 |
prefix=prefix, |
| 122 |
suffix=suffix, |
| 123 |
dir=module.params['path'], |
| 124 |
) |
| 125 |
os.close(handle) |
| 126 |
else: |
| 127 |
path = mkdtemp( |
| 128 |
prefix=prefix, |
| 129 |
suffix=suffix, |
| 130 |
dir=module.params['path'], |
| 131 |
) |
| 132 |
|
| 133 |
module.exit_json(changed=True, path=path) |
| 134 |
except Exception as e: |
| 135 |
module.fail_json(msg=to_native(e)) |
| 136 |
|
| 137 |
|
| 138 |
if __name__ == '__main__': |
| 139 |
main() |