# Generate unique identifier?

**URL:** <https://forum.ansible.com/t/generate-unique-identifier/15150>\
**Category:** Ansible Project\
**Created:** [May 2, 2014, 9:02pm UTC](https://forum.ansible.com/t/generate-unique-identifier/15150 "2014-05-02T21:02:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Trienis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Mike\_Trienis](https://forum.ansible.com/u/Mike_Trienis)\
**Post date:** [May 2, 2014, 9:02pm UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/1 "2014-05-02T21:02:19Z")

</div>

Hi All,

I was wondering if ansible has a way to generate a unique identifier?

Thanks, Mike.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 4, 2014, 3:22am UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/2 "2014-05-04T03:22:09Z")

</div>

depends on how unique you need it to be, you can create one easily with MAC address, date/time facts and random filter​

---

<div class="post-metadata">

**Author:** ![Juergen\_Brendel](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@Juergen\_Brendel](https://forum.ansible.com/u/Juergen_Brendel)\
**Post date:** [May 4, 2014, 8:48am UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/3 "2014-05-04T08:48:34Z")

</div>

Hello!

> I was wondering if ansible has a way to generate a unique identifier?

I don’t think Ansible itself has that capability built in. Luckily, you can easily do this yourself. As another response said, it also depends on how unique you want it to be. Just the other day I had to solve that problem as well. I settled for fine-grained time stamp plus random characters, which in my case was guaranteed to be unique enough. For the random characters I ended up taking a recipe I had found somewhere (sorry, can’t find the URL anymore) on how to create a random string, combined it in the shell with the datetime output and phrased this as an Ansible task. Like so:

- name: Create a unique ID  
shell: echo “`date +"%Y%m%d%H%M%S"`-$(cat /dev/urandom | tr -cd [:alpha:] | tr ‘[:upper:]’ ‘[:lower:]’ | head -c 4)”  
register: my\_unique\_id

This takes a datetime stamp and attaches some random letters. You can drop the “date” output and just go with random letters, for example. The “head -c 4” in the end determines how many random letters there will be appended. Just take that echo line and paste it into the shell, experiment around with different variations until you get the ID you want.

Later on in your playbook, you can then refer to the unique ID like so:

- name: Create a directory named after my unique ID  
file: path=/tmp/{{ my\_unique\_id.stdout }} state=directory

Juergen

---

<div class="post-metadata">

**Author:** ![Andrew\_O\_Brien](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Andrew\_O\_Brien](https://forum.ansible.com/u/Andrew_O_Brien)\
**Post date:** [May 5, 2014, 1:41am UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/4 "2014-05-05T01:41:39Z")

</div>

Hi Mike,

> From: ansible-project@googlegroups.com [mailto:ansible-project@googlegroups.com] On Behalf Of Mike Trienis  
> Sent: Saturday, 3 May 2014 7:02 AM
> 
> Hi All,
> 
> I was wondering if ansible has a way to generate a unique identifier?

Assuming that you are running ansible from a linux system you could always install uuidgen (part of libuuid). On debian-derived systems this then becomes, in ansible-speak (hand-typed - probably will not work as-is 🙂 ):

- name: install uuid runtime package on local  
&nbsp;&nbsp;local\_action: apt pkg=uuid-runtime state=latest

- name: generate a uuid for something  
&nbsp;&nbsp;local\_action: shell uuidgen  
&nbsp;&nbsp;register: my\_uuid

# later in the play use my\_uuid.stdout as a variable

Hope this is helpful.

Cheers,

Andrew

---

<div class="post-metadata">

**Author:** ![Mike\_Trienis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Mike\_Trienis](https://forum.ansible.com/u/Mike_Trienis)\
**Post date:** [August 19, 2014, 4:41am UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/5 "2014-08-19T04:41:39Z")

</div>

Thanks for all the responses, they were all very helpful.

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [August 19, 2014, 9:09pm UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/6 "2014-08-19T21:09:34Z")

</div>

If you're on linux you can just use the 'mktemp' utility, which is part of  
coreutils.

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [August 19, 2014, 9:10pm UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/7 "2014-08-19T21:10:51Z")

</div>

Though, I guess it’s not idempotent all by itself… You’d still have to store the output in a register, etc…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 20, 2014, 1:45am UTC](https://forum.ansible.com/t/generate-unique-identifier/15150/8 "2014-08-20T01:45:18Z")

</div>

“Though, I guess it’s not idempotent all by itself”

Easily dealt with 🙂

- shell: foo

changed\_when: False

Running commands that don’t change anything is perfectly fine in the config management world, rather, the concern should be changing things that don’t need to be changed 🙂
