# Generate (or reuse) random string/id in custom module common for all hosts

**URL:** <https://forum.ansible.com/t/generate-or-reuse-random-string-id-in-custom-module-common-for-all-hosts/17511>\
**Category:** Ansible Project\
**Created:** [February 13, 2015, 2:45pm UTC](https://forum.ansible.com/t/generate-or-reuse-random-string-id-in-custom-module-common-for-all-hosts/17511 "2015-02-13T14:45:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Horak](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Horak](https://forum.ansible.com/u/Daniel_Horak)\
**Post date:** [February 13, 2015, 2:45pm UTC](https://forum.ansible.com/t/generate-or-reuse-random-string-id-in-custom-module-common-for-all-hosts/17511/1 "2015-02-13T14:45:52Z")

</div>

Hi,  
I’ve created quite simple python module for installation and execution of multihost tests. And because of synchronization between the hosts, I need to generate “random” string/id/whatever and pass it to each test and it has to be the same for all hosts in one test. (But it has to be different when I’ll rerun the ansible playbook.)

Current solution looks like this: I’ll locally create random string in separate task before the test execution and pass it as argument to the run\_test module (named uuid):

- name: generate uuidgen for test1  
command: uuidgen  
run\_once: true  
delegate\_to: localhost  
register: uuid

- name: run test1  
run\_test: name=test1 unique\_id=“{{ uuid.stdout\_lines[0] }}”

- name: generate uuidgen for test2  
command: uuidgen  
run\_once: true  
delegate\_to: localhost  
register: uuid

- name: run test2  
run\_test: name=test2 unique\_id=“{{ uuid.stdout\_lines[0] }}”

- name: generate uuidgen for test3  
command: uuidgen  
run\_once: true  
delegate\_to: localhost  
register: uuid

- name: run test3  
run\_test: name=test3 unique\_id=“{{ uuid.stdout\_lines[0] }}”

But my idea was to simplify it and generate some id directly inside the module (or probably reuse some internal ansible variable), so the final playbook could look like this (and each test will have available unique string common for all hosts):

- name: run test1  
run\_test: name=test1

- name: run test2  
run\_test: name=test2

- name: run test3  
run\_test: name=test3

I’ve tried to use method get\_module\_path() and parse the first number after ansible-tmp-… in the path (it looks like /root/.ansible/tmp/ansible-tmp-1423668579.15-197746855489444), but it seems like it sometimes vary and it broke the synchronization.

Do you have any suggestion how to generate or where to obtain common id for all hosts inside the ansible module (written in python)?

Thanks!  
Daniel
