|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +ANSIBLE_METADATA = { |
| 4 | + 'metadata_version': '1.1', |
| 5 | + 'status': ['preview'], |
| 6 | + 'supported_by': 'community' |
| 7 | +} |
| 8 | + |
| 9 | +DOCUMENTATION = ''' |
| 10 | +--- |
| 11 | +module: phpipam_freeip |
| 12 | +
|
| 13 | +short_description: Grabs a free ip address from a subnet using phpipam api. |
| 14 | +
|
| 15 | +version_added: "2.4" |
| 16 | +
|
| 17 | +description: |
| 18 | + - "Simple module that grabs a free ip address from a subnet" |
| 19 | +
|
| 20 | +options: |
| 21 | + subnet: |
| 22 | + description: |
| 23 | + - Subnet in phpipam that you need to obtain a free ip address from |
| 24 | + required: true |
| 25 | + url: |
| 26 | + description: |
| 27 | + - Address of phpipam server including api and app name |
| 28 | + required: true |
| 29 | + username: |
| 30 | + description: |
| 31 | + - Username for initial authentication to phpipam api |
| 32 | + required: true |
| 33 | + password: |
| 34 | + description: |
| 35 | + - Password for initial authentication to phpipam api |
| 36 | +
|
| 37 | +author: |
| 38 | + - Carson Anderson (@rcanderson23) |
| 39 | +''' |
| 40 | + |
| 41 | +EXAMPLES = ''' |
| 42 | +# Pass in a message |
| 43 | +- name: obtain a free ip address from phpipam |
| 44 | + phpipam_getfree: |
| 45 | + subnet: '192.168.0.0/24' |
| 46 | + url: ipam.domain.tld/api/app_name/ |
| 47 | + username: user |
| 48 | + password: password |
| 49 | + delegate_to: localhost |
| 50 | + register: ip_address |
| 51 | +''' |
| 52 | + |
| 53 | +RETURN = ''' |
| 54 | +changed: |
| 55 | + description: If module successfully obtained an ip |
| 56 | + returned: success |
| 57 | + type: bool |
| 58 | +subnet_id: |
| 59 | + description: Subnet id of the ip address obtained |
| 60 | + returned: success |
| 61 | + type: str |
| 62 | + sample: '7' |
| 63 | +ip: |
| 64 | + description: IP address obtained from PHPIPAM |
| 65 | + returned: success |
| 66 | + type: str |
| 67 | + sample: '192.168.0.7' |
| 68 | +token: |
| 69 | + description: Authentication token used after initial user/pass authentication |
| 70 | + returned: success |
| 71 | + type: str |
| 72 | +
|
| 73 | +
|
| 74 | +
|
| 75 | +''' |
| 76 | + |
| 77 | +from ansible.module_utils.basic import AnsibleModule |
| 78 | +import requests |
| 79 | + |
| 80 | +def run_module(): |
| 81 | + module_args = dict( |
| 82 | + subnet=dict(type='str', required=True), |
| 83 | + url=dict(type='str', required=False), |
| 84 | + username=dict(type='str', required=False), |
| 85 | + password=dict(type='str', required=False, no_log=True), |
| 86 | + ) |
| 87 | + |
| 88 | + result = dict( |
| 89 | + changed=False |
| 90 | + ) |
| 91 | + |
| 92 | + module = AnsibleModule( |
| 93 | + argument_spec=module_args, |
| 94 | + supports_check_mode=False |
| 95 | + ) |
| 96 | + |
| 97 | + s = requests.Session() |
| 98 | + |
| 99 | + token = get_token(module.params['url'], |
| 100 | + module.params['username'], module.params['password']) |
| 101 | + result['token'] = token |
| 102 | + s.headers.update({'token': '%s' % token}) |
| 103 | + |
| 104 | + subnet_id = get_subnet_id(s, module.params['url'], module.params['subnet']) |
| 105 | + |
| 106 | + result['subnet_id'] = subnet_id |
| 107 | + |
| 108 | + ip = get_free_ip(s, module.params['url'], subnet_id) |
| 109 | + |
| 110 | + result['ip'] = ip |
| 111 | + result['changed'] = True |
| 112 | + |
| 113 | + module.exit_json(**result) |
| 114 | + |
| 115 | +def get_token(url, username, password): |
| 116 | + url += 'user/' |
| 117 | + r = requests.post(url, auth=(username, password)) |
| 118 | + return r.json().get('data').get('token') |
| 119 | + |
| 120 | +def get_subnet_id(session, url, subnet): |
| 121 | + url += 'subnets/cidr/%s/' % subnet |
| 122 | + subnet_id = session.get(url) |
| 123 | + return subnet_id.json().get('data')[0].get('id') |
| 124 | + |
| 125 | +def get_free_ip(session, url, subnet_id): |
| 126 | + payload = {'hostname': 'test-api', 'subnetId': '%s' % subnet_id} |
| 127 | + url += 'addresses/first_free/' |
| 128 | + free_ip = session.post(url, payload) |
| 129 | + return free_ip.json().get('data') |
| 130 | + |
| 131 | +def main(): |
| 132 | + run_module() |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
0 commit comments