Exemplo n.º 1
0
def main():
    argument_spec = dict(
        commands=dict(type="list", required=True, elements="raw"),
        wait_for=dict(type="list", aliases=["waitfor"], elements="str"),
        match=dict(default="all", choices=["all", "any"]),
        retries=dict(default=10, type="int"),
        interval=dict(default=1, type="int"),
    )

    argument_spec.update(iosxr_argument_spec)

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    warnings = list()
    result = {"changed": False, "warnings": warnings}
    commands = parse_commands(module, warnings)
    wait_for = module.params["wait_for"] or list()

    try:
        conditionals = [Conditional(c) for c in wait_for]
    except AttributeError as exc:
        module.fail_json(msg=to_text(exc))

    retries = module.params["retries"]
    interval = module.params["interval"]
    match = module.params["match"]

    while retries > 0:
        responses = run_commands(module, commands)

        for item in list(conditionals):
            if item(responses):
                if match == "any":
                    conditionals = list()
                    break
                conditionals.remove(item)

        if not conditionals:
            break

        time.sleep(interval)
        retries -= 1

    if conditionals:
        failed_conditions = [item.raw for item in conditionals]
        msg = "One or more conditional statements have not been satisfied"
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result.update(
        {
            "stdout": responses,
            "stdout_lines": list(to_lines(responses))
        }, )

    module.exit_json(**result)
Exemplo n.º 2
0
    def check_declarative_intent_params(self):
        failed_conditions = []
        for want_item in self._want:
            want_state = want_item.get('state')
            want_tx_rate = want_item.get('tx_rate')
            want_rx_rate = want_item.get('rx_rate')
            if want_state not in (
                    'up', 'down') and not want_tx_rate and not want_rx_rate:
                continue

            if self._result['changed']:
                sleep(want_item['delay'])

            command = 'show interfaces {0!s}'.format(want_item['name'])
            out = run_commands(self._module, command)[0]

            if want_state in ('up', 'down'):
                match = re.search(r'%s (\w+)' % 'line protocol is', out, re.M)
                have_state = None
                if match:
                    have_state = match.group(1)
                    if have_state.strip() == 'administratively':
                        match = re.search(r'%s (\w+)' % 'administratively',
                                          out, re.M)
                        if match:
                            have_state = match.group(1)

                if have_state is None or not conditional(
                        want_state, have_state.strip()):
                    failed_conditions.append('state ' +
                                             'eq({0!s})'.format(want_state))

            if want_tx_rate:
                match = re.search(r'%s (\d+)' % 'output rate', out, re.M)
                have_tx_rate = None
                if match:
                    have_tx_rate = match.group(1)

                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'%s (\d+)' % 'input rate', out, re.M)
                have_rx_rate = None
                if match:
                    have_rx_rate = match.group(1)

                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)

        if failed_conditions:
            msg = 'One or more conditional statements have not been satisfied'
            self._module.fail_json(msg=msg,
                                   failed_conditions=failed_conditions)
    def check_declarative_intent_params(self):
        failed_conditions = []
        for want_item in self._want:
            want_state = want_item.get("state")
            want_tx_rate = want_item.get("tx_rate")
            want_rx_rate = want_item.get("rx_rate")
            if (want_state not in ("up", "down") and not want_tx_rate
                    and not want_rx_rate):
                continue

            if self._result["changed"]:
                sleep(want_item["delay"])

            command = "show interfaces {0!s}".format(want_item["name"])
            out = run_commands(self._module, command)[0]

            if want_state in ("up", "down"):
                match = re.search(r"%s (\w+)" % "line protocol is", out, re.M)
                have_state = None
                if match:
                    have_state = match.group(1)
                    if have_state.strip() == "administratively":
                        match = re.search(r"%s (\w+)" % "administratively",
                                          out, re.M)
                        if match:
                            have_state = match.group(1)

                if have_state is None or not conditional(
                        want_state, have_state.strip()):
                    failed_conditions.append("state " +
                                             "eq({0!s})".format(want_state))

            if want_tx_rate:
                match = re.search(r"%s (\d+)" % "output rate", out, re.M)
                have_tx_rate = None
                if match:
                    have_tx_rate = match.group(1)

                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"%s (\d+)" % "input rate", out, re.M)
                have_rx_rate = None
                if match:
                    have_rx_rate = match.group(1)

                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)

        if failed_conditions:
            msg = "One or more conditional statements have not been satisfied"
            self._module.fail_json(msg=msg,
                                   failed_conditions=failed_conditions)
Exemplo n.º 4
0
def main():
    argument_spec = dict(commands=dict(type='list', required=True),
                         wait_for=dict(type='list', aliases=['waitfor']),
                         match=dict(default='all', choices=['all', 'any']),
                         retries=dict(default=10, type='int'),
                         interval=dict(default=1, type='int'))

    argument_spec.update(iosxr_argument_spec)
    argument_spec.update(command_spec)

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    warnings = list()
    result = {'changed': False, 'warnings': warnings}
    commands = parse_commands(module, warnings)
    wait_for = module.params['wait_for'] or list()

    try:
        conditionals = [Conditional(c) for c in wait_for]
    except AttributeError as exc:
        module.fail_json(msg=to_text(exc))

    retries = module.params['retries']
    interval = module.params['interval']
    match = module.params['match']

    while retries > 0:
        responses = run_commands(module, commands)

        for item in list(conditionals):
            if item(responses):
                if match == 'any':
                    conditionals = list()
                    break
                conditionals.remove(item)

        if not conditionals:
            break

        time.sleep(interval)
        retries -= 1

    if conditionals:
        failed_conditions = [item.raw for item in conditionals]
        msg = 'One or more conditional statements have not been satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result.update({
        'stdout': responses,
        'stdout_lines': list(to_lines(responses)),
    })

    module.exit_json(**result)
Exemplo n.º 5
0
def run_module():
    module_args = dict(
        keys=dict(type='dict', required=True),  # user -> SSH key
    )

    result = dict(changed=False)

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    # Get existing keys
    command = "show crypto key authentication rsa all"
    out = run_commands(module, command)

    # Parse keys

    # Key label: cedric
    # Type     : RSA public key authentication
    # Size     : 2048
    # Imported : 16:17:08 UTC Tue Aug 11 2020
    # Data     :
    #  30820122 300D0609 2A864886 F70D0101 01050003 82010F00 3082010A 02820101
    #  00EBCBD5 3B9B0B7E E495B8A6 D297C983 20049AD8 7F4BE9BA 1BD17278 45E40DD6
    #  5D98BBD7 BB2B5B80 0DBD512B 3B76114E 079BE459 0CD1DF82 78623AC1 206EAAAB
    #  1E72F7D3 B45EA954 506BA7A8 1E6020F3 73D3F09C 875273C3 A718EA5D 104DA3C5
    #  9BAB5907 06F61C38 A98EBB04 FC79A96F B3165B54 AC4F1E0E FDD404D2 59D28314
    #  38510F34 5FDDC5FB A2754050 0672685F FC971839 3344B352 E9A1B1E6 A709BD7A
    #  ADBC90A1 93268B1B C9193846 86ACD095 FF51BF7D F7856A56 7BE6FBE9 1AB7B5DA
    #  BE735C66 6332E7BD E680B45B 570F9F5D 29424A8B FBF33B6C 14B398F1 994CD35D
    #  6186467D 87283F9C 575AB642 E3A743DE 3683D308 73304450 0B1CA3E9 11CC116B
    #  35020301 0001

    out = out[0].replace(' \n', '\n')
    template = r"""
Value Required Label (\w+)
Value Required,List Data ([A-F0-9 ]+)

Start
 ^Key label: ${Label}
 ^Data\s+: -> GetData

GetData
 ^ ${Data}
 ^$$ -> Record Start
""".lstrip()
    re_table = textfsm.TextFSM(io.StringIO(template))
    got = {
        data[0]: "".join(data[1]).replace(' ', '')
        for data in re_table.ParseText(out)
    }

    # Check what we want
    wanted = {k: ssh2cisco(v) for k, v in module.params['keys'].items()}

    if got != wanted:
        result['changed'] = True
        result['diff'] = dict(before=yaml.dump(got), after=yaml.dump(wanted))

    if module.check_mode or not result['changed']:
        module.exit_json(**result)

    # Copy changed or missing SSH keys
    conn = get_connection(module)
    for user in wanted:
        if user not in got or wanted[user] != got[user]:
            dst = f"/harddisk:/publickey_{user}.b64"
            with tempfile.NamedTemporaryFile() as src:
                decoded = base64.b64decode(
                    module.params['keys'][user].split()[1])
                src.write(decoded)
                src.flush()
                copy_file(module, src.name, dst)
        command = ("admin crypto key import authentication rsa "
                   f"username {user} harddisk:/publickey_{user}.b64")
        conn.send_command(command, prompt="yes/no", answer="yes")

    # Remove unwanted users
    for user in got:
        if user not in wanted:
            command = ("admin crypto key zeroize authentication rsa "
                       f"username {user}")
            conn.send_command(command, prompt="yes/no", answer="yes")

    module.exit_json(**result)
Exemplo n.º 6
0
 def populate(self):
     self.responses = run_commands(
         self.module,
         list(self.COMMANDS),
         check_rc=False,
     )
Exemplo n.º 7
0
 def run_command(self):
     ping_results = run_commands(
         self.module,
         commands=self.result["commands"],
     )
     return ping_results