# Generate IPv6 ULA /64 subnet

**URL:** <https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019>\
**Category:** Get Help\
**Created:** [December 22, 2025, 10:55am UTC](https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019 "2025-12-22T10:55:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christophe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/christophe/32/10021_2.png) [@Christophe](https://forum.ansible.com/u/Christophe)\
**Post date:** [December 22, 2025, 10:55am UTC](https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019/1 "2025-12-22T10:55:55Z")

</div>

Hello all,  
Is there a way to generate an IPv6 ULA /64 subnet subnet?  
I’ve been looking in the docs but nowhere can I find how to do this.

Some Google-fu gave me this command, which works locally, but not when ran through ansible using ansible.builtin.shell:  
printf “fd%x:%x:%x:%x::/64” “$(( $RANDOM/256 ))” “$RANDOM” “$RANDOM” “$RANDOM”

Anybody who has a solution or workaround for this?

---

<div class="post-metadata">

**Author:** ![U880D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/u880d/32/10032_2.png) [@U880D](https://forum.ansible.com/u/U880D)\
**Post date:** [December 22, 2025, 2:43pm UTC](https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019/2 "2025-12-22T14:43:29Z")

</div>

Considering that [$RANDOM](https://tldp.org/LDP/abs/html/randomvar.html)

> is an internal Bash [function](https://tldp.org/LDP/abs/html/functions.html#FUNCTIONREF) (not a constant) that returns a [_pseudorandom_](https://tldp.org/LDP/abs/html/randomvar.html#FTN.AEN5817) integer in the range 0 - 32767

the almost exact equivalent to the given command

```auto
printf "fd%x:%x:%x:%x::/64\n" "$(( $RANDOM/256 ))" "$RANDOM" "$RANDOM" "$RANDOM"

```

resulting into a random output like

```auto
fd1:4dd9:8f1:6ea7::/64

```

would be in Ansible

```auto
---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

    - name: Generate an IPv6 Unique Local Address (ULA) /64 subnet
      debug:
        msg: "fd{{ '%02x' % ((127 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}::/64"

```

resulting into a random output of

```auto
TASK [Generate an IPv6 Unique Local Address (ULA) /64 subnet] ***
ok: [localhost] =>
  msg: fd01:4dd9:8f1:6ea7::/64

```

by using [Python Format](https://docs.python.org/3/library/string.html#format-specification-mini-language) with [`%` within Jinja2 Template](https://jinja.palletsprojects.com/en/stable/templates/).

---

<div class="post-metadata">

**Author:** ![Christophe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/christophe/32/10021_2.png) [@Christophe](https://forum.ansible.com/u/Christophe)\
**Post date:** [December 22, 2025, 4:01pm UTC](https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019/3 "2025-12-22T16:01:32Z")

</div>

> [@U880D](#):
>
> `"fd{{ '%02x' % ((127 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}:{{ '%x' % ((32767 | random) | int) }}::/64"`

Great, that seems to work!  
Don’t think I would have ever found this myself.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 21, 2026, 4:02pm UTC](https://forum.ansible.com/t/generate-ipv6-ula-64-subnet/45019/4 "2026-01-21T16:02:19Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
