community.routeros icon indicating copy to clipboard operation
community.routeros copied to clipboard

Extra lines in stdout (ROS 6.49 to 7.1.5)

Open 1rV1N-git opened this issue 3 years ago • 18 comments

SUMMARY

When I send a command to the router I get a response from the router with extra lines. image

ISSUE TYPE
  • Bug Report
COMPONENT NAME

routeros

ANSIBLE VERSION
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/wsl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
COLLECTION VERSION

CONFIGURATION
[defaults]
stdout_callback = yaml
host_key_checking = False
retry_files_enabled = False
inventory = hosts.yaml
[persistent_connection]
connect_timeout = 30
command_timeout = 30
STEPS TO REPRODUCE
- name: check user is exist
  hosts: mikrotik
  gather_facts: false
  tasks:
    - name: check user is exist
      community.routeros.command:
        commands: 
          - if ([/user find name=admin] !="") do={:put "exist"}
      register: exist

    - debug:
        var: exist
EXPECTED RESULTS

I want to get only output from line 3

ACTUAL RESULTS

I got 3 lines. First-line eq first letter of command, middle-lines = empty line(empty lines can be more than one), last line eq correct output.


1rV1N-git avatar Dec 17 '21 09:12 1rV1N-git

if i send the command with a space at the beginning it is ok

        commands: 
          - ' if ([/user find name=admin] !="") do={:put "exist"}'

1rV1N-git avatar Dec 17 '21 10:12 1rV1N-git

Maybe it's related to https://docs.ansible.com/ansible/devel/collections/community/routeros/docsite/ssh-guide.html#important-notes; have you tried adding something like +cet512w to the RouterOS admin username?

felixfontein avatar Dec 17 '21 11:12 felixfontein

I already tried it Same output

1rV1N-git avatar Dec 17 '21 11:12 1rV1N-git

Maybe it's related to how prompts are detected in the SSH output. If that's the case, this will not be simple to fix.

felixfontein avatar Dec 17 '21 12:12 felixfontein

It is sad. Maybe you will try. Please

1rV1N-git avatar Dec 17 '21 13:12 1rV1N-git

Interestingly, I see similar results when running /system resource print. This seems to be new.

felixfontein avatar Dec 17 '21 16:12 felixfontein

I have been playing around with this a bit. Neither downgrading ansible-core, nor downgrading ansible.netcommon, nor downgrading ansible-pylibssh, nor downgrading this collection did help. (I didn't combine any of these, maybe that would have been needed?) Maybe it's related to the RouterOS version? I have version 6.49.2 (latest release version) installed on my router.

@heuels have you seen anything like this?

felixfontein avatar Dec 17 '21 20:12 felixfontein

I tried RouterOS 7.1 same output. Tomorrow i will try oldest version 6.47 or 6.48

1rV1N-git avatar Dec 17 '21 21:12 1rV1N-git

Long-term 6.48.6 is ok stable 6.49 is bad

In the changelog I see only one line about ssh *) ssh - fixed "undo" functionality;

1rV1N-git avatar Dec 18 '21 14:12 1rV1N-git

https://forum.mikrotik.com/viewtopic.php?t=180391

1rV1N-git avatar Dec 18 '21 16:12 1rV1N-git

Yeah, what's described there is exactly what's causing our problem.

Hmm, changing +cte512w to +ct512w in my config reduces the problem (I get less newlines, but still the initial letter from the command - basically the same that you get).

felixfontein avatar Dec 18 '21 18:12 felixfontein

I have admin+ct1000w1000he and usually, I have only one empty(second) line.

1rV1N-git avatar Dec 18 '21 18:12 1rV1N-git

What I observe in 6.49 but not in 6.48.6 or before, is that stdout contains a slash when it should be empty:

'stdout': ['/'],
'stdout_lines': [['/']]}

This breaks my validation as I have for each command:

 register: result
 failed_when: result.stdout | join("") != ""

I am not sure this is the same issue as described here, but it seems related to the same change in the OS...

sgrimee avatar Feb 01 '22 10:02 sgrimee

@sgrimee prepending your command with a space (I assume right now it starts with /) might help for the moment.

felixfontein avatar Feb 01 '22 12:02 felixfontein

thanks @felixfontein, yes that does fix it, for lines starting not only with / but also :

I have hundreds to change... so instead I patched the plugins/module_utils/routeros.py file with

def run_commands(module, commands, check_rc=True):
...
        command = " " + command
        
        try:
            out = connection.get(command, prompt, answer)

This does the trick for me without having to change all the code. Cheers!

sgrimee avatar Feb 01 '22 15:02 sgrimee

@felixfontein would you accept a MR with this workaround (adding a space in front of every command) until a cleaner solution can be found? I have been running with it for a year now and it does not appear to cause any harm. But without it it breaks output validation from 6.49 on...

sgrimee avatar Mar 13 '23 13:03 sgrimee

@sgrimee I think it would be OK if this is only done for RouterOS versions where this problem actually happens (i.e. major version 6, starting at 6.49). Since the version is already retrieved it should be feasible to implement such a check - Cliconf.get_device_info() does that, though I don't know where that information is stored and how it can be obtained in the module utils.

felixfontein avatar Mar 19 '23 12:03 felixfontein

I'm going to reopen this since #161 is more of a band-aid than a proper fix.

felixfontein avatar Mar 23 '23 20:03 felixfontein