This article is part of the following series.
- 4 Ways to Find network interfaces in Linux
- 5 useful Linux network troubleshooting commands
- 3 Easy Ways to Find IP address in Linux
- 3 Ways to Find MAC Address In Linux
Table of Contents
Get MAC address with Ansible
You can use the ansible_default_ipv4.macaddress variable to get the MAC address using Ansible.This is a variable that contains the MAC address of default network interface.
Here is an example Ansible playbook that uses the Ansible facts to retrieve the MAC address of the default interface:
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!- name: Get MAC address hosts: your_host_name gather_facts: yes tasks: - name: Display MAC address debug: var: ansible_default_ipv4.macaddress
In this playbook, the gather_facts module is set to yes, which allows Ansible to collect information about the remote host, including the network interfaces and their MAC addresses. The debug module is then used to display the MAC address of the default IPv4 interface on the remote host.
You can run this playbook using the ansible-playbook command and specifying the inventory file and playbook file:
ansible-playbook -i inventory_file get_mac_address.yml
Replace your_host with the name or IP address of the remote host in your inventory file.
You can also use this variable ansible_facts[‘default_ipv4’][‘macaddress’]
If you already know the interface name, you can use the following Ansible code to get the MAC address of this network interface.
- name: Get MAC address of eth0 hosts: your_host_name gather_facts: yes tasks: - name: Display MAC address of eth0 debug: var: ansible_facts['eth0']['macaddress']
This playbook retrieves the MAC address of the eth0 interface and displays it using the debug module.
understanding gather_facts in Ansible
In Ansible, the gather_facts: yes is used to collect system information from remote hosts. When this is included in a playbook, it runs a series of default tasks to collect facts about the remote host, such as the hostname, IP address, CPU architecture, operating system version, available disk space, and memory usage.
The gathered facts are stored as variables that can be used later in the playbook. These variables can be accessed using the {{ }} syntax, for example, {{ ansible_hostname }} to retrieve the hostname of the remote host.
You can use the following code to get all the Ansible facts.
- name: Get facts hosts: your_host_name gather_facts: yes tasks: - name: Get Ansible facts debug: var: ansible_facts
The gather_facts can also be customized by specifying specific facts to collect or by running custom scripts to collect additional information. For example, you can run a custom script to collect information about specific software installed on the remote host.
Ansible setup module does the same action what gathering_facts does during the ansible playbook execution. in fact gathering_facts use setup module to collect the facts.
You can run the following command to get the facts using one line command.
ansible -i ansible_hosts servers -m setup
In some cases, we don’t need to collect all the facts. We can use the following Ansible code to collect subset facts.
- name: Filter and return only selected facts setup: gather_subset: - "devices"
Or like this:
ansible -i ansible_hosts servers -m setup -a 'filter=ansible_distribution,ansible_distribution_version,ansible_architecture'
Understanding network interface and MAC address
A network interface is a component that allows a device to connect to a network. It is also known as a network adapter, network card, or network interface card (NIC). The interface can be physical, such as an Ethernet or Wi-Fi card, or virtual, such as a VPN adapter.
Each network interface has a unique identifier called a MAC (Media Access Control) address. This address is assigned by the manufacturer and is usually a 48-bit number represented in hexadecimal format. It is used to identify the device on the network and to ensure that data is transmitted to the correct destination.
List all the network interfaces and MAC address with Ansible
This code is an Ansible playbook task that displays MAC (Media Access Control) information for each network interface on the target system.
- name: Display MAC Information debug: msg: "{{ item }} : {{ ansible_facts[item]['macaddress']}}" with_items: "{{ansible_interfaces|difference(['lo'])}}"
TASK [Display Disk Information]
ok: [howtouselinux.com] => (item=ens192) => {
"msg": "ens192:00:50:56:81:68:27"
}
Here’s a breakdown of what’s happening in the code:
- The task has a name of “Display MAC Information” for identification purposes.
- The “debug” module is used to output information during playbook execution. In this case, the “msg” parameter is used to output a message that includes the name of a network interface and its corresponding MAC address.
- The “with_items” parameter specifies that the task should be run for each item in the list generated by the expression “{{ansible_interfaces|difference([‘lo’])}}”. This expression filters out the loopback interface (“lo”) from the list of available interfaces.
To learn how to use with_items in Ansible, you can refer to this article: Ansible Lists 101: A Beginner’s Guide to Managing and Manipulating Data
Daniel Li
Sunday 29th of October 2023
You explain it very well. Thanks.