Search code examples
windowsnetwork-programmingansiblegateway

Retrieve Windows Host Default Gateway for use in powershell script


--- 
- name: "Gateway Checker"
  hosts: vms_windows
  gather_facts: True
  tasks: 
  - debug: var=ansible_interfaces

Gets me:

TASK [debug]     *******************************************************************
ok: [dc00.domain.test] => {
    "ansible_interfaces": [
        {
            "default_gateway": "192.168.10.254", 
            "dns_domain": null, 
            "interface_index": 12, 
            "interface_name": "Intel(R) PRO/1000 MT Network Connection"
        }
    ]
}

But going for:

- debug: var=ansible_interfaces.default_gateway

Gets me:

"ansible_interfaces.default_gateway": "VARIABLE IS NOT DEFINED!"

Any ideas on what I might be doing wrong here?


Solution

  • Well, you may use following form in this case:

    - debug: var=ansible_interfaces[0].default_gateway
    

    or

    - debug: var=ansible_interfaces.0.default_gateway
    

    Please, pay your attention that it is array with interfaces, and this code will work correctly if you have only one interface. But in general if you have few interfaces default gateway can be in other interface, and you should loop through array in that situation.