<p>Assuming you can connect to the machine (say, using a local connection), you can interrogate the <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables" rel="nofollow noreferrer">host facts</a>.</p>
<p>It should expose a list <code>ansible_interfaces</code>, which will include the name of your ethernet interface. Each device will have a key in the facts. If your device is called <code>eth0</code>, you will have a dict called <code>ansible_eth0</code>.</p>
<p>The keys should include:</p>
<pre><code>&quot;ansible_eth0&quot; {
  &quot;active&quot;: true,
  &quot;device&quot;: &quot;eth0&quot;,
    &quot;ipv4&quot;: {
      &quot;address&quot;: &quot;&lt;address&gt;&quot;,
      &quot;broadcast&quot;: &quot;&lt;address&gt;&quot;,
      &quot;netmask&quot;: &quot;&lt;netmask&gt;&quot;,
      &quot;network&quot;: &quot;&lt;network&gt;&quot;
    },
    &quot;ipv6&quot;: [
      {
        &quot;address&quot;: &quot;&lt;ipv6_network&gt;&quot;,
        &quot;prefix&quot;: &quot;64&quot;,
        &quot;scope&quot;: &quot;link&quot;
      }
    ],
    &quot;macaddress&quot;: &quot;&lt;hw_address&gt;&quot;,
    &quot;module&quot;: &quot;&lt;kernel_module&gt;&quot;,
    &quot;mtu&quot;: XXX,
    &quot;pciid&quot;: &quot;XXX&quot;,
    &quot;promisc&quot;: false,
    &quot;type&quot;: &quot;ether&quot;
}
</code></pre>
<p>The <code>active</code> key there will tell you whether the device is active.</p>
<p>I would do it in the following way:</p>
<ul>
<li>discover list of devices</li>
<li>find device that matches a name</li>
<li>discover status of the device.</li>
</ul>
<p>A shorter way might be with a <a href="https://docs.ansible.com/ansible/2.9/user_guide/playbooks_filters.html#json-query-filter" rel="nofollow noreferrer"><code>json_query</code> filter</a></p>
<p>If you need to check that the device is ready from a remote execution, you could use a <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html" rel="nofollow noreferrer"><code>wait_for</code></a> on a specific port that should become available when the device is up.</p>

CC BY-SA 4.0
2023-08-03T15:50:48.173Z
How would you do this after changing interface status. Example: ansible modified ifcfg-eth0 and restarted network service. What is the fastest way to verify the change did what it was supposed to?  It can run ansible_setup: again, but perhaps there is a faster way.
majorgear
CC BY-SA 4.0
2023-08-03T17:48:50.750Z
yes, the best way I think would be to refresh the facts, by running setup again. You could use a handler for setup and flush it when the relevant task changes.
Bruce Becker
CC BY-SA 4.0
2023-08-09T13:21:42.307Z
Good idea. For my current project, I went with what I think is a faster, but less Ansible-ish method , if using the "ip" command in a shell task to get the status. Fr my project,I only need to shutdown some ports once, and after that they will never be used again.
majorgear
CC BY-SA 4.0
2021-09-07T08:10:44.650Z
2021-09-07T08:10:44.650Z
Bruce Becker
<p>I want to check status of Ethernet port whether it is up or down on remote server using Ansible?</p>

CC BY-SA 4.0
2021-09-08T15:22:01.607Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
<ansible><automation>
How to check the status of Ethernet port using ansible?
user30584
# How to check the status of Ethernet port using ansible?

(Asked by: username_0 on 2021-09-07)

I want to check status of Ethernet port whether it is up or down on remote server using Ansible?

**Tags:** ansible, automation

> **Comments:**
> **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  

---

## 1 answers

### Answer by username_2 on 2021-09-07. Score: 1

Assuming you can connect to the machine (say, using a local connection), you can interrogate the host facts.
It should expose a list ansible_interfaces, which will include the name of your ethernet interface. Each device will have a key in the facts. If your device is called eth0, you will have a dict called ansible_eth0.
The keys should include:
"ansible_eth0" {
  "active": true,
  "device": "eth0",
    "ipv4": {
      "address": "",
      "broadcast": "",
      "netmask": "",
      "network": ""
    },
    "ipv6": [
      {
        "address": "",
        "prefix": "64",
        "scope": "link"
      }
    ],
    "macaddress": "",
    "module": "",
    "mtu": XXX,
    "pciid": "XXX",
    "promisc": false,
    "type": "ether"
}

The active key there will tell you whether the device is active.
I would do it in the following way:

discover list of devices
find device that matches a name
discover status of the device.

A shorter way might be with a json_query filter
If you need to check that the device is ready from a remote execution, you could use a wait_for on a specific port that should become available when the device is up.

> **Comments:**
> **username_3**: How would you do this after changing interface status. Example: ansible modified ifcfg-eth0 and restarted network service. What is the fastest way to verify the change did what it was supposed to?  It can run ansible_setup: again, but perhaps there is a faster way.  
> **username_2**: yes, the best way I think would be to refresh the facts, by running setup again. You could use a handler for setup and flush it when the relevant task changes.  
> **username_3**: Good idea. For my current project, I went with what I think is a faster, but less Ansible-ish method , if using the "ip" command in a shell task to get the status. Fr my project,I only need to shutdown some ports once, and after that they will never be used again.  

---

