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

Creates temporary files and directories

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

Authors: Krzysztof Magosa (@krzysztof-magosa)

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

The M(ansible.builtin.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(ansible.builtin.file) module.

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

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create temporary build directory
  ansible.builtin.tempfile:
    state: directory
    suffix: build
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create temporary file
  ansible.builtin.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
  ansible.builtin.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