ansible.builtin.tempfile – Creates temporary files and directories

Note

This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name tempfile even without specifying the collections: keyword. However, we recommend you use the FQCN for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

New in version 2.3: of ansible.builtin

Synopsis

  • The tempfile module creates temporary files and directories. 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 ansible.builtin.file module.

  • For Windows targets, use the ansible.windows.win_tempfile module instead.

Parameters

Parameter

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

string

Prefix of file/directory name created by module.

Default: “ansible.”

state

string

Whether to create file or directory.

Choices:

  • directory

  • file ← (default)

suffix

string

Suffix of file/directory name created by module.

Default: “”

See Also

See also

ansible.builtin.file

The official documentation on the ansible.builtin.file module.

ansible.windows.win_tempfile

The official documentation on the ansible.windows.win_tempfile 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: 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

Common return values are documented here, the following are the fields unique to this module:

Key

Description

path

string

Path to created file or directory.

Returned: success

Sample: “/tmp/ansible.bMlvdk”

Authors

  • Krzysztof Magosa (@krzysztof-magosa)