From ce17f5e1ec1cbbf1959e512c6b03e886b57a3440 Mon Sep 17 00:00:00 2001 From: Matt Stephenson Date: Mon, 23 Feb 2015 12:51:00 -0800 Subject: [PATCH 1/2] Fix indentation --- lib/ansible/runner/filter_plugins/ipaddr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner/filter_plugins/ipaddr.py b/lib/ansible/runner/filter_plugins/ipaddr.py index bcb19b16fde..0a86e5200e5 100644 --- a/lib/ansible/runner/filter_plugins/ipaddr.py +++ b/lib/ansible/runner/filter_plugins/ipaddr.py @@ -152,9 +152,9 @@ def _multicast_query(v, value): return value def _net_query(v): - if v.size > 1: - if v.ip == v.network: - return str(v.network) + '/' + str(v.prefixlen) + if v.size > 1: + if v.ip == v.network: + return str(v.network) + '/' + str(v.prefixlen) def _netmask_query(v): if v.size > 1: From a2ea785d694242dbd0c3ff1ad065b0f53d1da664 Mon Sep 17 00:00:00 2001 From: Matt Stephenson Date: Mon, 23 Feb 2015 13:42:03 -0800 Subject: [PATCH 2/2] Add function to ipaddr for getting the nth host within a network --- lib/ansible/runner/filter_plugins/ipaddr.py | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/ansible/runner/filter_plugins/ipaddr.py b/lib/ansible/runner/filter_plugins/ipaddr.py index 0a86e5200e5..5d9d6e31367 100644 --- a/lib/ansible/runner/filter_plugins/ipaddr.py +++ b/lib/ansible/runner/filter_plugins/ipaddr.py @@ -555,6 +555,38 @@ def ipsubnet(value, query = '', index = 'x'): return False +# Returns the nth host within a network described by value. +# Usage: +# +# - address or address/prefix | nthhost(nth) +# returns the nth host within the given network +def nthhost(value, query=''): + ''' Get the nth host within a given network ''' + try: + vtype = ipaddr(value, 'type') + if vtype == 'address': + v = ipaddr(value, 'cidr') + elif vtype == 'network': + v = ipaddr(value, 'subnet') + + value = netaddr.IPNetwork(v) + except: + return False + + if not query: + return False + + try: + vsize = ipaddr(v, 'size') + nth = int(query) + if value.size > nth: + return value[nth] + + except ValueError: + return False + + return False + # ---- HWaddr / MAC address filters ---- @@ -612,6 +644,7 @@ class FilterModule(object): 'ipv4': ipv4, 'ipv6': ipv6, 'ipsubnet': ipsubnet, + 'nthhost': nthhost, # MAC / HW addresses 'hwaddr': hwaddr,