I have an integer with me, I need to convert into MAC address. I check for hwaddr
in Ansible, did not worked for me. Please help me out.
I tried in built module like ipmath
and hwaddr
. Nothing came handy.
I have an integer with me, I need to convert into MAC address. I check for hwaddr
in Ansible, did not worked for me. Please help me out.
I tried in built module like ipmath
and hwaddr
. Nothing came handy.
I have an integer with me, I need to convert into MAC address.
Not the prettiest answer, ensure the pip package netaddr
is installed on the controller. Can be done w python3 -m pip install netaddr
.
- debug:
msg: "{{ 143683695594496 | int | hwaddr('unix') }}"
This returns:
82:ad:f7:a2:cc:00
Based on
netaddr
package installed (... which I like to remove)
TASK [Show MAC] *************************************************************************
fatal: [localhost]: FAILED! =>
msg: The hwaddr filter requires python's netaddr be installed on the ansible controller
0:ae:cd:9:db:50
I've crafted a sample filter plugin according tripleee's comment and Rafel's answer.
cat plugins/filter/int2mac.py
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'int2mac': self.int2mac,
}
def int2mac(self, IntegerValue):
return ':'.join(['{}{}'.format(a, b)
for a, b
in zip(*[iter('{:012x}'.format(IntegerValue))]*2)
])
Source of the return
part, Github Gist 'nlm' and almost tripleee's comment. There is also a second example which could be used as a starting point to write an own filter plugin. In any case take note of The default license of a Gist!
A sample playbook int2mac.yml
---
- hosts: localhost
become: false
gather_facts: false
vars:
tasks:
- name: Show MAC
debug:
msg: "{{ 750764284752 | int2mac }}"
will result into an output of
TASK [Show MAC] *********
ok: [localhost] =>
msg: 00:ae:cd:09:db:50
In other words, you can utilize Python and almost all the functionality of it to enhance your Ansible and playbooks, in example by creating easy and simple filters or other plugins. This means if functionality is not given or not behave as you like, you can just create them.
Further Q&A
... about writing an own simple filter plugin
Some Notes
Using the approach from tripleee's comment
return ':'.join([
("%12x" % IntegerValue)[x:x+2] for x in range(0, 12, 2)
])
will result into an output of
TASK [Show MAC] **********
ok: [localhost] =>
msg: ' :ae:cd:09:db:50'
and missing zeros.
Using the approach with hwaddr
filter from Kevin C's answer
msg: "{{ 15 | int | hwaddr('unix') }}"
will result into an output of
TASK [Show MAC] **
ok: [localhost] =>
msg: 0:0:0:0:0:f
and missing leading zeros.
According IEEE OUI's there are some vendors which have a significant amount of leading zeros.
Using
msg: "{{ 15 | int | hwaddr('linux') }}"
will result into an output of
TASK [Show MAC] ********
ok: [localhost] =>
msg: 00:00:00:00:00:0f
From the given examples one can see why it is necessary to provide Minimal, Reproducible Examples together with all necessary input and expected output.