ansible.builtin.tempfile (v2.9.27) — module

Creates temporary files and directories

| "added in version" 2.3 of ansible.builtin"

Authors: Krzysztof Magosa (@krzysztof-magosa)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.27

Description

The C(tempfile) module creates temporary files and directories. C(mktemp) command takes different parameters on various systems, this module helps to avoid troubles related to that. Files/directories created by module are accessible only by creator. In case you need to make them world-accessible you need to use M(file) module.

For Windows targets, use the M(win_tempfile) module instead.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create temporary build directory
  tempfile:
    state: directory
    suffix: build
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create temporary file
  tempfile:
    state: file
    suffix: temp
  register: tempfile_1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: use the registered var and the file module to remove the temporary file
  file:
    path: "{{ tempfile_1.path }}"
    state: absent
  when: tempfile_1.path is defined

Inputs

    
path:
    description:
    - Location where temporary file or directory should be created.
    - If path is not specified, the default system temporary directory will be used.
    type: path

state:
    choices:
    - directory
    - file
    default: file
    description:
    - Whether to create file or directory.
    type: str

prefix:
    default: ansible.
    description:
    - Prefix of file/directory name created by module.
    type: str

suffix:
    default: ''
    description:
    - Suffix of file/directory name created by module.
    type: str

Outputs

path:
  description: Path to created file or directory
  returned: success
  sample: /tmp/ansible.bMlvdk
  type: str

See also