Skip to content

tempfile

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 2.3.

Synopsis

  • 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.

Parameters

Parameter Defaults / Choices Comments
path
path
Location where temporary file or directory should be created.
If path is not specified, the default system temporary directory will be used.
prefix
str
Default: ansible.
Prefix of file/directory name created by module.
state
str
Default: file
Choices: directory, file
Whether to create file or directory.
suffix
str
Suffix of file/directory name created by module.

Examples

- name: Create temporary build directory
  ansible.builtin.tempfile:
    state: directory
    suffix: build

- name: Create temporary file
  ansible.builtin.tempfile:
    state: file
    suffix: temp
  register: tempfile_1

- name: Create a temporary file with a specific prefix
  ansible.builtin.tempfile:
     state: file
     suffix: txt
     prefix: myfile_

- 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

Return Values

Key Data Type Description Returned
path str Path to created file or directory. success

Authors

  • Krzysztof Magosa (@krzysztof-Magosa)