Iterating over all network interfaces in Ansible 2016-04-21

Table of Contents

Description: Getting information about every network interface on a system with ansible is not that obvious. Here's the solution I found.

Reading Time: 1

Tags: Ansible DevOps

The problem

Ansible keeps a list of all network interfaces in the ansible_interfaces fact. Sometimes you need to iterate over all of these, but access the ansible_eth0 or other interface data, but doing so is non-obvious. Here’s what I found that works:

- name: debug print all interface ipv4 data
  when: "{{ hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined }}"
     msg="{{ hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] | pprint }}"
     - "{{ ansible_interfaces | map('replace', '-','_') | list }}"