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

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/178f6b50-5b24-4213-8fb8-24e23fdd1327%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to