fix issue in random_mac filter with short prefixes (#53928)
This commit is contained in:
parent
bb3d97a787
commit
1e3428a766
3 changed files with 9 additions and 3 deletions
2
changelogs/fragments/random_mac-random-int-fix.yaml
Normal file
2
changelogs/fragments/random_mac-random-int-fix.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- random_mac - generate a proper MAC address when the provided vendor prefix is two or four characters (https://github.com/ansible/ansible/issues/50838)
|
|
@ -36,7 +36,7 @@ import yaml
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from random import Random, SystemRandom, shuffle, random
|
from random import Random, SystemRandom, shuffle, randint
|
||||||
|
|
||||||
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
|
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
|
||||||
|
|
||||||
|
@ -555,8 +555,8 @@ def random_mac(value):
|
||||||
if len(err):
|
if len(err):
|
||||||
raise AnsibleFilterError('Invalid value (%s) for random_mac: %s' % (value, err))
|
raise AnsibleFilterError('Invalid value (%s) for random_mac: %s' % (value, err))
|
||||||
|
|
||||||
# Generate random float and make it int
|
# Generate random int between x1000000000 and xFFFFFFFFFF
|
||||||
v = int(random() * 10.0**10)
|
v = randint(68719476736, 1099511627775)
|
||||||
# Select first n chars to complement input prefix
|
# Select first n chars to complement input prefix
|
||||||
remain = 2 * (6 - len(mac_items))
|
remain = 2 * (6 - len(mac_items))
|
||||||
rnd = ('%x' % v)[:remain]
|
rnd = ('%x' % v)[:remain]
|
||||||
|
|
|
@ -235,7 +235,11 @@
|
||||||
- name: Verify random_mac filter
|
- name: Verify random_mac filter
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
|
- "'00' | random_mac is match('^00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
|
||||||
|
- "'00:00' | random_mac is match('^00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
|
||||||
- "'00:00:00' | random_mac is match('^00:00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
|
- "'00:00:00' | random_mac is match('^00:00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
|
||||||
|
- "'00:00:00:00' | random_mac is match('^00:00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
|
||||||
|
- "'00:00:00:00:00' | random_mac is match('^00:00:00:00:00:[a-f0-9][a-f0-9]$')"
|
||||||
- "'00:00:00' | random_mac != '00:00:00' | random_mac"
|
- "'00:00:00' | random_mac != '00:00:00' | random_mac"
|
||||||
|
|
||||||
- name: Verify that union can be chained
|
- name: Verify that union can be chained
|
||||||
|
|
Loading…
Reference in a new issue