Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-08-03T15:50:48.173Z
Answers: 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.
Answers: majorgear
Answers: CC BY-SA 4.0
Answers: 2023-08-03T17:48:50.750Z
Answers: 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.
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2023-08-09T13:21:42.307Z
Answers: 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.
Answers: majorgear
Answers: CC BY-SA 4.0
Answers: 2021-09-07T08:10:44.650Z
Answers: 2021-09-07T08:10:44.650Z
Answers: Bruce Becker
Body: <p>I want to check status of Ethernet port whether it is up or down on remote server using Ansible?</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-09-08T15:22:01.607Z
Comments: 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.
Comments: Community
Tags: <ansible><automation>
Title: How to check the status of Ethernet port using ansible?
UserDisplayName: user30584
ThreadText: # 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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables).
  
  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": "<address>",
        "broadcast": "<address>",
        "netmask": "<netmask>",
        "network": "<network>"
      },
      "ipv6": [
        {
          "address": "<ipv6_network>",
          "prefix": "64",
          "scope": "link"
        }
      ],
      "macaddress": "<hw_address>",
      "module": "<kernel_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](https://docs.ansible.com/ansible/2.9/user_guide/playbooks_filters.html#json-query-filter)
  
  If you need to check that the device is ready from a remote execution, you could use a [`wait_for`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html) 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.  
  
  ---
  
  