Created
July 28, 2019 04:34
-
-
Save mrlesmithjr/5be60038b585e73e6923423301724b56 to your computer and use it in GitHub Desktop.
Ansible IP request loops mockup
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
#!/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() |
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
--- | |
- 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 |
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: 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!" |
Author
mrlesmithjr
commented
Jul 28, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment