Generate unique identifier?

4,442 views
Skip to first unread message

Mike Trienis

unread,
May 2, 2014, 5:02:19 PM5/2/14
to ansible...@googlegroups.com
Hi All,

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


Thanks, Mike. 

Brian Coca

unread,
May 3, 2014, 11:22:09 PM5/3/14
to ansible...@googlegroups.com
depends on how unique you need it to be, you can create one easily with MAC address, date/time facts and random filter​

Juergen Brendel

unread,
May 4, 2014, 4:48:34 AM5/4/14
to ansible...@googlegroups.com

Hello!


On Saturday, 3 May 2014 09:02:19 UTC+12, Mike Trienis wrote:
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

Andrew O'Brien

unread,
May 4, 2014, 9:41:39 PM5/4/14
to ansible...@googlegroups.com
Hi Mike,

> -----Original Message-----
> From: ansible...@googlegroups.com [mailto:ansible...@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
local_action: apt pkg=uuid-runtime state=latest

- name: generate a uuid for something
local_action: shell uuidgen
register: my_uuid

# later in the play use my_uuid.stdout as a variable

Hope this is helpful.

Cheers,

Andrew

Mike Trienis

unread,
Aug 19, 2014, 12:41:40 AM8/19/14
to ansible...@googlegroups.com
Thanks for all the responses, they were all very helpful.

Romeo Theriault

unread,
Aug 19, 2014, 5:09:38 PM8/19/14
to ansible...@googlegroups.com
On Sat, May 3, 2014 at 10:48 PM, Juergen Brendel <jbre...@gmail.com> wrote:

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

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


--
Romeo

Romeo Theriault

unread,
Aug 19, 2014, 5:10:55 PM8/19/14
to ansible...@googlegroups.com
Though, I guess it's not idempotent all by itself... You'd still have to store the output in a register, etc...
--
Romeo

Michael DeHaan

unread,
Aug 19, 2014, 9:45:21 PM8/19/14
to ansible...@googlegroups.com
"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 :)




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACUx58MroO2rYGg8OOvedQemR4OsC_4A%3D8khnev1CFarXAERSg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages