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

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

#!/usr/bin/python 

# 

# This file is part of Ansible 

# 

# Ansible is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# Ansible is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 

# 

 

ANSIBLE_METADATA = {'metadata_version': '1.1', 

'status': ['preview'], 

'supported_by': 'network'} 

 

DOCUMENTATION = """ 

--- 

module: nxos_interface 

extends_documentation_fragment: nxos 

version_added: "2.1" 

short_description: Manages physical attributes of interfaces. 

description: 

- Manages physical attributes of interfaces of NX-OS switches. 

author: 

- Jason Edelman (@jedelman8) 

- Trishna Guha (@trishnaguha) 

notes: 

- Tested against NXOSv 7.3.(0)D1(1) on VIRL 

- This module is also used to create logical interfaces such as 

svis and loopbacks. 

- Be cautious of platform specific idiosyncrasies. For example, 

when you default a loopback interface, the admin state toggles 

on certain versions of NX-OS. 

- The M(nxos_overlay_global) C(anycast_gateway_mac) attribute must be 

set before setting the C(fabric_forwarding_anycast_gateway) property. 

options: 

name: 

description: 

- Full name of interface, i.e. Ethernet1/1, port-channel10. 

required: true 

default: null 

aliases: [interface] 

interface_type: 

description: 

- Interface type to be unconfigured from the device. 

required: false 

default: null 

choices: ['loopback', 'portchannel', 'svi', 'nve'] 

version_added: 2.2 

speed: 

description: 

- Interface link speed. 

version_added: 2.5 

admin_state: 

description: 

- Administrative state of the interface. 

required: false 

default: up 

choices: ['up','down'] 

description: 

description: 

- Interface description. 

required: false 

default: null 

mode: 

description: 

- Manage Layer 2 or Layer 3 state of the interface. 

This option is supported for ethernet and portchannel interface. 

required: false 

default: null 

choices: ['layer2','layer3'] 

mtu: 

description: 

- MTU for a specific interface. Must be an even number between 576 and 9216. 

required: false 

version_added: 2.5 

ip_forward: 

description: 

- Enable/Disable ip forward feature on SVIs. 

required: false 

default: null 

choices: ['enable','disable'] 

version_added: 2.2 

fabric_forwarding_anycast_gateway: 

description: 

- Associate SVI with anycast gateway under VLAN configuration mode. 

required: false 

default: null 

choices: ['true','false'] 

version_added: 2.2 

duplex: 

description: 

- Interface link status 

default: auto 

choices: ['full', 'half', 'auto'] 

version_added: 2.5 

tx_rate: 

description: 

- Transmit rate in bits per second (bps). 

version_added: 2.5 

rx_rate: 

description: 

- Receiver rate in bits per second (bps). 

version_added: 2.5 

neighbors: 

description: 

- Check the operational state of given interface C(name) for LLDP neighbor. 

- The following suboptions are available. 

suboptions: 

host: 

description: 

- "LLDP neighbor host for given interface C(name)." 

port: 

description: 

- "LLDP neighbor port to which given interface C(name) is connected." 

version_added: 2.5 

aggregate: 

description: List of Interfaces definitions. 

version_added: 2.5 

state: 

description: 

- Specify desired state of the resource. 

required: true 

default: present 

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

delay: 

description: 

- Time in seconds to wait before checking for the operational state on remote 

device. This wait is applicable for operational state arguments. 

default: 10 

""" 

 

EXAMPLES = """ 

- name: Ensure an interface is a Layer 3 port and that it has the proper description 

nxos_interface: 

name: Ethernet1/1 

description: 'Configured by Ansible' 

mode: layer3 

 

- name: Admin down an interface 

nxos_interface: 

name: Ethernet2/1 

admin_state: down 

 

- name: Remove all loopback interfaces 

nxos_interface: 

name: loopback 

state: absent 

 

- name: Remove all logical interfaces 

nxos_interface: 

interface_type: "{{ item }} " 

state: absent 

loop: 

- loopback 

- portchannel 

- svi 

- nve 

 

- name: Admin up all loopback interfaces 

nxos_interface: 

name: loopback 0-1023 

admin_state: up 

 

- name: Admin down all loopback interfaces 

nxos_interface: 

name: looback 0-1023 

admin_state: down 

 

- name: Check neighbors intent arguments 

nxos_interface: 

name: Ethernet2/3 

neighbors: 

- port: Ethernet2/3 

host: abc.mycompany.com 

 

- name: Add interface using aggregate 

nxos_interface: 

aggregate: 

- { name: Ethernet0/1, mtu: 256, description: test-interface-1 } 

- { name: Ethernet0/2, mtu: 516, description: test-interface-2 } 

duplex: full 

speed: 100 

state: present 

 

- name: Delete interface using aggregate 

nxos_interface: 

aggregate: 

- name: Loopback9 

- name: Loopback10 

state: absent 

 

- name: Check intent arguments 

nxos_interface: 

name: Ethernet0/2 

state: up 

tx_rate: ge(0) 

rx_rate: le(0) 

""" 

 

RETURN = """ 

commands: 

description: command list sent to the device 

returned: always 

type: list 

sample: 

- interface Ethernet2/3 

- mtu 1500 

- speed 10 

""" 

 

import re 

import time 

 

from copy import deepcopy 

 

from ansible.module_utils.network.nxos.nxos import load_config, run_commands 

from ansible.module_utils.network.nxos.nxos import nxos_argument_spec 

from ansible.module_utils.basic import AnsibleModule 

from ansible.module_utils.network.common.utils import conditional, remove_default_spec 

 

 

def execute_show_command(command, module): 

if 'show run' not in command: 

output = 'json' 

else: 

output = 'text' 

cmds = [{ 

'command': command, 

'output': output, 

}] 

body = run_commands(module, cmds, check_rc=False) 

240 ↛ 241line 240 didn't jump to line 241, because the condition on line 240 was never true if body and "Invalid" in body[0]: 

return [] 

else: 

return body 

 

 

def search_obj_in_list(name, lst): 

247 ↛ 251line 247 didn't jump to line 251, because the loop on line 247 didn't complete for o in lst: 

if o['name'] == name: 

return o 

 

return None 

 

 

def get_interface_type(interface): 

"""Gets the type of interface 

""" 

257 ↛ 259line 257 didn't jump to line 259, because the condition on line 257 was never false if interface.upper().startswith('ET'): 

return 'ethernet' 

elif interface.upper().startswith('VL'): 

return 'svi' 

elif interface.upper().startswith('LO'): 

return 'loopback' 

elif interface.upper().startswith('MG'): 

return 'management' 

elif interface.upper().startswith('MA'): 

return 'management' 

elif interface.upper().startswith('PO'): 

return 'portchannel' 

elif interface.upper().startswith('NV'): 

return 'nve' 

else: 

return 'unknown' 

 

 

def get_interfaces_dict(module): 

"""Gets all active interfaces on a given switch 

""" 

try: 

body = execute_show_command('show interface', module)[0] 

except IndexError: 

return {} 

 

interfaces = { 

'ethernet': [], 

'svi': [], 

'loopback': [], 

'management': [], 

'portchannel': [], 

'nve': [], 

'unknown': [] 

} 

 

if body: 

interface_list = body['TABLE_interface']['ROW_interface'] 

for index in interface_list: 

intf = index['interface'] 

intf_type = get_interface_type(intf) 

interfaces[intf_type].append(intf) 

 

return interfaces 

 

 

def normalize_interface(name): 

"""Return the normalized interface name 

""" 

306 ↛ 307line 306 didn't jump to line 307, because the condition on line 306 was never true if not name: 

return None 

 

def _get_number(name): 

digits = '' 

for char in name: 

if char.isdigit() or char == '/': 

digits += char 

return digits 

 

316 ↛ 318line 316 didn't jump to line 318, because the condition on line 316 was never false if name.lower().startswith('et'): 

if_type = 'Ethernet' 

elif name.lower().startswith('vl'): 

if_type = 'Vlan' 

elif name.lower().startswith('lo'): 

if_type = 'loopback' 

elif name.lower().startswith('po'): 

if_type = 'port-channel' 

elif name.lower().startswith('nv'): 

if_type = 'nve' 

else: 

if_type = None 

 

number_list = name.split(' ') 

330 ↛ 331line 330 didn't jump to line 331, because the condition on line 330 was never true if len(number_list) == 2: 

number = number_list[-1].strip() 

else: 

number = _get_number(name) 

 

335 ↛ 338line 335 didn't jump to line 338, because the condition on line 335 was never false if if_type: 

proper_interface = if_type + number 

else: 

proper_interface = name 

 

return proper_interface 

 

 

def get_vlan_interface_attributes(name, intf_type, module): 

""" Returns dictionary that has two k/v pairs: 

admin_state & description if not an svi, returns None 

""" 

command = 'show run interface {0} all'.format(name) 

try: 

body = execute_show_command(command, module)[0] 

except IndexError: 

return None 

if body: 

command_list = body.split('\n') 

desc = None 

admin_state = 'down' 

for each in command_list: 

if 'description' in each: 

desc = each.lstrip().split("description")[1].lstrip() 

elif 'no shutdown' in each: 

admin_state = 'up' 

return dict(description=desc, admin_state=admin_state) 

else: 

return None 

 

 

def get_interface_type_removed_cmds(interfaces): 

commands = [] 

 

for interface in interfaces: 

if interface != 'Vlan1': 

commands.append('no interface {0}'.format(interface)) 

 

return commands 

 

 

def get_admin_state(admin_state): 

command = '' 

if admin_state == 'up': 

command = 'no shutdown' 

380 ↛ 382line 380 didn't jump to line 382, because the condition on line 380 was never false elif admin_state == 'down': 

command = 'shutdown' 

return command 

 

 

def is_default_interface(name, module): 

"""Checks to see if interface exists and if it is a default config 

""" 

command = 'show run interface {0}'.format(name) 

 

try: 

body = execute_show_command(command, module)[0] 

except (IndexError, TypeError) as e: 

body = '' 

 

395 ↛ 406line 395 didn't jump to line 406, because the condition on line 395 was never false if body: 

raw_list = body.split('\n') 

found = False 

398 ↛ 403line 398 didn't jump to line 403, because the loop on line 398 didn't complete for line in raw_list: 

if line.startswith('interface'): 

found = True 

if found and line and not line.startswith('interface'): 

return False 

return True 

 

else: 

return 'DNE' 

 

 

def add_command_to_interface(interface, cmd, commands): 

if interface not in commands: 

commands.append(interface) 

commands.append(cmd) 

 

 

def map_obj_to_commands(updates, module): 

commands = list() 

commands2 = list() 

want, have = updates 

 

args = ('speed', 'description', 'duplex', 'mtu') 

for w in want: 

name = w['name'] 

mode = w['mode'] 

ip_forward = w['ip_forward'] 

fabric_forwarding_anycast_gateway = w['fabric_forwarding_anycast_gateway'] 

admin_state = w['admin_state'] 

state = w['state'] 

interface_type = w['interface_type'] 

del w['state'] 

430 ↛ 433line 430 didn't jump to line 433, because the condition on line 430 was never false if name: 

w['interface_type'] = None 

 

obj_in_have = search_obj_in_list(name, have) 

is_default = is_default_interface(name, module) 

435 ↛ 438line 435 didn't jump to line 438, because the condition on line 435 was never false if name: 

interface = 'interface ' + name 

 

438 ↛ 439line 438 didn't jump to line 439, because the condition on line 438 was never true if interface_type and state == 'present': 

module.fail_json(msg='The interface_type param can be used only with state absent.') 

 

441 ↛ 442line 441 didn't jump to line 442, because the condition on line 441 was never true if state == 'absent': 

if obj_in_have: 

commands.append('no interface {0}'.format(name)) 

elif interface_type: 

intfs = get_interfaces_dict(module)[interface_type] 

cmds = get_interface_type_removed_cmds(intfs) 

commands.extend(cmds) 

 

449 ↛ 518line 449 didn't jump to line 518, because the condition on line 449 was never false elif state == 'present': 

450 ↛ 491line 450 didn't jump to line 491, because the condition on line 450 was never false if obj_in_have: 

for item in args: 

candidate = w.get(item) 

 

if candidate and candidate != obj_in_have.get(item): 

cmd = item + ' ' + str(candidate) 

add_command_to_interface(interface, cmd, commands) 

 

458 ↛ 459line 458 didn't jump to line 459, because the condition on line 458 was never true if mode == 'layer2' and mode != obj_in_have.get('mode'): 

add_command_to_interface(interface, 'switchport', commands) 

460 ↛ 461line 460 didn't jump to line 461, because the condition on line 460 was never true elif mode == 'layer3' and mode != obj_in_have.get('mode'): 

add_command_to_interface(interface, 'no switchport', commands) 

 

if admin_state == 'up' and admin_state != obj_in_have.get('admin_state'): 

add_command_to_interface(interface, 'no shutdown', commands) 

elif admin_state == 'down' and admin_state != obj_in_have.get('admin_state'): 

add_command_to_interface(interface, 'shutdown', commands) 

 

468 ↛ 469line 468 didn't jump to line 469, because the condition on line 468 was never true if ip_forward == 'enable' and ip_forward != obj_in_have.get('ip_forward'): 

add_command_to_interface(interface, 'ip forward', commands) 

470 ↛ 471line 470 didn't jump to line 471, because the condition on line 470 was never true elif ip_forward == 'disable' and ip_forward != obj_in_have.get('ip forward'): 

add_command_to_interface(interface, 'no ip forward', commands) 

 

473 ↛ 475line 473 didn't jump to line 475, because the condition on line 473 was never true if (fabric_forwarding_anycast_gateway is True and 

obj_in_have.get('fabric_forwarding_anycast_gateway') is True): 

add_command_to_interface(interface, 'fabric forwarding mode anycast-gateway', commands) 

 

477 ↛ 479line 477 didn't jump to line 479, because the condition on line 477 was never true elif (fabric_forwarding_anycast_gateway is False and 

obj_in_have.get('fabric_forwarding_anycast_gateway') is False): 

add_command_to_interface(interface, 'no fabric forwarding mode anycast-gateway', commands) 

 

481 ↛ 421line 481 didn't jump to line 421, because the condition on line 481 was never false if name and get_interface_type(name) == 'ethernet': 

if mode != obj_in_have.get('mode'): 

admin_state = w.get('admin_state') or obj_in_have.get('admin_state') 

484 ↛ 421line 484 didn't jump to line 421, because the condition on line 484 was never false if admin_state: 

c1 = 'interface {0}'.format(normalize_interface(w['name'])) 

c2 = get_admin_state(admin_state) 

commands2.append(c1) 

commands2.append(c2) 

 

else: 

commands.append(interface) 

for item in args: 

candidate = w.get(item) 

if candidate: 

commands.append(item + ' ' + str(candidate)) 

 

if mode == 'layer2': 

commands.append('switchport') 

elif mode == 'layer3': 

commands.append('no switchport') 

 

if admin_state == 'up': 

commands.append('no shutdown') 

elif admin_state == 'down': 

commands.append('shutdown') 

 

if ip_forward == 'enable': 

commands.append('ip forward') 

elif ip_forward == 'disable': 

commands.append('no ip forward') 

 

if fabric_forwarding_anycast_gateway is True: 

commands.append('fabric forwarding mode anycast-gateway') 

 

elif fabric_forwarding_anycast_gateway is False: 

commands.append('no fabric forwarding mode anycast-gateway') 

 

elif state == 'default': 

if is_default is False: 

commands.append('default interface {0}'.format(name)) 

elif is_default == 'DNE': 

module.exit_json(msg='interface you are trying to default does not exist') 

 

return commands, commands2 

 

 

def map_params_to_obj(module): 

obj = [] 

aggregate = module.params.get('aggregate') 

if aggregate: 

for item in aggregate: 

for key in item: 

if item.get(key) is None: 

item[key] = module.params[key] 

 

d = item.copy() 

name = d['name'] 

d['name'] = normalize_interface(name) 

obj.append(d) 

 

else: 

obj.append({ 

'name': normalize_interface(module.params['name']), 

'description': module.params['description'], 

'speed': module.params['speed'], 

'mode': module.params['mode'], 

'mtu': module.params['mtu'], 

'duplex': module.params['duplex'], 

'ip_forward': module.params['ip_forward'], 

'fabric_forwarding_anycast_gateway': module.params['fabric_forwarding_anycast_gateway'], 

'admin_state': module.params['admin_state'], 

'state': module.params['state'], 

'interface_type': module.params['interface_type'], 

'tx_rate': module.params['tx_rate'], 

'rx_rate': module.params['rx_rate'], 

'neighbors': module.params['neighbors'] 

}) 

 

return obj 

 

 

def map_config_to_obj(want, module): 

objs = list() 

 

for w in want: 

obj = dict(name=None, description=None, admin_state=None, speed=None, 

mtu=None, mode=None, duplex=None, interface_type=None, 

ip_forward=None, fabric_forwarding_anycast_gateway=None) 

 

command = 'show interface {0}'.format(w['name']) 

try: 

body = execute_show_command(command, module)[0] 

except IndexError: 

return list() 

575 ↛ 649line 575 didn't jump to line 649, because the condition on line 575 was never false if body: 

try: 

interface_table = body['TABLE_interface']['ROW_interface'] 

except KeyError: 

return list() 

 

581 ↛ 649line 581 didn't jump to line 649, because the condition on line 581 was never false if interface_table: 

582 ↛ 583line 582 didn't jump to line 583, because the condition on line 582 was never true if interface_table.get('eth_mode') == 'fex-fabric': 

module.fail_json(msg='nxos_interface does not support interfaces with mode "fex-fabric"') 

 

intf_type = get_interface_type(w['name']) 

 

587 ↛ 591line 587 didn't jump to line 591, because the condition on line 587 was never false if intf_type in ['portchannel', 'ethernet']: 

588 ↛ 589line 588 didn't jump to line 589, because the condition on line 588 was never true if not interface_table.get('eth_mode'): 

obj['mode'] = 'layer3' 

 

591 ↛ 619line 591 didn't jump to line 619, because the condition on line 591 was never false if intf_type == 'ethernet': 

obj['name'] = normalize_interface(interface_table.get('interface')) 

obj['admin_state'] = interface_table.get('admin_state') 

obj['description'] = interface_table.get('desc') 

obj['mtu'] = int(interface_table.get('eth_mtu')) 

obj['duplex'] = interface_table.get('eth_duplex') 

speed = interface_table.get('eth_speed') 

598 ↛ 599line 598 didn't jump to line 599, because the condition on line 598 was never true if 'auto' in speed: 

obj['speed'] = speed 

else: 

obj['speed'] = int(speed.split()[0]) 

mode = interface_table.get('eth_mode') 

603 ↛ 604line 603 didn't jump to line 604, because the condition on line 603 was never true if mode in ('access', 'trunk'): 

obj['mode'] = 'layer2' 

605 ↛ 608line 605 didn't jump to line 608, because the condition on line 605 was never false elif mode in ('routed', 'layer3'): 

obj['mode'] = 'layer3' 

 

command = 'show run interface {0}'.format(obj['name']) 

body = execute_show_command(command, module)[0] 

610 ↛ 611line 610 didn't jump to line 611, because the condition on line 610 was never true if 'ip forward' in body: 

obj['ip_forward'] = 'enable' 

else: 

obj['ip_forward'] = 'disable' 

614 ↛ 615line 614 didn't jump to line 615, because the condition on line 614 was never true if 'fabric forwarding mode anycast-gateway' in body: 

obj['fabric_forwarding_anycast_gateway'] = True 

else: 

obj['fabric_forwarding_anycast_gateway'] = False 

 

elif intf_type == 'svi': 

obj['name'] = normalize_interface(interface_table.get('interface')) 

attributes = get_vlan_interface_attributes(obj['name'], intf_type, module) 

obj['admin_state'] = str(attributes.get('admin_state', 

'nxapibug')) 

obj['description'] = str(attributes.get('description', 

'nxapi_bug')) 

 

command = 'show run interface {0}'.format(obj['name']) 

body = execute_show_command(command, module)[0] 

if 'ip forward' in body: 

obj['ip_forward'] = 'enable' 

else: 

obj['ip_forward'] = 'disable' 

if 'fabric forwarding mode anycast-gateway' in body: 

obj['fabric_forwarding_anycast_gateway'] = True 

else: 

obj['fabric_forwarding_anycast_gateway'] = False 

 

elif intf_type in ('loopback', 'management', 'nve'): 

obj['name'] = normalize_interface(interface_table.get('interface')) 

obj['admin_state'] = interface_table.get('admin_state') 

obj['description'] = interface_table.get('desc') 

 

elif intf_type == 'portchannel': 

obj['name'] = normalize_interface(interface_table.get('interface')) 

obj['admin_state'] = interface_table.get('admin_state') 

obj['description'] = interface_table.get('desc') 

obj['mode'] = interface_table.get('eth_mode') 

 

objs.append(obj) 

 

return objs 

 

 

def check_declarative_intent_params(module, want): 

failed_conditions = [] 

have_neighbors = None 

for w in want: 

want_tx_rate = w.get('tx_rate') 

want_rx_rate = w.get('rx_rate') 

want_neighbors = w.get('neighbors') 

 

time.sleep(module.params['delay']) 

 

cmd = [{'command': 'show interface {0}'.format(w['name']), 'output': 'text'}] 

output = run_commands(module, cmd, check_rc=False) 

666 ↛ 669line 666 didn't jump to line 669, because the condition on line 666 was never false if output: 

out = output[0] 

else: 

out = '' 

if want_tx_rate: 

match = re.search(r'output rate (\d+)', out, re.M) 

have_tx_rate = None 

 

674 ↛ 677line 674 didn't jump to line 677, because the condition on line 674 was never false if match: 

have_tx_rate = match.group(1) 

 

677 ↛ 680line 677 didn't jump to line 680, because the condition on line 677 was never false if have_tx_rate is None or not conditional(want_tx_rate, have_tx_rate.strip(), cast=int): 

failed_conditions.append('tx_rate ' + want_tx_rate) 

 

if want_rx_rate: 

match = re.search(r'input rate (\d+)', out, re.M) 

have_rx_rate = None 

 

684 ↛ 687line 684 didn't jump to line 687, because the condition on line 684 was never false if match: 

have_rx_rate = match.group(1) 

 

687 ↛ 690line 687 didn't jump to line 690, because the condition on line 687 was never false if have_rx_rate is None or not conditional(want_rx_rate, have_rx_rate.strip(), cast=int): 

failed_conditions.append('rx_rate ' + want_rx_rate) 

 

690 ↛ 691line 690 didn't jump to line 691, because the condition on line 690 was never true if want_neighbors: 

have_host = [] 

have_port = [] 

if have_neighbors is None: 

cmd = [{'command': 'show lldp neighbors interface {0} detail'.format(w['name']), 'output': 'text'}] 

output = run_commands(module, cmd, check_rc=False) 

if output: 

have_neighbors = output[0] 

else: 

have_neighbors = '' 

if have_neighbors and 'Total entries displayed: 0' not in have_neighbors: 

for line in have_neighbors.strip().split('\n'): 

if line.startswith('Port Description'): 

have_port.append(line.split(': ')[1]) 

if line.startswith('System Name'): 

have_host.append(line.split(': ')[1]) 

 

for item in want_neighbors: 

host = item.get('host') 

port = item.get('port') 

if host and host not in have_host: 

failed_conditions.append('host ' + host) 

if port and port not in have_port: 

failed_conditions.append('port ' + port) 

 

return failed_conditions 

 

 

def main(): 

""" main entry point for module execution 

""" 

neighbors_spec = dict( 

host=dict(), 

port=dict() 

) 

 

element_spec = dict( 

name=dict(aliases=['interface']), 

admin_state=dict(default='up', choices=['up', 'down']), 

description=dict(), 

speed=dict(), 

mode=dict(choices=['layer2', 'layer3']), 

mtu=dict(), 

duplex=dict(choices=['full', 'half', 'auto']), 

interface_type=dict(choices=['loopback', 'portchannel', 'svi', 'nve']), 

ip_forward=dict(choices=['enable', 'disable']), 

fabric_forwarding_anycast_gateway=dict(type='bool'), 

tx_rate=dict(), 

rx_rate=dict(), 

neighbors=dict(type='list', elements='dict', options=neighbors_spec), 

delay=dict(default=10, type='int'), 

state=dict(choices=['absent', 'present', 'default'], default='present') 

) 

 

aggregate_spec = deepcopy(element_spec) 

aggregate_spec['name'] = dict(required=True) 

 

# remove default in aggregate spec, to handle common arguments 

remove_default_spec(aggregate_spec) 

 

argument_spec = dict( 

aggregate=dict(type='list', elements='dict', options=aggregate_spec, 

mutually_exclusive=[['name', 'interface_type']]) 

) 

 

argument_spec.update(element_spec) 

argument_spec.update(nxos_argument_spec) 

 

required_one_of = [['name', 'aggregate']] 

mutually_exclusive = [['name', 'aggregate'], 

['name', 'interface_type']] 

 

module = AnsibleModule(argument_spec=argument_spec, 

required_one_of=required_one_of, 

mutually_exclusive=mutually_exclusive, 

supports_check_mode=True) 

warnings = list() 

 

result = {'changed': False} 

769 ↛ 770line 769 didn't jump to line 770, because the condition on line 769 was never true if warnings: 

result['warnings'] = warnings 

 

want = map_params_to_obj(module) 

have = map_config_to_obj(want, module) 

 

commands = [] 

commands1, commands2 = map_obj_to_commands((want, have), module) 

commands.extend(commands1) 

 

if commands: 

780 ↛ 790line 780 didn't jump to line 790, because the condition on line 780 was never false if not module.check_mode: 

load_config(module, commands) 

result['changed'] = True 

# if the mode changes from L2 to L3, the admin state 

# seems to change after the API call, so adding a second API 

# call to ensure it's in the desired state. 

786 ↛ 789line 786 didn't jump to line 789, because the condition on line 786 was never false if commands2: 

load_config(module, commands2) 

commands.extend(commands2) 

commands = [cmd for cmd in commands if cmd != 'configure'] 

result['commands'] = commands 

 

if result['changed']: 

failed_conditions = check_declarative_intent_params(module, want) 

 

if failed_conditions: 

msg = 'One or more conditional statements have not been satisfied' 

module.fail_json(msg=msg, failed_conditions=failed_conditions) 

 

module.exit_json(**result) 

 

 

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

main()