コード例 #1
0
ファイル: ios_command.py プロジェクト: lvrfrc87/cisco.ios
def parse_commands(module, warnings):
    commands = transform_commands(module)
    if module.check_mode:
        for item in list(commands):
            if not item["command"].startswith("show"):
                warnings.append(
                    "Only show commands are supported when using check mode, not executing %s"
                    % item["command"])
                commands.remove(item)
    return commands
コード例 #2
0
ファイル: fos_command.py プロジェクト: hs0210/fujitsu.fos
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')
    )

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

    warnings = list()
    result = {'changed': False, 'warnings': warnings}
    commands = transform_commands(module)
    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)
コード例 #3
0
def parse_commands(module, warnings):
    commands = transform_commands(module)

    for item in list(commands):
        if module.check_mode:
            if item['command'].startswith('conf'):
                warnings.append(
                    'only non-config commands are supported when using check mode, not '
                    'executing %s' % item['command'])
                commands.remove(item)
    return commands
コード例 #4
0
def parse_commands(module, warnings):
    commands = transform_commands(module)

    if module.check_mode:
        for item in list(commands):
            if not item['command'].startswith('show'):
                warnings.append(
                    'Only show commands are supported when using check mode, not '
                    'executing %s' % item['command'])
                commands.remove(item)

    return commands
コード例 #5
0
ファイル: test_utils.py プロジェクト: kokasha/layer2_demo
def test_transform_commands():
    module = MagicMock()

    module.params = {"commands": ["show interfaces"]}
    transformed = utils.transform_commands(module)
    assert transformed == [
        {
            "answer": None,
            "check_all": False,
            "command": "show interfaces",
            "newline": True,
            "output": None,
            "prompt": None,
            "sendonly": False,
        }
    ]

    module.params = {"commands": ["show version", "show memory"]}
    transformed = utils.transform_commands(module)
    assert transformed == [
        {
            "answer": None,
            "check_all": False,
            "command": "show version",
            "newline": True,
            "output": None,
            "prompt": None,
            "sendonly": False,
        },
        {
            "answer": None,
            "check_all": False,
            "command": "show memory",
            "newline": True,
            "output": None,
            "prompt": None,
            "sendonly": False,
        },
    ]

    module.params = {
        "commands": [
            {"command": "show version", "output": "json"},
            "show memory",
        ]
    }
    transformed = utils.transform_commands(module)
    assert transformed == [
        {
            "answer": None,
            "check_all": False,
            "command": "show version",
            "newline": True,
            "output": "json",
            "prompt": None,
            "sendonly": False,
        },
        {
            "answer": None,
            "check_all": False,
            "command": "show memory",
            "newline": True,
            "output": None,
            "prompt": None,
            "sendonly": False,
        },
    ]