Kathy, your "when:" condition is incorrect. "not 'lo'" will always be false, so likewise

  when: ansible_facts[item] is defined and not 'lo'

will also always be false.

On 1/17/23 7:10 AM, Kathy L wrote:
Thanks for that info - very interesting.

I'm trying to start arpwatch for each interface using the following:

    name: Start arpwatch
    command: arpwatch -i {{ item }}
    loop:  "{{ ansible_interfaces }}"
    when:  ansible_facts[item] is defined and not 'lo'

I need to provide for eth interfaces as well as ens192... etc. Any idea what I can do to ignore these VLAN interfaces?



On Tuesday, January 17, 2023 at 3:45:38 AM UTC-5 [email protected] wrote:

    Hi 👋


    It’s the VLAN ID for the sub-interface which ansible_facts is
    showing for eth0_1


    You must use the iproute 2 package with ip addr not net-tools


    If you want to modify this then you will need set_facts magic;


    To access variables of other hosts, you should enable fact caching
    <http://docs.ansible.com/ansible/playbooks_variables.html#fact-caching>.


    In playbook files, you should add in:

    gather_facts: True

    #to update facts.


    The ansible_interfaces fact lists all of the existing network
    interfaces.


    Some hints to get an interface when you know more information:


    var:

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

    allNetworkInterfaces_variant2: "{{ ansible_facts.interfaces |
    map('extract', ansible_facts ) | list }}"

    interfaceWithKnownIp: "{{ ansible_facts | dict2items |
    selectattr('value.ipv4', 'defined') |
    selectattr('value.ipv4.address', 'equalto', myKnowIpV4) | first }}"

    interfaceWithKnownIp_fromVar: "{{ allNetworkInterfaces |
    selectattr('ipv4.address', 'equalto', myKnowIpV4) | first }}"

    interfacesWithPartialKnowMac: "{{ allNetworkInterfaces |
    selectattr('macaddress', 'match', knownMacPrefix~'.*') | list }}"

    interfacesWitKnowType: "{{ allNetworkInterfaces |
    selectattr('type', 'equalto', knownType) |
    sort(attribute='device') | list }}"

    # extended on 2020-10-28

    queryForKnownIpv6: "[*].{device: device, ipv4: ipv4, ipv6: ipv6[?
    address == 'fe80::a00:27ff:fe38:ad36']}[?ipv6])" # string must be
    in ' # sorry, only partial interface info, did not find out how to
    return all info directly

    interfacesWithKnownIpv6: '{{ allNetworkInterfaces |
    json_query(queryForKnownIpv6) | first }}'

    queryForKnownIpv4_linux: "[?ipv4.address == '{{ myKnownIpV4
    }}']}[?ipv4])" # string must be in '

    interfacesWithKnownIp_variantJsonQuery: '{{ allNetworkInterfaces |
    json_query(queryForKnownIpv4_linux) | first }}'

    some short explications:


      * dict2item
        
<https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#dict-filter>
 because
        selectattr
        <http://jinja.pocoo.org/docs/dev/templates/#selectattr> expects
        an array

      * map(attribute='...')
        <http://jinja.pocoo.org/docs/dev/templates/#map> to unpack
        this array
      * map('extract', ...)
        
<https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#extracting-values-from-containers>
 to
        extract interfaces from ansible_facts
      * ansible_facts.interfaces is the same as ansible_interfaces
      * json_query()
        
<https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-json-data-json-queries>
 for
        flexible selecting and unpacking, an alternative variant with
        `map()? is welcome


    the ansible_interfaces fact contains a list of all the network
    interfaces and they appear to be in order (i.e., the first
    ethernet interface would have been called eth0, the second eth1,
    etc). With a little set_fact magic:


    - name: define traditional ethernet facts

    set_fact:

    ansible_eth: "{% set ansible_eth = ansible_eth|default([]) +
    [hostvars[inventory_hostname]['ansible_' + item]] %}{{
    ansible_eth|list }}"

    when: hostvars[inventory_hostname]['ansible_' + item]['type'] ==
    'ether'

    with_items:

    - "{{ hostvars[inventory_hostname]['ansible_interfaces'] }}"

    This loops over all of the ansible_interfaces entries for the
    current machine and builds a list of
    the hostvars[inventory_hostname]['ansible_' + item] entries that
    have a type equal to "ether".

    Thus now ansible_eth.0 and ansible_eth.1 should be roughly
    equivalent to the old ansible_eth0 and ansible_eth1 respectively.


    Best,

    S.W

    On Thu, 12 Jan 2023 at 11:24, Kathy L <[email protected]> wrote:

        I'm using ansible-core 1.18.  When I gather facts on a device,
        it shows the interfaces of eth0, eth0_1, eth1 and lo. The
        results of "ip a" show only eth0, eth1 and lo interfaces . 
        When I try to start arpwatch for each interface (other than
        lo), it fails on eth0_1, which makes sense to me.  Why does
        ansible_facts interfaces show eth0_1?

--
        You received this message because you are subscribed to the
        Google Groups "Ansible Project" group.
        To unsubscribe from this group and stop receiving emails from
        it, send an email to [email protected].
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/ansible-project/61377336-30f3-4b80-b783-82ff963de609n%40googlegroups.com
        
<https://groups.google.com/d/msgid/ansible-project/61377336-30f3-4b80-b783-82ff963de609n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/78ec98fd-b8ea-4e8b-9ece-638ed08e96e3n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/78ec98fd-b8ea-4e8b-9ece-638ed08e96e3n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Todd

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ff093e62-6686-cfdb-6535-ebe1ffe3b911%40gmail.com.

Reply via email to