# Generate group\_var file from Ansible playbook

**URL:** <https://forum.ansible.com/t/generate-group-var-file-from-ansible-playbook/33831>\
**Category:** Ansible Project\
**Created:** [September 16, 2020, 7:13pm UTC](https://forum.ansible.com/t/generate-group-var-file-from-ansible-playbook/33831 "2020-09-16T19:13:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [September 16, 2020, 7:13pm UTC](https://forum.ansible.com/t/generate-group-var-file-from-ansible-playbook/33831/1 "2020-09-16T19:13:46Z")

</div>

Hello,

I need to use an Ansible playbook to dynamically generate Ansible group\_var type file. Here is my inventory file:

[group1]  
[server1.example.com](http://server1.example.com)  
[server2.example.com](http://server2.example.com)

[group2]  
[server3.example.com](http://server3.example.com)  
[server4.example.com](http://server4.example.com)

The generated mygroupvars file is expected to be:

group1\_var: xyzabcdefghi  
group2\_var: abcejrje2ljree

The random string is produced by dd command like below:

dd if=/dev/urandom count=1 2\>/dev/null | uuencode -m - | sed -ne 2p | cut -c -20 | perl -p -i -e ‘s/\+/x/g’ | perl -p -i -e ‘s/\//L/g’

uuencode is from sharutils package on CentOS 7

Here is my playbook:

- name: dummy  
hosts: localhost  
gather\_facts: no

tasks:

- name: create file  
file:  
dest: mygroupvars  
state: touch  
delegate\_to: localhost

- name: Generate random string  
shell: “dd if=/dev/urandom count=1 2\>/dev/null | uuencode -m - | sed -ne 2p | cut -c -20 | perl -p -i -e ‘s/+/x/g’ | perl -p -i -e ‘s///L/g’”  
register: op

- name: debug  
debug:  
msg: “{{ op }}”

- name: append  
lineinfile:  
dest: mygroupvars  
line: “{{ op.stdout\_lines }}”  
insertafter: EOF  
delegate\_to: localhost

This playbook only generates one random string, and writes it to mygroupvars file. I need to loop through inventory file, based on number of groups in inventory file, generate the same number of random string matching number of groups, and write them all to mygroupvars file using the group\_var file format. It is key/value pair. The key contains group name with suffix \_var. The values come from random string generated.

How can I accomplish it?

Thank you!

- Xinhuan

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 16, 2020, 8:32pm UTC](https://forum.ansible.com/t/generate-group-var-file-from-ansible-playbook/33831/2 "2020-09-16T20:32:32Z")

</div>

Try this single task instead (I used some slightly simpler logic for  
random generation):

- name: dummy  
&nbsp;&nbsp;hosts: localhost  
&nbsp;&nbsp;gather\_facts: no

&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Populate random values for each group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for group in (groups.keys() | difference(['all', 'ungrouped'])) %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ group }}\_var: {{ lookup('pipe', 'dd if=/dev/urandom  
bs=256 count=2 2\>/dev/null | LC\_ALL=C tr -cd "[:alnum:]" | head -c  
${1-64}') }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: mygroupvars
