Help required for extracting Interface mac address

21 views
Skip to first unread message

Rakesh Parida

unread,
Jun 28, 2019, 1:54:03 AM6/28/19
to Ansible Project
Hi ,

My VM has 4 ip-interfaces . I want to extract a data as below using ansible.
Interface1: macaddress
Interface2: macaddress
Interface3: macaddress
Interface4: macaddress

Kindly help.

Punit Jain

unread,
Jun 28, 2019, 11:13:43 PM6/28/19
to ansible...@googlegroups.com
Assuming you have all macs in a list:

 - name: process mac addresses into a list
      set_fact:
        result: "{{result | default([]) | union([ 'Interface' + idx|string + ': ' + item]) }}"
      loop: "{{macs}}"
      loop_control:
        index_var: idx

    - name: set fact
      set_fact:
        final: "{{result|join('\n')}}"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fdbcffbf-0f59-4af1-942f-a85a751237cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dick Visser

unread,
Jun 29, 2019, 3:42:47 AM6/29/19
to ansible...@googlegroups.com
You could use a template like this:



{% for iface in ansible_interfaces | difference(['lo','eth0']) | sort %}
{{ iface }}: {{ hostvars[inventory_hostname]['ansible_' ~
iface]['macaddress'] }}
{% endfor %}


This will yield:

eth1: 08:00:27:b3:59:d0
eth2: 08:00:27:87:1b:de
eth3: 08:00:27:9b:80:85
eth4: 08:00:27:90:ad:eb
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fdbcffbf-0f59-4af1-942f-a85a751237cf%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Rakesh Parida

unread,
Jul 1, 2019, 12:42:13 AM7/1/19
to Ansible Project
Hi Dick,

Thanks very much for your help. :)
Thats exactly what i wanted. 

Regards
Rakesh


On Saturday, June 29, 2019 at 1:12:47 PM UTC+5:30, Dick Visser wrote:
You could use a template like this:



{% for iface in ansible_interfaces | difference(['lo','eth0']) | sort %}
{{ iface }}: {{ hostvars[inventory_hostname]['ansible_' ~
iface]['macaddress'] }}
{% endfor %}


This will yield:

eth1: 08:00:27:b3:59:d0
eth2: 08:00:27:87:1b:de
eth3: 08:00:27:9b:80:85
eth4: 08:00:27:90:ad:eb





On Fri, 28 Jun 2019 at 07:54, Rakesh Parida <rakeshp...@gmail.com> wrote:
>
> Hi ,
>
> My VM has 4 ip-interfaces . I want to extract a data as below using ansible.
> Interface1: macaddress
> Interface2: macaddress
> Interface3: macaddress
> Interface4: macaddress
>
> Kindly help.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages