Need to get the network interface status.

42 views
Skip to first unread message

lish...@gmail.com

unread,
Nov 28, 2016, 7:05:30 PM11/28/16
to Ansible Project
Number of network interfaces varies in our environment and we have atleast 5 variants, hardcoding is not an option.
This playbook need to check {{ ansible_interfaces }} and then get the status of all available interfaces active status.
from the example snippet below
 "ansible_eth1": {
                                    "active": true,
                                    "device": "eth1",

I am trying to set_fact which should return the dict like
"eth1status_active" = "yes"
eth0status_active" = "no"
...
ethxstatus_active = 'xxx'


I have lost just before the last task


 tasks:
  - name: get filesystem
    setup: gather_subset=network

  - name: set fact network_interfaces
    set_fact: network_interfaces={{ ansible_interfaces }}

  - name: set fact
    set_fact: iface_item="ansible_{{ item }}"
    with_items: "{{ ansible_interfaces }}"
    register: iface_result

  - name: make a list
    set_fact: iface_list="{{ iface_result.results | map(attribute='ansible_facts.iface_item') | list }}"

  Please help me continue playing ..


Kai Stian Olstad

unread,
Nov 29, 2016, 10:30:15 AM11/29/16
to ansible...@googlegroups.com
You only need one task like this:

- set_fact:
"{{ item | replace('-', '_') }}status_active": "{{
hostvars[inventory_hostname]['ansible_' + item | replace('-',
'_')].active }}"
with_items: "{{ ansible_interfaces }}"

The replace is needed since dash in variable names is not allowed.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages