Home ansible get the interface name by providing an IP address
Reply: 0

ansible get the interface name by providing an IP address

user6897
1#
user6897 Published in September 19, 2018, 9:13 am

I have two interfaces in machine(linux). One interface has been addressed and second is down without any IP. Now I would like to get the interface name matched with IP what I provide as value in ansible.

I was trying something like this:

    - name: interface name from provides IP
  set_fact:
    interface_name="{{ item }}"
  with_items:
     - "{{ ansible_interfaces | map('replace', '-','_') | list }}"
  when: hostvars[ansible_fqdn]['ansible_'~item]['ipv4']['address'] == PROVIDED_IP

It works good when all interfaces have IP address but problem is when one interface has no IP then I have error: 'dict object' has no attribute 'ipv4'

Is possible to get interface name without getting errors?

share|improve this question
  • Perhaps something like this? when: hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined and hostvars[ansible_fqdn]['ansible_'~item]['ipv4']['address'] == PROVIDED_IP. I'm not sure if it supports Python's .get(name, default) which would be better in my opinion. – Harvey Apr 16 at 18:05
  • I tried only when: hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined and error the same "The conditional check 'hostvars[ansible_fqdn]['ansible_'~item]['ipv4']['address'] is defined' failed. The error was: error while evaluating conditional (hostvars[ansible_fqdn]['ansible_'~item]['ipv4']['address'] is defined): 'dict object' has no attribute 'ipv4'" – stefan nowak Apr 16 at 18:16

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352305 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO