1

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

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.
    – Community Bot
    Sep 8, 2021 at 15:22

1 Answer 1

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": "<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

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.

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.
    – majorgear
    Aug 3, 2023 at 15:50
  • 1
    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. Aug 3, 2023 at 17:48
  • 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
    Aug 9, 2023 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.