Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

 

# Copyright: (c) 2019, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net> 

# 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 = r''' 

--- 

module: meraki_management_interface 

short_description: Configure Meraki management interfaces 

version_added: "2.10" 

description: 

- Allows for configuration of management interfaces on Meraki devices. 

notes: 

- C(WAN2) parameter is only valid for MX appliances. 

- C(enabled) can only be set to C(not configured) for MS switches. 

options: 

state: 

description: 

- Specifies whether configuration template information should be queried, modified, or deleted. 

choices: ['absent', 'query', 'present'] 

default: query 

type: str 

org_name: 

description: 

- Name of organization containing the configuration template. 

type: str 

org_id: 

description: 

- ID of organization associated to a configuration template. 

type: str 

net_name: 

description: 

- Name of the network to bind or unbind configuration template to. 

type: str 

net_id: 

description: 

- ID of the network to bind or unbind configuration template to. 

type: str 

serial: 

description: 

- serial number of the device to configure. 

type: str 

wan1: 

description: 

- Management interface details for management interface. 

aliases: [mgmt1] 

type: dict 

suboptions: 

wan_enabled: 

description: 

- States whether the management interface is enabled. 

type: str 

choices: [disabled, enabled, not configured] 

using_static_ip: 

description: 

- Configures the interface to use static IP or DHCP. 

type: bool 

static_ip: 

description: 

- IP address assigned to Management interface. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_gateway_ip: 

description: 

- IP address for default gateway. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_subnet_mask: 

description: 

- Netmask for static IP address. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_dns: 

description: 

- DNS servers to use. 

- Allows for a maximum of 2 addresses. 

type: list 

vlan: 

description: 

- VLAN number to use for the management network. 

type: int 

wan2: 

description: 

- Management interface details for management interface. 

type: dict 

aliases: [mgmt2] 

suboptions: 

wan_enabled: 

description: 

- States whether the management interface is enabled. 

type: str 

choices: [disabled, enabled, not configured] 

using_static_ip: 

description: 

- Configures the interface to use static IP or DHCP. 

type: bool 

static_ip: 

description: 

- IP address assigned to Management interface. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_gateway_ip: 

description: 

- IP address for default gateway. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_subnet_mask: 

description: 

- Netmask for static IP address. 

- Valid only if C(using_static_ip) is C(True). 

type: str 

static_dns: 

description: 

- DNS servers to use. 

- Allows for a maximum of 2 addresses. 

type: list 

vlan: 

description: 

- VLAN number to use for the management network. 

type: int 

 

author: 

- Kevin Breit (@kbreit) 

extends_documentation_fragment: meraki 

''' 

 

EXAMPLES = r''' 

- name: Set WAN2 as static IP 

meraki_management_interface: 

auth_key: abc123 

state: present 

org_name: YourOrg 

net_id: YourNetId 

serial: AAAA-BBBB-CCCC 

wan2: 

wan_enabled: enabled 

using_static_ip: yes 

static_ip: 192.168.16.195 

static_gateway_ip: 192.168.16.1 

static_subnet_mask: 255.255.255.0 

static_dns: 

- 1.1.1.1 

vlan: 1 

delegate_to: localhost 

 

- name: Query management information 

meraki_management_interface: 

auth_key: abc123 

state: query 

org_name: YourOrg 

net_id: YourNetId 

serial: AAAA-BBBB-CCCC 

delegate_to: localhost 

''' 

 

RETURN = r''' 

data: 

description: Information about queried object. 

returned: success 

type: complex 

contains: 

wan1: 

description: Management configuration for WAN1 interface 

returned: success 

type: complex 

contains: 

wan_enabled: 

description: Enabled state of interface 

returned: success 

type: string 

sample: enabled 

using_static_ip: 

description: Boolean value of whether static IP assignment is used on interface 

returned: success 

type: bool 

sample: True 

static_ip: 

description: Assigned static IP 

returned: only if static IP assignment is used 

type: string 

sample: 192.0.1.2 

static_gateway_ip: 

description: Assigned static gateway IP 

