DEV Community

IPYE
IPYE

Posted on

Ansible: Carve and Assign a Free /48 from One Variable

Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.

Hand-typed IPv6 addresses are where typos hide. A single variable holding the /48, plus a filter that computes subnets and host addresses from it, gives you configurations that are consistent across every server and easy to renumber. This article uses Ansible with the ansible.utils collection to turn a free IPv6 /48 from IPYE into per-site /64s and per-host addresses, then renders them into systemd-networkd files. The examples use 2001:db8:100::/48.

Install the pieces

ansible-galaxy collection install ansible.utils
pip install netaddr
Enter fullscreen mode Exit fullscreen mode

The ipaddr family of filters lives in ansible.utils and depends on the netaddr Python library on the control node.

One source of truth

Put the prefix and the subnet plan in group_vars/all.yml:

free48: "2001:db8:100::/48"

subnets:
  infra:   1
  web:     16
  db:      32
  lab:     255
Enter fullscreen mode Exit fullscreen mode

The numbers are subnet indexes within the /48. Index 16 becomes 2001:db8:100:10::/64, because 16 in hexadecimal is 10. Choosing indexes on round hex values keeps the resulting addresses readable.

Each host gets its role and a host number in its inventory entry:

[web]
web1 host_index=10
web2 host_index=11

[db]
db1 host_index=10
Enter fullscreen mode Exit fullscreen mode

Compute subnets and addresses

# group_vars/all.yml (continued)
role_subnet: "{{ free48 | ansible.utils.ipsubnet(64, subnets[role]) }}"
host_addr: "{{ role_subnet | ansible.utils.ipaddr(host_index) | ansible.utils.ipaddr('address') }}"
gateway6: "{{ role_subnet | ansible.utils.ipaddr(1) | ansible.utils.ipaddr('address') }}"
Enter fullscreen mode Exit fullscreen mode

ipsubnet(64, 16) returns the /64 at index 16 inside the /48, counting from zero. ipaddr(10) returns the tenth address in that subnet with its prefix length, and ipaddr('address') strips the length. Set role per group in group_vars/web.yml and the like.

Check the results before rendering anything:

ansible -i inventory web1 -m debug -a "msg={{ host_addr }} via {{ gateway6 }}"
Enter fullscreen mode Exit fullscreen mode

For web1 this prints 2001:db8:100:10::a via 2001:db8:100:10::1.

Render systemd-networkd files

# templates/10-uplink.network.j2
[Match]
Name={{ uplink_iface | default('ens3') }}

[Network]
Address={{ host_addr }}/64
Gateway={{ gateway6 }}
IPv6AcceptRA=no
Enter fullscreen mode Exit fullscreen mode
- name: Configure IPv6 address from the free /48
  ansible.builtin.template:
    src: 10-uplink.network.j2
    dest: /etc/systemd/network/10-uplink.network
    mode: "0644"
  notify: reload networkd

handlers:
  - name: reload networkd
    ansible.builtin.command: networkctl reload
Enter fullscreen mode Exit fullscreen mode

Run with --check --diff first. It shows the rendered file and the change without writing anything, which is the cheapest review step you will ever get.

Guard against mistakes

Add assertions so a bad variable stops the play instead of producing a wrong address:

- ansible.builtin.assert:
    that:
      - free48 | ansible.utils.network_in_network(host_addr)
      - subnets[role] < 65536
    fail_msg: "{{ inventory_hostname }} would get an address outside the free /48"
Enter fullscreen mode Exit fullscreen mode

network_in_network is true only when the address falls inside the /48, so the first test fails for anything outside it. The second test catches an index too large for a /48.

Also check for duplicate host indexes within a role. A short task that builds a list of host_addr across groups['web'] and compares its length with the unique set catches the classic copy-and-paste inventory error.

Feed the same data to the router

The router needs the same plan: one connected /64 per role, plus a route for the aggregate. Render its configuration from the same variables, for example an FRR file:

ipv6 route {{ free48 }} blackhole
{% for name, idx in subnets.items() %}
! {{ name }}: {{ free48 | ansible.utils.ipsubnet(64, idx) }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

The rendered comments double as documentation on the router itself, so whoever logs in next can see which /64 belongs to which role without opening the repository.

Because both sides come from one variable, renumbering into a different /48 later is a one-line change and a playbook run.

Reverse direction: find the owner of an address

The same data answers the question you will ask during an incident: which role owns 2001:db8:100:20::a? A small ad-hoc task that loops over subnets and tests each ipsubnet with network_in_network prints the role name, which beats searching through config files at three in the morning.

Where IPYE fits

The prefix itself comes from the free /48 offer. The Letter of Authorization for your upstream and the ROA status are both in the IPYE customer portal, and the ROA appears without any action on your part, because the prefix is held on IPYE's ARIN or APNIC accounts.

Do this next

Claim a free /48, put it in group_vars/all.yml, and run the debug task against your inventory before you render a single file. Questions: ipye@turnel.com.

About IPYE

IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: ipye@turnel.com.

Disclosure: I run IPYE

Top comments (0)