0

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.

U880D
  • 8,601
  • 6
  • 24
  • 40
Avish
  • 49
  • 6
  • 2
    The conventional MAC representation is just a hex number with colons. `12:34:56:78:90:ab:cd` corresponds to the integer 5124095575370701. – tripleee Nov 02 '22 at 12:31
  • So, how can we do int to hexa in ansible? please suggest something. – Avish Nov 02 '22 at 13:12
  • 1
    `print(":".join([("%12x" % 5124095575370701)[x:x+2] for x in range(0, 12, 2)]))` – tripleee Nov 02 '22 at 13:20

2 Answers2

3

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

Kevin C
  • 4,851
  • 8
  • 30
  • 64
  • Hi, I basically tried the below integer but I recieved wrong mac. - name: mac debug: msg: "{{ 750764284752 | int | hwaddr('unix') }}" Output : 75:7:64:28:47:52 Please help !! – Avish Nov 03 '22 at 06:18
  • @Avish, regarding your comment I've run a test with `{{ 750764284752 | int | hwaddr('unix') }}` resulting into an output of `msg: 0:ae:cd:9:db:50`. In other words, I wasn't able to reproduce it. – U880D Nov 03 '22 at 07:27
3

Based on

  • the given comment of tripleee
  • the former given answer of Rafel (... which seems to be deleted now)
  • the given answers of Kevin C
  • in respect to the dependency to have Python 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
    
  • as well the formatting, a missing leading zero in my test result
    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.

U880D
  • 8,601
  • 6
  • 24
  • 40