returned: only if static IP assignment is used 

type: string 

sample: 192.0.1.1 

static_subnet_mask: 

description: Assigned netmask for static IP 

returned: only if static IP assignment is used 

type: string 

sample: 255.255.255.0 

static_dns: 

description: List of DNS IP addresses 

returned: only if static IP assignment is used 

type: list 

sample: ["1.1.1.1"] 

vlan: 

description: VLAN tag id of management VLAN 

returned: success 

type: int 

sample: 2 

wan2: 

description: Management configuration for WAN1 interface 

returned: success 

type: complex 

contains: 

wan_enabled: 

description: Enabled state of interface 

returned: success 

type: string 

sample: enabled 

using_static_ip: 

description: Boolean value of whether static IP assignment is used on interface 

returned: success 

type: bool 

sample: True 

static_ip: 

description: Assigned static IP 

returned: only if static IP assignment is used 

type: string 

sample: 192.0.1.2 

static_gateway_ip: 

description: Assigned static gateway IP 

returned: only if static IP assignment is used 

type: string 

sample: 192.0.1.1 

static_subnet_mask: 

description: Assigned netmask for static IP 

returned: only if static IP assignment is used 

type: string 

sample: 255.255.255.0 

static_dns: 

description: List of DNS IP addresses 

returned: only if static IP assignment is used 

type: list 

sample: ["1.1.1.1"] 

vlan: 

description: VLAN tag id of management VLAN 

returned: success 

type: int 

sample: 2 

''' 

 

from ansible.module_utils.basic import AnsibleModule, json 

from ansible.module_utils.common.dict_transformations import recursive_diff 

from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec 

 

 

def main(): 

# define the available arguments/parameters that a user can pass to 

# the module 

 

int_arg_spec = dict(wan_enabled=dict(type='str', choices=['enabled', 'disabled', 'not configured']), 

using_static_ip=dict(type='bool'), 

static_ip=dict(type='str'), 

static_gateway_ip=dict(type='str'), 

static_subnet_mask=dict(type='str'), 

static_dns=dict(type='list', element='str'), 

vlan=dict(type='int'), 

) 

 

argument_spec = meraki_argument_spec() 

argument_spec.update(state=dict(type='str', choices=['absent', 'query', 'present'], default='query'), 

net_name=dict(type='str'), 

net_id=dict(type='str'), 

serial=dict(type='str', required=True), 

wan1=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt1']), 

wan2=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt2']), 

) 

 

# the AnsibleModule object will be our abstraction working with Ansible 

# this includes instantiation, a couple of common attr would be the 

# args/params passed to the execution, as well as if the module 

# supports check mode 

module = AnsibleModule(argument_spec=argument_spec, 

supports_check_mode=True, 

) 

meraki = MerakiModule(module, function='management_interface') 

meraki.params['follow_redirects'] = 'all' 

 

query_urls = {'management_interface': '/networks/{net_id}/devices/{serial}/managementInterfaceSettings'} 

 

meraki.url_catalog['get_one'].update(query_urls) 

 

if meraki.params['net_id'] and meraki.params['net_name']: 

meraki.fail_json('net_id and net_name are mutually exclusive.') 

297 ↛ 298,   297 ↛ 3052 missed branches: 1) line 297 didn't jump to line 298, because the condition on line 297 was never true, 2) line 297 didn't jump to line 305, because the condition on line 297 was never false if meraki.params['state'] == 'present': 

interfaces = ('wan1', 'wan2') 

299 ↛ 305line 299 didn't jump to line 305, because the loop on line 299 didn't complete for interface in interfaces: 

300 ↛ 299,   300 ↛ 3012 missed branches: 1) line 300 didn't jump to line 299, because the condition on line 300 was never false, 2) line 300 didn't jump to line 301, because the condition on line 300 was never true if meraki.params[interface] is not None: 

if meraki.params[interface]['using_static_ip'] is True: 

302 ↛ 299,   302 ↛ 3032 missed branches: 1) line 302 didn't jump to line 299, because the condition on line 302 was never false, 2) line 302 didn't jump to line 303, because the condition on line 302 was never true if len(meraki.params[interface]['static_dns']) > 2: 

meraki.fail_json("Maximum number of static DNS addresses is 2.") 

 

payload = dict() 

 

if meraki.params['state'] == 'present': 

interfaces = ('wan1', 'wan2') 

309 ↛ 325line 309 didn't jump to line 325, because the loop on line 309 didn't complete for interface in interfaces: 

310 ↛ 309line 310 didn't jump to line 309, because the condition on line 310 was never false if meraki.params[interface] is not None: 

wan_int = {'wanEnabled': meraki.params[interface]['wan_enabled'], 

'usingStaticIp': meraki.params[interface]['using_static_ip'], 

} 

314 ↛ 315,   314 ↛ 3162 missed branches: 1) line 314 didn't jump to line 315, because the condition on line 314 was never true, 2) line 314 didn't jump to line 316, because the condition on line 314 was never false if meraki.params[interface]['vlan'] is not None: 

wan_int['vlan'] = meraki.params[interface]['vlan'] 

316 ↛ 317,   316 ↛ 3212 missed branches: 1) line 316 didn't jump to line 317, because the condition on line 316 was never true, 2) line 316 didn't jump to line 321, because the condition on line 316 was never false if meraki.params[interface]['using_static_ip'] is True: 

wan_int['staticIp'] = meraki.params[interface]['static_ip'] 

wan_int['staticGatewayIp'] = meraki.params[interface]['static_gateway_ip'] 

wan_int['staticSubnetMask'] = meraki.params[interface]['static_subnet_mask'] 

wan_int['staticDns'] = meraki.params[interface]['static_dns'] 

payload[interface] = wan_int 

 

# manipulate or modify the state as needed (this is going to be the 

# part where your module will do what it needs to do) 

org_id = meraki.params['org_id'] 

326 ↛ 328line 326 didn't jump to line 328, because the condition on line 326 was never false if meraki.params['org_name']: 

org_id = meraki.get_org_id(meraki.params['org_name']) 

net_id = meraki.params['net_id'] 

329 ↛ 333line 329 didn't jump to line 333, because the condition on line 329 was never false if net_id is None: 

nets = meraki.get_nets(org_id=org_id) 

net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) 

 

if meraki.params['state'] == 'query': 

path = meraki.construct_path('get_one', net_id=net_id, custom={'serial': meraki.params['serial']}) 

response = meraki.request(path, method='GET') 

336 ↛ 363line 336 didn't jump to line 363, because the condition on line 336 was never false if meraki.status == 200: 

meraki.result['data'] = response 

338 ↛ 363line 338 didn't jump to line 363, because the condition on line 338 was never false elif meraki.params['state'] == 'present': 

path = meraki.construct_path('get_one', net_id=net_id, custom={'serial': meraki.params['serial']}) 

# meraki.fail_json(path) 

original = meraki.request(path, method='GET') 

if meraki.is_update_required(original, payload): 

if meraki.check_mode is True: 

diff = recursive_diff(original, payload) 

original.update(payload) 

meraki.result['diff'] = {'before': diff[0], 

'after': diff[1]} 

meraki.result['data'] = original 

meraki.result['changed'] = True 

meraki.exit_json(**meraki.result) 

response = meraki.request(path, method='PUT', payload=json.dumps(payload)) 

352 ↛ 363line 352 didn't jump to line 363, because the condition on line 352 was never false if meraki.status == 200: 

diff = recursive_diff(original, response) 

meraki.result['diff'] = {'before': diff[0], 

'after': diff[1]} 

meraki.result['data'] = response 

meraki.result['changed'] = True 

else: 

meraki.result['data'] = original 

 

# in the event of a successful module execution, you will want to 

# simple AnsibleModule.exit_json(), passing the key/value results 

meraki.exit_json(**meraki.result) 

 

 

366 ↛ exitline 366 didn't exit the module, because the condition on line 366 was never falseif __name__ == '__main__': 

main()