Community discussions

MikroTik App
 
akelsey
just joined
Topic Author
Posts: 6
Joined: Wed Jul 22, 2015 9:37 am

Ansible breaks stdout if string longer 8 symbols

Mon Nov 14, 2022 10:35 pm

Hello everybody,

I would like to automate my home network and maybe apply this after test in production then, but I faced with an strange issue which cannot solve by myself.

So I have installed CHR and group_vars for "all" (Off course inventory as well):
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: community.routeros.routeros
ansible_port: 22
ansible_user: ansible
ansible_password: "1"
host_key_checking: false
and a simple playbook:

- hosts: all
  name: Test
  gather_facts: true
  tasks:
    - name: Check rule
      community.routeros.command:
        commands:
          - :put [/ip firewall filter get [find where comment="1234567"]]
      register: does_exist
    
    - name: Debug
      ansible.builtin.debug:
        msg:
          - "{{ does_exist.stdout }}"
ansible version
ansible [core 2.13.4]
  config file = /home/aleksey/.ansible.cfg
  configured module search path = ['/home/aleksey/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/aleksey/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/aleksey/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/aleksey/.local/bin/ansible
  python version = 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0]
  jinja version = 3.1.2
  libyaml = True
So pay attention on the sting in "find" if it is "1234567" the stdout is:

changed: [vbox2] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "commands": [
                ":put [/ip firewall filter get [find where comment \"1234567\"]]"
            ],
            "interval": 1,
            "match": "all",
            "retries": 10,
            "wait_for": null
        }
    },
    "stdout": [
        "no such itemno such item"
    ],
    "stdout_lines": [
        [
            "no such itemno such item"
        ]
    ]
}
changed: [vbox1] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "commands": [
                ":put [/ip firewall filter get [find where comment \"1234567\"]]"
            ],
            "interval": 1,
            "match": "all",
            "retries": 10,
            "wait_for": null
        }
    },
    "stdout": [
        "no such itemno such item"
    ],
    "stdout_lines": [
        [
            "no such itemno such item"
        ]
    ]
}
but if I changed it to "12345678" (+1 symbol, this is doesn't matter letters or digits, just digits are countable :-))

changed: [vbox2] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "commands": [
                ":put [/ip firewall filter get [find where comment \"12345678\"]]"
            ],
            "interval": 1,
            "match": "all",
            "retries": 10,
            "wait_for": null
        }
    },
    "stdout": [
        ":put [/ip firewall filter get [find where comment \"12345678\"]\n<ter get [find where comment \"12345678\"]]                                      \n<ter get [find where comment \"12345678\"]]\n<ter get [find where comment \"12345678\"]]\n\nno such item:put [/ip firewall filter get [find where comment \"12345678\"]\n<ter get [find where comment \"12345678\"]]                                      \n<ter get [find where comment \"12345678\"]]\n<ter get [find where comment \"12345678\"]]\n\nno such item"
    ],
    "stdout_lines": [
        [
            ":put [/ip firewall filter get [find where comment \"12345678\"]",
            "<ter get [find where comment \"12345678\"]]                                      ",
            "<ter get [find where comment \"12345678\"]]",
            "<ter get [find where comment \"12345678\"]]",
            "",
            "no such item:put [/ip firewall filter get [find where comment \"12345678\"]",
            "<ter get [find where comment \"12345678\"]]                                      ",
            "<ter get [find where comment \"12345678\"]]",
            "<ter get [find where comment \"12345678\"]]",
            "",
            "no such item"
        ]
    ]
}
I thought it is "paramiko" library issue (as I know ansible is using it for connection to mikrotik) and I wrote simple script in python:

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.104.61', username='ansible', password='1', port=22, allow_agent=False, look_for_keys=False)
stdin, stdout, stderr = client.exec_command(':put [/ip firewall filter get [find where comment "123456789101234567891012345678910"]]')

for line in stdout:
    print(line.strip('\n'))
client.close()
and you know - it shows perfect stdout:

[Running] python -u "/home/aleksey/dev/mine/mikrotiks_vbox/tempCodeRunnerFile.py"
no such item
Mikrotik VM /system/resource/print:

                   uptime: 3h17m45s
                  version: 7.6 (stable)
               build-time: Oct/17/2022 10:55:40
         factory-software: 7.1
              free-memory: 17.7MiB
             total-memory: 96.0MiB
                      cpu: AMD
                cpu-count: 1
            cpu-frequency: 3399MHz
                 cpu-load: 0%
           free-hdd-space: 186.6MiB
          total-hdd-space: 204.2MiB
  write-sect-since-reboot: 5480
         write-sect-total: 5480
        architecture-name: x86_64
               board-name: x86
                 platform: MikroTik



So I don't know the root cause (mikrotik chr? ssh connection? network issue? ansible bug? AMD cpu bug?) how to fix it and any help is appreciated.
 
akelsey
just joined
Topic Author
Posts: 6
Joined: Wed Jul 22, 2015 9:37 am

Re: Ansible breaks stdout if string longer 8 symbols

Tue Nov 15, 2022 4:50 pm

Hi,
I found a solution and just put it here for anyone who will have similar issue.
Maybe this is obliviously maybe not but I found solution here: https://docs.ansible.com/ansible/latest ... teros.html
If you are getting timeout errors you may want to add +cet1024w suffix to your username which will disable console colors, enable “dumb” mode, tell RouterOS not to try detecting terminal capabilities and set terminal width to 1024 columns. See article Console login process in MikroTik wiki for more information.
This is not related to my issue directly, but it solved my problem.
 
User avatar
chechito
Forum Guru
Forum Guru
Posts: 2989
Joined: Sun Aug 24, 2014 3:14 am
Location: Bogota Colombia
Contact:

Re: Ansible breaks stdout if string longer 8 symbols

Tue Nov 15, 2022 6:30 pm

Hi,
I found a solution and just put it here for anyone who will have similar issue.
Maybe this is obliviously maybe not but I found solution here: https://docs.ansible.com/ansible/latest ... teros.html
If you are getting timeout errors you may want to add +cet1024w suffix to your username which will disable console colors, enable “dumb” mode, tell RouterOS not to try detecting terminal capabilities and set terminal width to 1024 columns. See article Console login process in MikroTik wiki for more information.
This is not related to my issue directly, but it solved my problem.
interesting, thanks for sharing

Who is online

Users browsing this forum: No registered users and 17 guests