The situation

I needed a UUID that is unique for this specific Ansible task. It should also be unique when calling the same task again, so essentially what I assume to be the usual way to work with UUIDs.

The solution

The short code is: lookup("pipe", "uuidgen").

  • The pipe is actually the ansible.builtin.pipe and documented here. It runs a command and returns the output.
  • The command to be executed by the pipe is uuidgen. It is available on Ubuntu systems and part of the util-linux package. It essentially just returns some UUID.

Use it

My use case was to send it to an API, so my task looked as follows:

- name: Send a UUID to a web server
  ansible.builtin.uri:
      body:
          data: '{{ lookup("pipe", "uuidgen") }}'
      body_format: json
      url: https://domain.com