Skip to content

Instantly share code, notes, and snippets.

@tomgidden
Created February 2, 2015 17:42
Show Gist options
  • Save tomgidden/4b764eb77eaf736c1e7b to your computer and use it in GitHub Desktop.
Save tomgidden/4b764eb77eaf736c1e7b to your computer and use it in GitHub Desktop.
Ansible: get IP address of host by regular-expression
# file: filter_plugins/match_ip.py
from ansible import errors
import re
def match_ip(ips, pattern):
if type(ips) != list:
raise errors.AnsibleFilterError("|failed expects a list")
for ip in ips:
if re.match(pattern, ip):
return ip
return ''
class FilterModule (object):
def filters(self):
return {"match_ip": match_ip}
---
- hosts: default
tasks:
- name: Get IP
debug: "msg={{ ansible_all_ipv4_addresses|match_ip('^192.168.1.') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment