Skip to content

Instantly share code, notes, and snippets.

@MatteoMori
Forked from tuxfight3r/validate_zone.py
Created January 22, 2018 09:30
Show Gist options
  • Select an option

  • Save MatteoMori/09da77d2cfcbd0dfacb9f327da51a56a to your computer and use it in GitHub Desktop.

Select an option

Save MatteoMori/09da77d2cfcbd0dfacb9f327da51a56a to your computer and use it in GitHub Desktop.

Revisions

  1. @tuxfight3r tuxfight3r revised this gist Jan 21, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions validate_zone.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    #!/usr/bin/python
    # file path: filter_plugins/validate_zone.py
    # script will give out the corresponding zone the ip belongs to
    # if the ip doesnt match the range, it will give the default "NA"
    # Date: 21/01/2018
    # Author: Mohan Balasundaram
    # File path: filter_plugins/validate_zone.py
    # Purpose: script to validate ip address and issue the corresponding zone name
    # the ip belongs to and if the ip doesnt match the range, it will give the default "NA"

    import ipaddress

  2. @tuxfight3r tuxfight3r revised this gist Jan 21, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions validate_zone.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/python
    # file path: filter_plugins/validate_zone.py
    # script will give out the corresponding zone the ip belongs to
    # if the ip doesnt match the range, it will give the default "NA"

    import ipaddress

  3. @tuxfight3r tuxfight3r created this gist Jan 21, 2018.
    32 changes: 32 additions & 0 deletions validate_zone.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/python
    # file path: filter_plugins/validate_zone.py

    import ipaddress

    class FilterModule(object):
    def filters(self):
    return {
    'zone_test': self.zone_test,
    'zone_lookup': self.zone_lookup

    }

    def zone_test(self, a_variable):
    a_new_variable = 'IP ADDRESS:' + a_variable
    return a_new_variable

    def zone_lookup(self, ip_var,zone_value=""):
    zones_dict = { "AZ1": ipaddress.ip_network(u'192.168.1.0/24'),
    "AZ2": ipaddress.ip_network(u'192.168.2.0/24'),
    "AZ3": ipaddress.ip_network(u'192.168.3.0/24') }

    altip_var=ipaddress.ip_address(unicode(ip_var))
    for zone in zones_dict.keys():
    ip_array=list(zones_dict[zone].hosts())
    if altip_var in ip_array:
    zone_value=zone

    if zone_value == "":
    return "NA"
    else:
    return zone_value
    24 changes: 24 additions & 0 deletions wtest.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    $ cat wtest.yml
    ---
    - hosts: localhost
    connection: local
    vars:
    - ip1: "192.168.1.10"
    - ip2: "192.168.2.10"
    - ip3: "192.168.3.10"
    - ip4: "192.168.4.10"

    tasks:
    - debug: var="{{item}}"
    with_items:
    - ip1
    - ip2
    - ip3

    - debug: msg="IP {{item}} - {{item|zone_lookup}}"
    with_items:
    - "{{ip1}}"
    - "{{ip2}}"
    - "{{ip3}}"
    - "{{ip4}}"

    43 changes: 43 additions & 0 deletions zoutput.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    $ ansible-playbook wtest.yml
    [WARNING]: Could not match supplied host pattern, ignoring: all

    [WARNING]: provided hosts list is empty, only localhost is available


    PLAY [localhost] **************************************************************************************************************************************************************************************************

    TASK [debug] ******************************************************************************************************************************************************************************************************
    ok: [localhost] => (item=ip1) => {
    "ip1": "192.168.1.10",
    "item": "ip1"
    }
    ok: [localhost] => (item=ip2) => {
    "ip2": "192.168.2.10",
    "item": "ip2"
    }
    ok: [localhost] => (item=ip3) => {
    "ip3": "192.168.3.10",
    "item": "ip3"
    }

    TASK [debug] ******************************************************************************************************************************************************************************************************
    ok: [localhost] => (item=192.168.1.10) => {
    "item": "192.168.1.10",
    "msg": "IP 192.168.1.10 - AZ1"
    }
    ok: [localhost] => (item=192.168.2.10) => {
    "item": "192.168.2.10",
    "msg": "IP 192.168.2.10 - AZ2"
    }
    ok: [localhost] => (item=192.168.3.10) => {
    "item": "192.168.3.10",
    "msg": "IP 192.168.3.10 - AZ3"
    }
    ok: [localhost] => (item=192.168.4.10) => {
    "item": "192.168.4.10",
    "msg": "IP 192.168.4.10 - NA"
    }

    PLAY RECAP ********************************************************************************************************************************************************************************************************
    localhost : ok=2 changed=0 unreachable=0 failed=0