Skip to content

Instantly share code, notes, and snippets.

@mrlesmithjr
Created July 28, 2019 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlesmithjr/5be60038b585e73e6923423301724b56 to your computer and use it in GitHub Desktop.
Save mrlesmithjr/5be60038b585e73e6923423301724b56 to your computer and use it in GitHub Desktop.
Ansible IP request loops mockup
#!/usr/bin/python
from random import getrandbits
from ipaddress import IPv4Network, IPv4Address
from ansible.module_utils.basic import AnsibleModule
def main():
fields = {'subnet': {'required': True, 'type': 'str'}}
module = AnsibleModule(argument_spec=fields)
response = get_ip(module)
module.exit_json(changed=False, meta=response)
def get_ip(module):
subnet = IPv4Network(module.params['subnet'])
# subnet.max_prefixlen contains 32 for IPv4 subnets and 128 for IPv6 subnets
# subnet.prefixlen is 24 in this case, so we'll generate only 8 random bits
bits = getrandbits(subnet.max_prefixlen - subnet.prefixlen)
# here, we combine the subnet and the random bits
# to get an IP address from the previously specified subnet
addr = IPv4Address(subnet.network_address + bits)
addr_str = str(addr)
return addr_str
if __name__ == '__main__':
main()
---
- block:
- name: Obtaining random IP - Round {{ loop_number }}
get_ip:
subnet: "{{ subnets|random }}"
register: random_ip
- name: Obtained IP {{ random_ip['meta'] }}
set_fact:
ip_address: "{{ random_ip['meta'] }}"
- name: Checking whether {{ ip_address }} is present on network
command: ping -c 4 "{{ ip_address }}"
register: ping_result
failed_when: ping_result['rc']|int == 0
- name: Setting ip_avail to true
set_fact:
ip_avail: true
rescue:
- debug:
msg: "IP Address: {{ ip_address }} already in use!"
when: not ip_avail|bool
---
- hosts: localhost
connection: local
gather_facts: false
vars:
loop_count: 5
subnets:
- 10.0.0.0/8
- 172.16.24.0/24
- 192.168.0.0/16
tasks:
- name: Setting default ip_avail variable to false
set_fact:
ip_avail: false
- name: Looping {{ loop_count }} until IP found
include_tasks: ip_request_check.yml
loop: "{{ range(0, loop_count)|list }}"
loop_control:
loop_var: loop_number
- name: Displaying available IP address
debug:
msg: "IP Address: {{ ip_address }} available for use!"
@mrlesmithjr
Copy link
Author

ansible-playbook ip_request_loops.yml
 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


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

TASK [Setting default ip_avail variable to false] ********************************************************************************************************************************
ok: [localhost]

TASK [Looping 5 until IP found] **************************************************************************************************************************************************
included: /Users/larrysmithjr/projects/Ansible/loops/ip_request_check.yml for localhost
included: /Users/larrysmithjr/projects/Ansible/loops/ip_request_check.yml for localhost
included: /Users/larrysmithjr/projects/Ansible/loops/ip_request_check.yml for localhost
included: /Users/larrysmithjr/projects/Ansible/loops/ip_request_check.yml for localhost
included: /Users/larrysmithjr/projects/Ansible/loops/ip_request_check.yml for localhost

TASK [Obtaining random IP - Round 0] *********************************************************************************************************************************************
ok: [localhost]

TASK [Obtained IP 10.170.11.139] *************************************************************************************************************************************************
ok: [localhost]

TASK [Checking whether 10.170.11.139 is present on network] **********************************************************************************************************************
changed: [localhost]

TASK [Setting ip_avail to true] **************************************************************************************************************************************************
ok: [localhost]

TASK [Obtaining random IP - Round 1] *********************************************************************************************************************************************
skipping: [localhost]

TASK [Obtained IP {{ random_ip['meta'] }}] ***************************************************************************************************************************************
skipping: [localhost]

TASK [Checking whether 10.170.11.139 is present on network] **********************************************************************************************************************
skipping: [localhost]

TASK [Setting ip_avail to true] **************************************************************************************************************************************************
skipping: [localhost]

TASK [Obtaining random IP - Round 2] *********************************************************************************************************************************************
skipping: [localhost]

TASK [Obtained IP {{ random_ip['meta'] }}] ***************************************************************************************************************************************
skipping: [localhost]

TASK [Checking whether 10.170.11.139 is present on network] **********************************************************************************************************************
skipping: [localhost]

TASK [Setting ip_avail to true] **************************************************************************************************************************************************
skipping: [localhost]

TASK [Obtaining random IP - Round 3] *********************************************************************************************************************************************
skipping: [localhost]

TASK [Obtained IP {{ random_ip['meta'] }}] ***************************************************************************************************************************************
skipping: [localhost]

TASK [Checking whether 10.170.11.139 is present on network] **********************************************************************************************************************
skipping: [localhost]

TASK [Setting ip_avail to true] **************************************************************************************************************************************************
skipping: [localhost]

TASK [Obtaining random IP - Round 4] *********************************************************************************************************************************************
skipping: [localhost]

TASK [Obtained IP {{ random_ip['meta'] }}] ***************************************************************************************************************************************
skipping: [localhost]

TASK [Checking whether 10.170.11.139 is present on network] **********************************************************************************************************************
skipping: [localhost]

TASK [Setting ip_avail to true] **************************************************************************************************************************************************
skipping: [localhost]

TASK [Displaying available IP address] *******************************************************************************************************************************************
ok: [localhost] => {
    "msg": "IP Address: 10.170.11.139 available for use!"
}

PLAY RECAP ***********************************************************************************************************************************************************************
localhost                  : ok=11   changed=1    unreachable=0    failed=0    skipped=16   rescued=0    ignored=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment