summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/iosxr/plugins/cliconf/iosxr.py
blob: 6b569ae2a95f5e3d727e9f61cfd605bb00da5b8c (plain)
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
#
# (c) 2017 Red Hat Inc.
#
# 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/>.
#
from __future__ import absolute_import, division, print_function


__metaclass__ = type

DOCUMENTATION = """
author: Ansible Networking Team (@ansible-network)
name: iosxr
short_description: Use iosxr cliconf to run command on Cisco IOS XR platform
description:
- This iosxr plugin provides low level abstraction apis for sending and receiving
  CLI commands from Cisco IOS XR network devices.
version_added: 1.0.0
notes:
- IOSXR commit confirmed command varies with IOSXR version releases,
  commit_comment and commit_label may or may not
  be valid together as per the device version.
options:
  commit_confirmed:
    type: boolean
    default: false
    description:
    - enable or disable commit confirmed mode
    env:
    - name: ANSIBLE_IOSXR_COMMIT_CONFIRMED
    vars:
    - name: ansible_iosxr_commit_confirmed
  commit_confirmed_timeout:
    type: int
    description:
    - Commits the configuration on a trial basis for the time specified in seconds or minutes.
    env:
    - name: ANSIBLE_IOSXR_COMMIT_CONFIRMED_TIMEOUT
    vars:
    - name: ansible_iosxr_commit_confirmed_timeout
  commit_label:
    type: str
    description:
    - Adds label to commit confirmed.
    env:
    - name: ANSIBLE_IOSXR_COMMIT_LABEL
    vars:
    - name: ansible_iosxr_commit_label
  commit_comment:
    type: str
    description:
    - Adds comment to commit confirmed..
    env:
    - name: ANSIBLE_IOSXR_COMMIT_COMMENT
    vars:
    - name: ansible_iosxr_commit_comment
  config_commands:
    description:
    - Specifies a list of commands that can make configuration changes
      to the target device.
    - When `ansible_network_single_user_mode` is enabled, if a command sent
      to the device is present in this list, the existing cache is invalidated.
    version_added: 2.0.0
    type: list
    elements: str
    default: []
    vars:
    - name: ansible_iosxr_config_commands
  config_mode_exclusive:
    type: boolean
    default: false
    description:
    - enable or disable config mode exclusive
    env:
    - name: ANSIBLE_IOSXR_CONFIG_MODE_EXCLUSIVE
    vars:
    - name: ansible_iosxr_config_mode_exclusive
"""

EXAMPLES = """
# Use commit confirmed within a task with timeout, label and comment

- name: Commit confirmed with a task
  vars:
    ansible_iosxr_commit_confirmed: True
    ansible_iosxr_commit_confirmed_timeout: 50
    ansible_iosxr_commit_label: TestLabel
    ansible_iosxr_commit_comment: I am a test comment
  cisco.iosxr.iosxr_logging_global:
    state: merged
    config:
      buffered:
        severity: errors #alerts #informational
      correlator:
        buffer_size: 2024

# Commands (cliconf specific)
# ["commit confirmed 50 label TestLabel comment I am a test comment"]

# Use commit within a task with label

- name: Commit label with a task
  vars:
    ansible_iosxr_commit_label: lblTest
  cisco.iosxr.iosxr_hostname:
    state: merged
    config:
      hostname: R1

# Commands (cliconf specific)
# ["commit label lblt1"]

# Use commit confirm with timeout and confirm the commit

# NOTE - IOSXR waits for a `commit` when the command
# executed is `commit confirmed <timeout>` within the timeout
# period for the config to commit successfully, else a rollback
# happens.

- name: Example commit confirmed
  vars:
    ansible_iosxr_commit_confirmed: True
    ansible_iosxr_commit_confirmed_timeout: 60
  tasks:
    - name: "Commit confirmed with timeout"
      cisco.iosxr.iosxr_hostname:
        state: merged
        config:
          hostname: R1

    - name: "Confirm the Commit"
      cisco.iosxr.iosxr_command:
        commands:
          - commit

# Commands (cliconf specific)
# ["commit confirmed 60"]

# Use exclusive mode with a task

- name: Configure exclusive mode with a task
  vars:
    ansible_iosxr_config_mode_exclusive: True
  cisco.iosxr.iosxr_interfaces:
    state: merged
    config:
      - name: GigabitEthernet0/0/0/2
        description: Configured via Ansible
      - name: GigabitEthernet0/0/0/3
        description: Configured via Ansible

# Commands (cliconf specific)
# ["configure exclusive"]

# Use Replace option with commit confirmed

# NOTE - IOSXR waits for a `commit` when the command
# executed is `commit replace confirmed <timeout>` within the timeout
# period for the config to commit successfully, else a rollback
# happens.
# This option is supported by only iosxr_config module

- name: Example replace config with commit confirmed
  vars:
    ansible_iosxr_commit_confirmed: True
    ansible_iosxr_commit_confirmed_timeout: 60
  tasks:
    - name: "Replace config with Commit confirmed"
      cisco.iosxr.iosxr_config:
        src: 'replace_running_cfg_iosxr.txt'
        replace: config

    - name: "Confirm the Commit"
      cisco.iosxr.iosxr_command:
        commands:
          - commit
"""

import json
import re

from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils._text import to_text
from ansible.module_utils.common._collections_compat import Mapping
from ansible.module_utils.connection import ConnectionError
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import (
    NetworkConfig,
    dumps,
)
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
from ansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_base import CliconfBase

from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxr import (
    mask_config_blocks_from_diff,
    sanitize_config,
)


class Cliconf(CliconfBase):
    def __init__(self, *args, **kwargs):
        self._device_info = {}
        super(Cliconf, self).__init__(*args, **kwargs)

    def get_command_output(self, command):
        reply = self.get(command)
        data = to_text(reply, errors="surrogate_or_strict").strip()
        return data

    def get_device_info(self):
        if not self._device_info:
            device_info = dict()
            device_info["network_os"] = "iosxr"
            data = self.get_command_output("show version | utility head -n 20")
            match = re.search(r"Version (\S+)$", data, re.M)
            if match:
                device_info["network_os_version"] = match.group(1)
            else:
                match = re.search(r"Version (\S+ \S+)$", data, re.M)
                if match:
                    device_info["network_os_version"] = match.group(1)

            match = re.search(r'image file is "(.+)"', data)
            if match:
                device_info["network_os_image"] = match.group(1)

            model_search_strs = [
                r"^[Cc]isco (.+) \(\) processor",
                r"^[Cc]isco ([A-Z0-9\-]+) processor",
                r"^[Cc]isco (.+) \(revision",
                r"^[Cc]isco (\S+ \S+).+bytes of .*memory",
            ]
            for item in model_search_strs:
                match = re.search(item, data, re.M)
                if match:
                    device_info["network_os_model"] = match.group(1)
                    break

            if "network_os_model" not in device_info:
                data = self.get_command_output("show inventory")
                match = re.search(r"DESCR: \"[Cc]isco (\S+ \S+)", data, re.M)
                if match:
                    device_info["network_os_model"] = match.group(1)

            data = self.get_command_output("show inventory")
            match = re.search(r"SN: (\S+)\n\nNAME:", data, re.M)
            if match:
                device_info["network_os_serialnum"] = match.group(1)

            hostname = self.get_command_output("show running-config hostname")
            match = re.search(r"hostname\s(\S+)$", hostname, re.M)
            if match:
                device_info["network_os_hostname"] = match.group(1)

            self._device_info = device_info

        return self._device_info

    def configure(self, admin=False, exclusive=False):
        prompt = to_text(self._connection.get_prompt(), errors="surrogate_or_strict").strip()
        if not prompt.endswith(")#"):
            if admin and "admin-" not in prompt:
                self.send_command("admin")
            if exclusive or self.get_option("config_mode_exclusive"):
                self.send_command("configure exclusive")
                return
            self.send_command("configure terminal")

    def abort(self, admin=False):
        prompt = to_text(self._connection.get_prompt(), errors="surrogate_or_strict").strip()
        if prompt.endswith(")#"):
            self.send_command("abort")
            if admin and "admin-" in prompt:
                self.send_command("exit")

    def get_config(self, source="running", flags=None, format="text"):
        if source not in ["running"]:
            raise ValueError("fetching configuration from %s is not supported" % source)

        lookup = {"running": "running-config"}

        cmd = "show {0} ".format(lookup[source])
        cmd += " ".join(to_list(flags))
        cmd = cmd.strip()

        return self.send_command(cmd)

    def edit_config(
        self,
        candidate=None,
        commit=True,
        replace=None,
        diff=False,
        comment=None,
        admin=False,
        exclusive=False,
        label=None,
    ):
        operations = self.get_device_operations()
        self.check_edit_config_capability(operations, candidate, commit, replace, comment)

        resp = {}
        results = []
        requests = []

        self.configure(admin=admin, exclusive=exclusive)

        if replace:
            candidate = "load {0}".format(replace)

        for line in to_list(candidate):
            if not isinstance(line, Mapping):
                line = {"command": line}
            cmd = line["command"]
            results.append(self.send_command(**line))
            requests.append(cmd)

        # Before any commit happened, we can get a real configuration
        # diff from the device and make it available by the iosxr_config module.
        # This information can be useful either in check mode or normal mode.
        resp["show_commit_config_diff"] = self.get("show commit changes diff")

        if commit:
            try:
                self.commit(comment=comment, label=label, replace=replace)
            except AnsibleConnectionFailure as exc:
                error_msg = to_text(exc, errors="surrogate_or_strict").strip()
                if "Invalid input detected" in error_msg and "comment" in error_msg:
                    msg = (
                        "value of comment option '%s' is ignored as it in not supported by IOSXR"
                        % comment
                    )
                    self._connection.queue_message("warning", msg)
                    comment = None
                    self.commit(comment=comment, label=label, replace=replace)
                else:
                    raise ConnectionError(error_msg)

        else:
            self.discard_changes()

        if not self.get_option("commit_confirmed"):
            self.abort(admin=admin)

        resp["request"] = requests
        resp["response"] = results
        return resp

    def get_diff(
        self,
        candidate=None,
        running=None,
        diff_match="line",
        diff_ignore_lines=None,
        path=None,
        diff_replace="line",
    ):
        diff = {}
        device_operations = self.get_device_operations()
        option_values = self.get_option_values()

        if candidate is None and device_operations["supports_generate_diff"]:
            raise ValueError("candidate configuration is required to generate diff")

        if diff_match not in option_values["diff_match"]:
            raise ValueError(
                "'match' value %s in invalid, valid values are %s"
                % (diff_match, ", ".join(option_values["diff_match"])),
            )

        if diff_replace not in option_values["diff_replace"]:
            raise ValueError(
                "'replace' value %s in invalid, valid values are %s"
                % (diff_replace, ", ".join(option_values["diff_replace"])),
            )

        # prepare candidate configuration
        sanitized_candidate = sanitize_config(candidate)
        candidate_obj = NetworkConfig(indent=1, comment_tokens=["!"])
        candidate_obj.load(sanitized_candidate)

        if running and diff_match != "none":
            # running configuration
            running = mask_config_blocks_from_diff(running, candidate, "ansible")
            running = sanitize_config(running)

            running_obj = NetworkConfig(
                indent=1,
                contents=running,
                ignore_lines=diff_ignore_lines,
                comment_tokens=["!"],
            )
            configdiffobjs = candidate_obj.difference(
                running_obj,
                path=path,
                match=diff_match,
                replace=diff_replace,
            )

        else:
            configdiffobjs = candidate_obj.items

        diff["config_diff"] = dumps(configdiffobjs, "commands") if configdiffobjs else ""
        return diff

    def get(
        self,
        command=None,
        prompt=None,
        answer=None,
        sendonly=False,
        newline=True,
        output=None,
        check_all=False,
    ):
        if output:
            raise ValueError("'output' value %s is not supported for get" % output)
        return self.send_command(
            command=command,
            prompt=prompt,
            answer=answer,
            sendonly=sendonly,
            newline=newline,
            check_all=check_all,
        )

    def commit(self, comment=None, label=None, replace=None):
        """Implements commit functionality of config module
        and commit confirmed functionality of cliconf module

        Args:
            comment (str, optional): commit comment. Defaults to None.
            label (str, optional): commit label. Defaults to None.
            replace (bool, optional): Flag to replace commit. Defaults to None.
        """

        cmd_obj = {}
        if replace:
            cmd_obj["command"] = "commit replace"
            if self.get_option("commit_confirmed"):
                cmd_obj["command"] = "commit replace confirmed"
                if self.get_option("commit_confirmed_timeout"):
                    cmd_obj["command"] += " {0}".format(self.get_option("commit_confirmed_timeout"))

            cmd_obj[
                "prompt"
            ] = "This commit will replace or remove the entire running configuration"
            cmd_obj["answer"] = "yes"

        elif self.get_option("commit_confirmed"):
            cmd_obj["command"] = "commit confirmed"
            if self.get_option("commit_confirmed_timeout"):
                cmd_obj["command"] += " {0}".format(self.get_option("commit_confirmed_timeout"))
            if self.get_option("commit_label"):
                cmd_obj["command"] += " label {0}".format(self.get_option("commit_label"))
            if self.get_option("commit_comment"):
                cmd_obj["command"] += " comment {0}".format(self.get_option("commit_comment"))

        else:
            label = label or self.get_option("commit_label")
            comment = comment or self.get_option("commit_comment")

            if comment or label:
                cmd_obj["command"] = "commit"
                if label:
                    cmd_obj["command"] += " label {0}".format(label)
                if comment:
                    cmd_obj["command"] += " comment {0}".format(comment)

            else:
                cmd_obj["command"] = "commit show-error"
            # In some cases even a normal commit, i.e., !replace,
            # throws a prompt and we need to handle it before
            # proceeding further
            cmd_obj["prompt"] = "(C|c)onfirm"
            cmd_obj["answer"] = "y"
        self.send_command(**cmd_obj)

    def run_commands(self, commands=None, check_rc=True):
        if commands is None:
            raise ValueError("'commands' value is required")
        responses = list()
        for cmd in to_list(commands):
            if not isinstance(cmd, Mapping):
                cmd = {"command": cmd}

            output = cmd.pop("output", None)
            if output:
                raise ValueError("'output' value %s is not supported for run_commands" % output)

            try:
                out = self.send_command(**cmd)
            except AnsibleConnectionFailure as e:
                if check_rc:
                    raise
                out = getattr(e, "err", e)

            if out is not None:
                try:
                    out = to_text(out, errors="surrogate_or_strict").strip()
                except UnicodeError:
                    raise ConnectionError(
                        message="Failed to decode output from %s: %s" % (cmd, to_text(out)),
                    )

                try:
                    out = json.loads(out)
                except ValueError:
                    pass

                responses.append(out)
        return responses

    def discard_changes(self):
        self.send_command("abort")

    def get_device_operations(self):
        return {
            "supports_diff_replace": True,
            "supports_commit": True,
            "supports_rollback": False,
            "supports_defaults": False,
            "supports_onbox_diff": False,
            "supports_commit_comment": True,
            "supports_multiline_delimiter": False,
            "supports_diff_match": True,
            "supports_diff_ignore_lines": True,
            "supports_generate_diff": True,
            "supports_replace": True,
            "supports_admin": True,
            "supports_commit_label": True,
        }

    def get_option_values(self):
        return {
            "format": ["text"],
            "diff_match": ["line", "strict", "exact", "none"],
            "diff_replace": ["line", "block", "config"],
            "output": [],
        }

    def get_capabilities(self):
        result = super(Cliconf, self).get_capabilities()
        result["rpc"] += ["commit", "discard_changes", "get_diff", "configure", "exit"]
        result["device_operations"] = self.get_device_operations()
        result.update(self.get_option_values())
        return json.dumps(result)

    def set_cli_prompt_context(self):
        """
        Make sure we are in the operational cli mode
        :return: None
        """
        if self._connection.connected and not self.get_option("commit_confirmed"):
            self._update_cli_prompt_context(config_context=")#", exit_command="abort")