ansiblerouter-os

Empty string in Ansible responses


I am developing RouterOS network module for Ansible 2.5.

RouterOS shell can print a few messages that should be detected in on_open_shell() event and either skipped or dismissed automatically. These are Do you want to see the software license? [Y/n]: and a few others, all of which are well documented here in the MikroTik Wiki.

Here is how I'm doing this:

def on_open_shell(self):
    try:
        if not prompt.strip().endswith(b'>'):
            self._exec_cli_command(b' ')
    except AnsibleConnectionFailure:
        raise AnsibleConnectionFailure('unable to bypass license prompt')

It indeed does bypass the license prompt. However it seems that the \n response from the RouterOS device counts as a reply for the actual commands that follow. So, if I have two tasks in my playbook like this:

---
- hosts: routeros
  gather_facts: no
  connection: network_cli
  tasks:
    - routeros_command:
        commands:
          - /system resource print
          - /system routerboard print
      register: result

    - name: Print result
      debug: var=result.stdout_lines

This is the output I get:

ok: [example] => {
    "result.stdout_lines": [
        [
            ""
        ],
        [
            "uptime: 12h33m29s",
            "                  version: 6.42.1 (stable)",
            "               build-time: Apr/23/2018 10:46:55",
            "              free-memory: 231.0MiB",
            "             total-memory: 249.5MiB",
            "                      cpu: Intel(R)",
            "                cpu-count: 1",
            "            cpu-frequency: 2700MHz",
            "                 cpu-load: 2%",
            "           free-hdd-space: 943.8MiB",
            "          total-hdd-space: 984.3MiB",
            "  write-sect-since-reboot: 7048",
            "         write-sect-total: 7048",
            "        architecture-name: x86",
            "               board-name: x86",
            "                 platform: MikroTik"
        ]
    ]
}

As you can see, the output seems to be offset by 1. What should I do to correct this?


Solution

  • It turns out that the problem was in the regular expression that defined the shell prompt. I had it defined like this:

    terminal_stdout_re = [
        re.compile(br"\[\w+\@[\w\-\.]+\] ?>"),
        # other cases
    ]
    

    It did not match the end of prompt which caused Ansible to think that there was a newline before the actual command output. Here is the correct regexp:

    terminal_stdout_re = [
        re.compile(br"\[\w+\@[\w\-\.]+\] ?> ?$"),
        # other cases
    ]