Created
February 2, 2015 17:42
-
-
Save tomgidden/4b764eb77eaf736c1e7b to your computer and use it in GitHub Desktop.
Ansible: get IP address of host by regular-expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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