diff --git a/plugins/filter/next_nth_usable.py b/plugins/filter/next_nth_usable.py index 1f766d0..4927148 100644 --- a/plugins/filter/next_nth_usable.py +++ b/plugins/filter/next_nth_usable.py @@ -118,17 +118,23 @@ def next_nth_usable(value, offset): Returns the next nth usable ip within a network described by value. """ try: + v = None vtype = ipaddr(value, "type") if vtype == "address": v = ipaddr(value, "cidr") elif vtype == "network": v = ipaddr(value, "subnet") - v = netaddr.IPNetwork(v) + if v is not None: + v = netaddr.IPNetwork(v) + else: + return False except Exception: return False + if not isinstance(offset, int): raise AnsibleFilterError("Must pass in an integer") + if v.size > 1: first_usable, last_usable = _first_last(v) nth_ip = int(netaddr.IPAddress(int(v.ip) + offset)) diff --git a/plugins/filter/previous_nth_usable.py b/plugins/filter/previous_nth_usable.py index 6f767dd..2533bb7 100644 --- a/plugins/filter/previous_nth_usable.py +++ b/plugins/filter/previous_nth_usable.py @@ -117,13 +117,18 @@ def previous_nth_usable(value, offset): Returns the previous nth usable ip within a network described by value. """ try: + v = None vtype = ipaddr(value, "type") if vtype == "address": v = ipaddr(value, "cidr") elif vtype == "network": v = ipaddr(value, "subnet") - v = netaddr.IPNetwork(v) + if v is not None: + v = netaddr.IPNetwork(v) + else: + return False + except Exception: return False diff --git a/tests/integration/targets/utils_keep_keys/tasks/complex1.yaml b/tests/integration/targets/utils_keep_keys/tasks/complex1.yaml index 61050b9..7bb6df0 100644 --- a/tests/integration/targets/utils_keep_keys/tasks/complex1.yaml +++ b/tests/integration/targets/utils_keep_keys/tasks/complex1.yaml @@ -4,126 +4,85 @@ file: complex.yaml - name: Test keep_keys - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}" - target: - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } tags: keep_keys block: + - name: Setup data as facts for complex keep_keys integration test l1 + ansible.builtin.set_fact: + l1: + - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - name: Test keep_keys debug ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool + msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}" + register: result + - name: Test keep_keys assert ansible.builtin.assert: - that: result == target + that: result['msg'] == keep['l1'] + + - name: Debug l1 for starts_with 'p' + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}" + register: result_l1_starts_with_p + + - name: Assert result dicts l1 for starts_with 'p' + ansible.builtin.assert: + that: + - result_l1_starts_with_p['msg'] == keep['l1_starts_with_p'] + + - name: Debug l1 for ends_with + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}" + register: result_l1_ends_with + + - name: Assert result dicts l1 for ends_with + ansible.builtin.assert: + that: + - result_l1_ends_with['msg'] == keep['l1_ends_with'] + + - name: Debug l1 for ends_with '2' + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}" + register: result_l1_ends_with_2 + + - name: Assert result dicts l1 for ends_with '2' + ansible.builtin.assert: + that: + - result_l1_ends_with_2['msg'] == keep['l1_ends_with_2'] + + - name: Test keep_keys regex + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}" + register: result_l1_regex + + - name: Assert result dicts l1 for regex + ansible.builtin.assert: + that: + - result_l1_regex['msg'] == keep['l1_regex'] - name: Test map keep_keys - vars: - result: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}" - target: - - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } - - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } - - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } tags: map_keep_keys block: + - name: Setup data as facts for complex keep integration test l2 + ansible.builtin.set_fact: + l2: + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - name: Test map keep_keys debug ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool + msg: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}" + register: result + - name: Test map keep_keys assert ansible.builtin.assert: - that: result == target - -- name: Test keep_keys starts_with - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}" - target: - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } - tags: keep_keys_starts_with_2 - block: - - name: Test keep_keys starts_with debug - ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool - - name: Test keep_keys starts_with assert - ansible.builtin.assert: - that: result == target - -- name: Test keep_keys starts_with 'p' - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['p'], matching_parameter='starts_with') }}" - target: - - { p1: a, p2: a, p3: a } - - { p1: b, p2: b, p3: b } - - { p1: c, p2: c, p3: c } - tags: keep_keys_starts_with_1 - block: - - name: Test keep_keys starts_with 'p' debug - ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool - - name: Test keep_keys starts_with 'p' assert - ansible.builtin.assert: - that: result == target - -- name: Test keep_keys ends_with - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}" - target: - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } - tags: keep_keys_ends_with_2 - block: - - name: Test keep_keys end_with debug - ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool - - name: Test keep_keys end_with assert - ansible.builtin.assert: - that: result == target - -- name: Test keep_keys ends_with '2' - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}" - target: - - { p2: a } - - { p2: b } - - { p2: c } - tags: keep_keys_ends_with_1 - block: - - name: Test keep_keys end_with '2' debug - ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool - - name: Test keep_keys end_with '2' assert - ansible.builtin.assert: - that: result == target - -- name: Test keep_keys regex - vars: - result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}" - target: - - { p1: a, p2: a } - - { p1: b, p2: b } - - { p1: c, p2: c } - tags: keep_keys_regex - block: - - name: Test keep_keys regex debug - ansible.builtin.debug: - var: result|to_yaml - when: debug_test|d(false)|bool - - name: Test keep_keys regex assert - ansible.builtin.assert: - that: result == target + that: result['msg'] == keep['l2'] diff --git a/tests/integration/targets/utils_keep_keys/vars/complex.yaml b/tests/integration/targets/utils_keep_keys/vars/complex.yaml index 8bdec7c..aebfc06 100644 --- a/tests/integration/targets/utils_keep_keys/vars/complex.yaml +++ b/tests/integration/targets/utils_keep_keys/vars/complex.yaml @@ -1,19 +1,36 @@ --- -l1: - - { p1: a, p2: a, p3: a } - - { p1: b, p2: b, p3: b } - - { p1: c, p2: c, p3: c } -l2: - - - { p1: a, p2: a, p3: a } - - { p1: b, p2: b, p3: b } - - { p1: c, p2: c, p3: c } - - - { p1: a, p2: a, p3: a } - - { p1: b, p2: b, p3: b } - - { p1: c, p2: c, p3: c } - - - { p1: a, p2: a, p3: a } - - { p1: b, p2: b, p3: b } - - { p1: c, p2: c, p3: c } -l3: - - { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a } - - { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b } - - { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c } +keep: + l1: + - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + l2: + - - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + - - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + - - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + l3: + - { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a } + - { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b } + - { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c } + l1_starts_with_p: + - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + l1_ends_with: + - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c } + l1_ends_with_2: + - { p2: a } + - { p2: b } + - { p2: c } + l1_regex: + - { p1: a, p2: a } + - { p1: b, p2: b } + - { p1: c, p2: c }