Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to generate MAC addresses #1699

Open
giorgiga opened this issue Apr 4, 2023 · 8 comments
Open

Function to generate MAC addresses #1699

giorgiga opened this issue Apr 4, 2023 · 8 comments

Comments

@giorgiga
Copy link
Contributor

giorgiga commented Apr 4, 2023

Please add a function that allows generating MAC addresses based on some value (similar to ansible's community.general.random_mac when used with a seed)

@hairyhenderson
Copy link
Owner

hairyhenderson commented Apr 7, 2023

This seems like a reasonably decent idea. Until I (or someone else!) can implement this, one option is to define a template that calls random.Number a bunch of times and formats it as hex:

mac.t:

{{ define "hex" -}}
  {{ random.Number 0 255 | printf "%02x" -}}
{{ end -}}
{{ define "mac" -}}
  {{ template "hex" -}}
  {{ range (seq 5) }}:{{ template "hex" }}{{ end -}}
{{end -}}

then you can include that as a nested template:

$ gomplate -t mac=mac.t -i 'mac is {{ template "mac" }}{{"\n"}}'
mac is 3b:c2:aa:86:b2:b9

@giorgiga
Copy link
Contributor Author

giorgiga commented Apr 7, 2023

Random MACs are good but IMHO the killer feature of ansible's community.general.random_mac is that you can use any string as a seed.

This allows writing things like:

mac: "{{ 'AA:BB' | community.general.random_mac( seed= hostname + '/' + nic ) }}")

to get a (consistently reproducible) MAC based on hostname and nic (or on whatever else you please).

The above is ansible's syntax; personally I think that it would be better to have two separate functions, one (say prefix | net.CompleteRandomMAC) that generates a random MAC based on an optional prefix and another (say seed | net.ToMAC prefix) that "converts" any string to a mac address.

@hairyhenderson
Copy link
Owner

Ah, thanks for the followup - this makes sense.

BTW, two functions aren't necessary - a single function can behave differently given different inputs. For example:

  • net.GenerateMAC (no args) - could create a random MAC address
  • net.GenerateMAC "AA:BB" (1 arg) - could create a random MAC address with a fixed prefix
  • net.GenerateMAC "AA:BB" "seed" (2 args) - could create a predictably-random MAC with a fixed prefix - the prefix could be "" for none (to preserve the 2-arg signature)

The concept of allowing a user-provided seed is interesting - this may be useful to add into the random functions too 🤔

I can't promise that I'll be able to write this function any time soon - I have limited time to work on gomplate, and most of my gomplate-time is currently being used to get the v4.0 release ready... But if you're interested in contributing I would accept a PR for a function like this!

@giorgiga
Copy link
Contributor Author

TIA about the U/L bit in MAC addresses, which I think might be relevant for implementing this feature: https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local_(U/L_bit)

I doubt this would matter much in any practical scenario, but I think the new net.GenerateMAC should generate locally administered addresses when the user doesn't specify a prefix.

@hairyhenderson
Copy link
Owner

huh, good callout, TIL 😂

@github-actions
Copy link

This issue is stale because it has been open for 60 days with no activity. Remove stale label or comment or this will be automatically closed in a few days.

@github-actions
Copy link

This issue is stale because it has been open for 60 days with no
activity. If it is no longer relevant or necessary, please close it.
Given no action, it will be closed in 14 days.

If it's still relevant, one of the following will remove the stale
marking:

  • A maintainer can add this issue to a milestone to indicate that
    it's been accepted and will be worked on
  • A maintainer can remove the stale label
  • Anyone can post an update or other comment

@github-actions github-actions bot added the Stale label Aug 15, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2023
@hairyhenderson hairyhenderson added this to the future milestone Nov 24, 2023
@hairyhenderson
Copy link
Owner

Reopening as I still think this is useful, but can't commit to this for v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants