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

# (c) 2016 Red Hat Inc. 

# (c) 2017 Ansible Project 

# 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 

 

DOCUMENTATION = """ 

--- 

author: Ansible Networking Team 

connection: network_cli 

short_description: Use network_cli to run command on network appliances 

description: 

- This connection plugin provides a connection to remote devices over the 

SSH and implements a CLI shell. This connection plugin is typically used by 

network devices for sending and receiving CLi commands to network devices. 

version_added: "2.3" 

options: 

host: 

description: 

- Specifies the remote device FQDN or IP address to establish the SSH 

connection to. 

default: inventory_hostname 

vars: 

- name: ansible_host 

port: 

type: int 

description: 

- Specifies the port on the remote device to listening for connections 

when establishing the SSH connection. 

default: 22 

ini: 

- section: defaults 

key: remote_port 

env: 

- name: ANSIBLE_REMOTE_PORT 

vars: 

- name: ansible_port 

network_os: 

description: 

- Configures the device platform network operating system. This value is 

used to load the correct terminal and cliconf plugins to communicate 

with the remote device 

default: null 

vars: 

- name: ansible_network_os 

remote_user: 

description: 

- The username used to authenticate to the remote device when the SSH 

connection is first established. If the remote_user is not specified, 

the connection will use the username of the logged in user. 

- Can be configured form the CLI via the C(--user) or C(-u) options 

ini: 

- section: defaults 

key: remote_user 

env: 

- name: ANSIBLE_REMOTE_USER 

vars: 

- name: ansible_user 

password: 

description: 

- Configures the user password used to authenticate to the remote device 

when first establishing the SSH connection. 

vars: 

- name: ansible_pass 

private_key_file: 

description: 

- The private SSH key or certificate file used to to authenticate to the 

remote device when first establishing the SSH connection. 

ini: 

section: defaults 

key: private_key_file 

env: 

- name: ANSIBLE_PRIVATE_KEY_FILE 

vars: 

- name: ansible_private_key_file 

timeout: 

type: int 

description: 

- Sets the connection time, in seconds, for the communicating with the 

remote device. This timeout is used as the default timeout value for 

commands when issuing a command to the network CLI. If the command 

does not return in timeout seconds, the an error is generated. 

default: 120 

become: 

type: boolean 

description: 

- The become option will instruct the CLI session to attempt privilege 

escalation on platforms that support it. Normally this means 

transitioning from user mode to C(enable) mode in the CLI session. 

If become is set to True and the remote device does not support 

privilege escalation or the privilege has already been elevated, then 

this option is silently ignored 

- Can be configured form the CLI via the C(--become) or C(-b) options 

default: False 

ini: 

section: privilege_escalation 

key: become 

env: 

- name: ANSIBLE_BECOME 

vars: 

- name: ansible_become 

become_method: 

description: 

- This option allows the become method to be specified in for handling 

privilege escalation. Typically the become_method value is set to 

C(enable) but could be defined as other values. 

default: sudo 

ini: 

section: privilege_escalation 

key: become_method 

env: 

- name: ANSIBLE_BECOME_METHOD 

vars: 

- name: ansible_become_method 

host_key_auto_add: 

type: boolean 

description: 

- By default, Ansible will prompt the user before adding SSH keys to the 

known hosts file. Since persistent connections such as network_cli run 

in background processes, the user will never be prompted. By enabling 

this option, unknown host keys will automatically be added to the 

known hosts file. 

- Be sure to fully understand the security implications of enabling this 

option on production systems as it could create a security vulnerability. 

default: False 

ini: 

section: paramiko_connection 

key: host_key_auto_add 

env: 

- name: ANSIBLE_HOST_KEY_AUTO_ADD 

persistent_connect_timeout: 

type: int 

description: 

- Configures, in seconds, the amount of time to wait when trying to 

initially establish a persistent connection. If this value expires 

before the connection to the remote device is completed, the connection 

will fail 

default: 30 

ini: 

section: persistent_connection 

key: persistent_connect_timeout 

env: 

- name: ANSIBLE_PERSISTENT_CONNECT_TIMEOUT 

persistent_command_timeout: 

type: int 

description: 

- Configures, in seconds, the amount of time to wait for a command to 

return from the remote device. If this timer is exceeded before the 

command returns, the connection plugin will raise an exception and 

close 

default: 10 

ini: 

section: persistent_connection 

key: persistent_command_timeout 

env: 

- name: ANSIBLE_PERSISTENT_COMMAND_TIMEOUT 

""" 

 

import json 

import logging 

import re 

import os 

import socket 

import traceback 

 

from ansible import constants as C 

from ansible.errors import AnsibleConnectionFailure 

from ansible.module_utils.six import BytesIO, PY3 

from ansible.module_utils.six.moves import cPickle 

from ansible.module_utils._text import to_bytes, to_text 

from ansible.playbook.play_context import PlayContext 

from ansible.plugins.loader import cliconf_loader, terminal_loader, connection_loader 

from ansible.plugins.connection import ConnectionBase 

from ansible.utils.path import unfrackpath 

 

try: 

from __main__ import display 

except ImportError: 

from ansible.utils.display import Display 

display = Display() 

 

 

class Connection(ConnectionBase): 

''' CLI (shell) SSH connections on Paramiko ''' 

 

transport = 'network_cli' 

has_pipelining = True 

force_persistence = True 

 

def __init__(self, play_context, new_stdin, *args, **kwargs): 

super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) 

 

self._ssh_shell = None 

 

self._matched_prompt = None 

self._matched_pattern = None 

self._last_response = None 

self._history = list() 

self._play_context = play_context 

 

self._local = connection_loader.get('local', play_context, '/dev/null') 

self._local.set_options() 

 

self._terminal = None 

self._cliconf = None 

 

self._ansible_playbook_pid = kwargs.get('ansible_playbook_pid') 

 

210 ↛ 214line 210 didn't jump to line 214, because the condition on line 210 was never false if self._play_context.verbosity > 3: 

logging.getLogger('paramiko').setLevel(logging.DEBUG) 

 

# reconstruct the socket_path and set instance values accordingly 

self._update_connection_state() 

 

def __getattr__(self, name): 

try: 

return self.__dict__[name] 

except KeyError: 

if name.startswith('_'): 

raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name)) 

return getattr(self._cliconf, name) 

 

def get_prompt(self): 

"""Returns the current prompt from the device""" 

return self._matched_prompt 

 

def exec_command(self, cmd, in_data=None, sudoable=True): 

# this try..except block is just to handle the transition to supporting 

# network_cli as a toplevel connection. Once connection=local is gone, 

# this block can be removed as well and all calls passed directly to 

# the local connection 

if self._ssh_shell: 

try: 

cmd = json.loads(to_text(cmd, errors='surrogate_or_strict')) 

kwargs = {'command': to_bytes(cmd['command'], errors='surrogate_or_strict')} 

for key in ('prompt', 'answer', 'sendonly', 'newline', 'prompt_retry_check'): 

if cmd.get(key) is True or cmd.get(key) is False: 

kwargs[key] = cmd[key] 

elif cmd.get(key) is not None: 

kwargs[key] = to_bytes(cmd[key], errors='surrogate_or_strict') 

return self.send(**kwargs) 

except ValueError: 

cmd = to_bytes(cmd, errors='surrogate_or_strict') 

return self.send(command=cmd) 

 

else: 

return self._local.exec_command(cmd, in_data, sudoable) 

 

def put_file(self, in_path, out_path): 

return self._local.put_file(in_path, out_path) 

 

def fetch_file(self, in_path, out_path): 

return self._local.fetch_file(in_path, out_path) 

 

def update_play_context(self, pc_data): 

"""Updates the play context information for the connection""" 

pc_data = to_bytes(pc_data) 

259 ↛ 260line 259 didn't jump to line 260, because the condition on line 259 was never true if PY3: 

pc_data = cPickle.loads(pc_data, encoding='bytes') 

else: 

pc_data = cPickle.loads(pc_data) 

play_context = PlayContext() 

play_context.deserialize(pc_data) 

 

messages = ['updating play_context for connection'] 

267 ↛ 268line 267 didn't jump to line 268, because the condition on line 267 was never true if self._play_context.become is False and play_context.become is True: 

auth_pass = play_context.become_pass 

self._terminal.on_become(passwd=auth_pass) 

messages.append('authorizing connection') 

 

272 ↛ 273line 272 didn't jump to line 273, because the condition on line 272 was never true elif self._play_context.become is True and not play_context.become: 

self._terminal.on_unbecome() 

messages.append('deauthorizing connection') 

 

self._play_context = play_context 

return messages 

 

def _connect(self): 

''' 

Connects to the remote device and starts the terminal 

''' 

283 ↛ 284line 283 didn't jump to line 284, because the condition on line 283 was never true if self.connected: 

return 

 

self.paramiko_conn = connection_loader.get('paramiko', self._play_context, '/dev/null') 

self.paramiko_conn.set_options(direct={'look_for_keys': not bool(self._play_context.password and not self._play_context.private_key_file)}) 

self.paramiko_conn.force_persistence = self.force_persistence 

ssh = self.paramiko_conn._connect() 

 

display.vvvv('ssh connection done, setting terminal', host=self._play_context.remote_addr) 

 

self._ssh_shell = ssh.ssh.invoke_shell() 

self._ssh_shell.settimeout(self._play_context.timeout) 

 

network_os = self._play_context.network_os 

297 ↛ 298line 297 didn't jump to line 298, because the condition on line 297 was never true if not network_os: 

raise AnsibleConnectionFailure( 

'Unable to automatically determine host network os. Please ' 

'manually configure ansible_network_os value for this host' 

) 

 

self._terminal = terminal_loader.get(network_os, self) 

304 ↛ 305line 304 didn't jump to line 305, because the condition on line 304 was never true if not self._terminal: 

raise AnsibleConnectionFailure('network os %s is not supported' % network_os) 

 

display.vvvv('loaded terminal plugin for network_os %s' % network_os, host=self._play_context.remote_addr) 

 

self._cliconf = cliconf_loader.get(network_os, self) 

310 ↛ 313line 310 didn't jump to line 313, because the condition on line 310 was never false if self._cliconf: 

display.vvvv('loaded cliconf plugin for network_os %s' % network_os, host=self._play_context.remote_addr) 

else: 

display.vvvv('unable to load cliconf for network_os %s' % network_os) 

 

self.receive() 

 

display.vvvv('firing event: on_open_shell()', host=self._play_context.remote_addr) 

self._terminal.on_open_shell() 

 

320 ↛ 321line 320 didn't jump to line 321, because the condition on line 320 was never true if self._play_context.become and self._play_context.become_method == 'enable': 

display.vvvv('firing event: on_become', host=self._play_context.remote_addr) 

auth_pass = self._play_context.become_pass 

self._terminal.on_become(passwd=auth_pass) 

 

display.vvvv('ssh connection has completed successfully', host=self._play_context.remote_addr) 

self._connected = True 

 

return self 

 

def _update_connection_state(self): 

''' 

Reconstruct the connection socket_path and check if it exists 

 

If the socket path exists then the connection is active and set 

both the _socket_path value to the path and the _connected value 

to True. If the socket path doesn't exist, leave the socket path 

value to None and the _connected value to False 

''' 

ssh = connection_loader.get('ssh', class_only=True) 

cp = ssh._create_control_path(self._play_context.remote_addr, self._play_context.port, self._play_context.remote_user, self._play_context.connection, 

self._ansible_playbook_pid) 

 

tmp_path = unfrackpath(C.PERSISTENT_CONTROL_PATH_DIR) 

socket_path = unfrackpath(cp % dict(directory=tmp_path)) 

 

if os.path.exists(socket_path): 

self._connected = True 

self._socket_path = socket_path 

 

def reset(self): 

''' 

Reset the connection 

''' 

if self._socket_path: 

display.vvvv('resetting persistent connection for socket_path %s' % self._socket_path, host=self._play_context.remote_addr) 

self.close() 

display.vvvv('reset call on connection instance', host=self._play_context.remote_addr) 

 

def close(self): 

''' 

Close the active connection to the device 

''' 

# only close the connection if its connected. 

if self._connected: 

display.debug("closing ssh connection to device") 

if self._ssh_shell: 

display.debug("firing event: on_close_shell()") 

self._terminal.on_close_shell() 

self._ssh_shell.close() 

self._ssh_shell = None 

display.debug("cli session is now closed") 

self._connected = False 

display.debug("ssh connection has been closed successfully") 

 

def receive(self, command=None, prompts=None, answer=None, newline=True, prompt_retry_check=False): 

''' 

Handles receiving of output from command 

''' 

recv = BytesIO() 

handled = False 

 

self._matched_prompt = None 

self._matched_cmd_prompt = None 

matched_prompt_window = window_count = 0 

 

while True: 

data = self._ssh_shell.recv(256) 

 

# when a channel stream is closed, received data will be empty 

390 ↛ 391line 390 didn't jump to line 391, because the condition on line 390 was never true if not data: 

break 

 

recv.write(data) 

offset = recv.tell() - 256 if recv.tell() > 256 else 0 

recv.seek(offset) 

 

window = self._strip(recv.read()) 

window_count += 1 

 

400 ↛ 401line 400 didn't jump to line 401, because the condition on line 400 was never true if prompts and not handled: 

handled = self._handle_prompt(window, prompts, answer, newline) 

matched_prompt_window = window_count 

403 ↛ 407line 403 didn't jump to line 407, because the condition on line 403 was never true elif prompts and handled and prompt_retry_check and matched_prompt_window + 1 == window_count: 

# check again even when handled, if same prompt repeats in next window 

# (like in the case of a wrong enable password, etc) indicates 

# value of answer is wrong, report this as error. 

if self._handle_prompt(window, prompts, answer, newline, prompt_retry_check): 

raise AnsibleConnectionFailure("For matched prompt '%s', answer is not valid" % self._matched_cmd_prompt) 

 

if self._find_prompt(window): 

self._last_response = recv.getvalue() 

resp = self._strip(self._last_response) 

return self._sanitize(resp, command) 

 

def send(self, command, prompt=None, answer=None, newline=True, sendonly=False, prompt_retry_check=False): 

''' 

Sends the command to the device in the opened shell 

''' 

try: 

self._history.append(command) 

self._ssh_shell.sendall(b'%s\r' % command) 

422 ↛ 423line 422 didn't jump to line 423, because the condition on line 422 was never true if sendonly: 

return 

response = self.receive(command, prompt, answer, newline, prompt_retry_check) 

return to_text(response, errors='surrogate_or_strict') 

except (socket.timeout, AttributeError): 

display.vvvv(traceback.format_exc(), host=self._play_context.remote_addr) 

raise AnsibleConnectionFailure("timeout trying to send command: %s" % command.strip()) 

 

def _strip(self, data): 

''' 

Removes ANSI codes from device response 

''' 

for regex in self._terminal.ansi_re: 

data = regex.sub(b'', data) 

return data 

 

def _handle_prompt(self, resp, prompts, answer, newline, prompt_retry_check=False): 

''' 

Matches the command prompt and responds 

 

:arg resp: Byte string containing the raw response from the remote 

:arg prompts: Sequence of byte strings that we consider prompts for input 

:arg answer: Byte string to send back to the remote if we find a prompt. 

A carriage return is automatically appended to this string. 

:returns: True if a prompt was found in ``resp``. False otherwise 

''' 

if not isinstance(prompts, list): 

prompts = [prompts] 

prompts = [re.compile(r, re.I) for r in prompts] 

for regex in prompts: 

match = regex.search(resp) 

if match: 

# if prompt_retry_check is enabled to check if same prompt is 

# repeated don't send answer again. 

if not prompt_retry_check: 

self._ssh_shell.sendall(b'%s' % answer) 

if newline: 

self._ssh_shell.sendall(b'\r') 

self._matched_cmd_prompt = match.group() 

return True 

return False 

 

def _sanitize(self, resp, command=None): 

''' 

Removes elements from the response before returning to the caller 

''' 

cleaned = [] 

for line in resp.splitlines(): 

if (command and line.strip() == command.strip()) or self._matched_prompt.strip() in line: 

continue 

cleaned.append(line) 

return b'\n'.join(cleaned).strip() 

 

def _find_prompt(self, response): 

'''Searches the buffered response for a matching command prompt 

''' 

errored_response = None 

is_error_message = False 

for regex in self._terminal.terminal_stderr_re: 

481 ↛ 482line 481 didn't jump to line 482, because the condition on line 481 was never true if regex.search(response): 

is_error_message = True 

 

# Check if error response ends with command prompt if not 

# receive it buffered prompt 

for regex in self._terminal.terminal_stdout_re: 

match = regex.search(response) 

if match: 

errored_response = response 

self._matched_prompt = match.group() 

break 

 

493 ↛ 502line 493 didn't jump to line 502, because the condition on line 493 was never false if not is_error_message: 

for regex in self._terminal.terminal_stdout_re: 

match = regex.search(response) 

if match: 

self._matched_pattern = regex.pattern 

self._matched_prompt = match.group() 

499 ↛ 494line 499 didn't jump to line 494, because the condition on line 499 was never false if not errored_response: 

return True 

 

502 ↛ 503line 502 didn't jump to line 503, because the condition on line 502 was never true if errored_response: 

raise AnsibleConnectionFailure(errored_response) 

 

return False