-
Notifications
You must be signed in to change notification settings - Fork 7
/
phpipam_freeip.py
173 lines (157 loc) · 4.74 KB
/
phpipam_freeip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Carson Anderson <rcanderson23@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: phpipam_freeip
author: "Carson Anderson (@rcanderson23)"
short_description: Obtain free ip address
requirements: []
version_added: "2.8"
description:
- Obtain first free ip address in a subnet from phpIPAM instance
options:
username:
description:
- username that has permission to access phpIPAM API
required: True
password:
description:
- password for username provided
required: True
url:
description:
- API url for phpIPAM instance
required: True
subnet:
description:
- Subnet to obtain ip address from.
- Must be in CIDR format.
required: True
section:
description:
- Section name that the subnet resides in.
required: True
hostname:
description:
- Hostname displayed next to address in phpIPAM.
required: False
description:
description:
- Optional description displayed next to address in phpIPAM.
required: False
'''
EXAMPLES = '''
- name: Obtain next free ip address
phpipam_freeip:
username: username
password: secret
url: "https://ipam.domain.tld/api/app/"
subnet: "192.168.10.0/24"
hostname: "newhost"
description: "optional description"
register: new_ip
- name: print obtained ip address
debug:
msg: "IP Obtained {{ new_ip.ip }}"
'''
RETURN = '''
ip:
description: string containing ip address
returned: success
type: string
output:
description: dictionary containing phpIPAM response
returned: success
type: complex
contains:
code:
description: HTTP response code
returned: success
type: int
sample: 201
success:
description: True or False depending on if ip was successfully obtained
returned: success
type: bool
sample: True
time:
description: Amount of time operation took.
returned: success
type: float
sample: 0.015
message:
description: Response message of what happened
returned: success
type: string
sample: "Address created"
data:
description: Contains ip address obtained
returned: success
type: string
sample: "192.168.10.2"
id:
description: ID of address created
returned: success
type: string
sample: "206"
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.phpipam import PhpIpamWrapper
import urllib
import json
def main():
module = AnsibleModule(
argument_spec=dict(
username=dict(type=str, required=True),
password=dict(type=str, required=True, no_log=True),
url=dict(type=str, required=True),
subnet=dict(type=str, required=True),
section=dict(type=str, required=True),
hostname=dict(type=str, required=False),
description=dict(type=str, required=False)
),
supports_check_mode=False
)
result = dict(
changed=False
)
username = module.params['username']
password = module.params['password']
url = module.params['url']
subnet = module.params['subnet']
section = module.params['section']
hostname = module.params['hostname']
description = module.params['description']
session = PhpIpamWrapper(username, password, url)
try:
session.create_session()
except AttributeError:
module.fail_json(msg='Error getting authorization token', **result)
subnet_response = session.get_subnet(subnet, section)
if subnet_response:
url += 'addresses/first_free/'
subnet_id = session.get_subnet_id(subnet, section)
payload = urllib.urlencode({'subnetId': subnet_id,
'hostname': hostname,
'description': description})
free_ip = json.load(session.post(url, payload))
if free_ip['success']:
ip = free_ip['data']
result['ip'] = ip
result['output'] = free_ip
module.exit_json(**result)
else:
module.fail_json(msg='Subnet is full', **result)
else:
module.fail_json(msg='Subnet or section doesn\'t exist', **result)
if __name__ == '__main__':
main()