0
votes

I want to use ansible facts to filter the network interfaces where their module is set to igb.

I know that I can run this command and get the network facts for all interfaces. (inc the module for each interface)

ansible -m setup -a 'gather_subset=network' host.so.com

I've tried using these variables below and doing a debug on the variable, hoping to see the names of the interfaces with that module, but i can only get it to output all facts for interfaces with module=ibg instead of just the device name.

allNetworkModules: "{{ ansible_facts | dict2items | selectattr('value.module', 'defined') | map(attribute='value') | list }}"

ixgbe_module: "{{ ansible_facts | dict2items | selectattr('value.module', 'defined') | selectattr('value.module', 'equalto', 'igb') | list }}"

Can anyone tell me how I can filter to just get the interface names please?

Thank you

1

1 Answers

0
votes

If you want to see just the names of all the interfaces using a particular module, you could do something like this:

- hosts: localhost
  gather_facts: true
  vars:
    module_name: e1000e
  tasks:
    - debug:
        msg: >-
          {{
            ansible_facts | dict2items
            | selectattr('value.module', 'defined')
            | selectattr('value.module', 'equalto', module_name)
            | map(attribute='key')
            | list
          }}

On my system, which has a single e1000e nic, this will output:

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "eth0"
    ]
}