Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RouterOS - issue with routeros_facts when interface has a space in name #8

Closed
danpospisil opened this issue May 21, 2020 · 13 comments
Closed

Comments

@danpospisil
Copy link

SUMMARY

When an interface has a space in its name, the routeros_facts module will fail.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

routeros

ANSIBLE VERSION
ansible 2.9.9
  config file = None
  configured module search path = ['/home/daniel/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.9 (default, Apr 18 2020, 01:56:04) [GCC 8.4.0]
CONFIGURATION
no changes
OS / ENVIRONMENT

Windows 10, Ubuntu WSL 1

STEPS TO REPRODUCE
  1. change an interface name to: "spaces in name"
  2. run the example playbook
- name: Mikrotik Playbook
  hosts: all
  gather_facts: no
  tasks:
  - name: Get Facts
    routeros_facts:
      gather_subset: all
EXPECTED RESULTS

No crash should occur

ACTUAL RESULTS

Ansible will crash - it extracts not the whole name when spaces are present in it

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: u'spaces'

To fix this I have changed the extraction regexp in routeros_facts.py file:
function parse_name:
match = re.search(r'name=\"([\w\d\-\s]+)\"', data, re.M)
function parse_interface:
match = re.search(r'\sinterface=([\w\d\-\s]+[\w\d\-])\s', data, re.M)

@felixfontein
Copy link
Collaborator

CC @heuels

@heuels
Copy link
Collaborator

heuels commented May 25, 2020

Hey @danpospisil, thank for reporting this. Would you have the time to create a pull request with the fix you're proposing?


@felixfontein, thanks for mentioning me ;)

@danpospisil
Copy link
Author

@heuels - sure, but first I want to ask which repo is correct? I see there was some massive refactoring done in this separate repo (i suppose it will be used starting Ansible 2.10.x, but Ansible 2.9.9 is using the original repo - https://github.com/ansible/ansible/blob/v2.9.9/lib/ansible/modules/network/routeros/routeros_facts.py and not this one.
Thanks for clarification!

@felixfontein
Copy link
Collaborator

All changes have to get merged in this repo first before they can be backported to the stable-2.9 branch in the ansible/ansible repo.

@danpospisil
Copy link
Author

@felixfontein - unfortunately routeros_facts.py file in this repo after refactoring doesn't even contain parse_name and parse_interface functions where I did those modifications. So I guess I will leave this issue just like a warning "dont use spaces in your interface names". I know how ti fix it in Ansible 2.9.9 but I havent explored it in this completely different code that is here (it fails too though).

@heuels
Copy link
Collaborator

heuels commented May 28, 2020

@danpospisil, I could try and fix this for both current and previous versions :)

@felixfontein felixfontein transferred this issue from ansible-collections/community.network Nov 1, 2020
@zagi-tng
Copy link

zagi-tng commented Jul 6, 2021

Similar happens when using routeros_command and there is a interface list delimited with comma

for example:

community.network.routeros_command:
commands: /interface wireless cap set caps-man-names=WIFI_Manager1,WIFI_Manager2 discovery-interfaces=ether1 interfaces=wlan1,wlan2 enabled=yes

..

      "<_02 discovery-interfaces=ether1 interfaces=wlan1",
            "<_02 discovery-interfaces=ether1 interfaces=wlan1",
            "",
            "expected command name (line 1 column 1)"
        ],
        [
            "bad command name wlan2 (line 1 column 1)"

@felixfontein
Copy link
Collaborator

@zagi-tng this could also be caused by line wrapping. You could try adding +cet512w to your RouterOS username (see 3. in https://docs.ansible.com/ansible/devel/collections/community/routeros/docsite/ssh-guide.html#important-notes).

@virtualguy
Copy link

virtualguy commented Jul 7, 2021

I can reproduce the issue with commas too, looks like its splitting it into different commands on the comma. Fixed by wrapping the command in single quotes

E.g.:

TASK [router : run "/interface ethernet set [ find default-name=sfp1 ] advertise=10M-half,10M-full,100M-half,100M-full,1000M-half,1000M-full arp=enabled arp-timeout=auto auto-negotiation=yes bandwidth=unlimited/unlimited disabled=no full-duplex=yes l2mtu=1598 loop-protect=default loop-protect-disable-time=5m loop-protect-send-interval=5s mac-address=C4:AD:34:B9:34:0F mtu=1500 name=sfp1 orig-mac-address=C4:AD:34:B9:34:0F rx-flow-control=off sfp-shutdown-temperature=95C speed=1Gbps tx-flow-control=off"] ***
task path: /home/developer/instant-networks/ansible/roles/router/tasks/mikrotik-command.yml:3
redirecting (type: terminal) community.network.routeros to community.routeros.routeros
redirecting (type: cliconf) community.network.routeros to community.routeros.routeros
<10.70.0.159> ESTABLISH LOCAL CONNECTION FOR USER: developer
<10.70.0.159> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr `"&& mkdir "` echo /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412 `" && echo ansible-tmp-1625622046.650678-869261-254331226435412="` echo /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412 `" ) && sleep 0'
redirecting (type: modules) community.network.routeros_command to community.routeros.command
Using module file /usr/lib/python3.9/site-packages/ansible_collections/community/routeros/plugins/modules/command.py
<10.70.0.159> PUT /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/tmpfwx6dp4i TO /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412/AnsiballZ_command.py
<10.70.0.159> EXEC /bin/sh -c 'chmod u+x /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412/ /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412/AnsiballZ_command.py && sleep 0'
<10.70.0.159> EXEC /bin/sh -c '/usr/bin/python /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412/AnsiballZ_command.py && sleep 0'
<10.70.0.159> EXEC /bin/sh -c 'rm -f -r /home/developer/.ansible/tmp/ansible-local-868661rk7m4bmr/ansible-tmp-1625622046.650678-869261-254331226435412/ > /dev/null 2>&1 && sleep 0'
ok: [6736d821-e1ed-40e2-8652-4e402ec602af.example.com] => changed=false 
  invocation:
    module_args:
      commands:
      - /interface ethernet set [ find default-name=sfp1 ] advertise=10M-half
      - 10M-full
      - 100M-half
      - 100M-full
      - 1000M-half
      - 1000M-full arp=enabled arp-timeout=auto auto-negotiation=yes bandwidth=unlimited/unlimited disabled=no full-duplex=yes l2mtu=1598 loop-protect=default loop-protect-disable-time=5m loop-protect-send-interval=5s mac-address=C4:AD:34:B9:34:0F mtu=1500 name=sfp1 orig-mac-address=C4:AD:34:B9:34:0F rx-flow-control=off sfp-shutdown-temperature=95C speed=1Gbps tx-flow-control=off
      interval: 1
      match: all
      retries: 10
      wait_for: null
  stdout:
  - ''
  - syntax error (line 1 column 3)
  - syntax error (line 1 column 4)
  - syntax error (line 1 column 4)
  - syntax error (line 1 column 5)
  - syntax error (line 1 column 5)
  stdout_lines: <omitted>

@felixfontein
Copy link
Collaborator

BTW, this issue is about community.routeros.facts, not about community.routeros.command.

In any case, you seem to be supplying a string to the commands argument of community.routeros.command, which expects a list and not a string. Ansible helps you by splitting a string along comma into a list. If you don't want to do that, supply a list with one element instead:

- community.routeros.command:
    commands:
      - put command here

@zagi-tng
Copy link

zagi-tng commented Jul 7, 2021

Fixed by wrapping the command in single quotes

yes. that works.
and i use 'username+cet1024w'

tasks:
    - name: executes bootstrap commands
      community.network.routeros_command:
        commands:
        - '/interface wireless cap set discovery-interfaces=ether1 enabled=yes caps-man-names=WIFI_Manager02,WIFI_Manager01 interfaces=wlan1,wlan2'
        - '/interface ethernet set [ find default-name=ether1 ] auto-negotiation=yes speed=1Gbps tx-flow-control=off rx-flow-control=off'
        - '/ip service set ftp disabled=yes'
....

@NikolayDachev
Copy link
Collaborator

Fixed by wrapping the command in single quotes

yes. that works.
and i use 'username+cet1024w'

tasks:
    - name: executes bootstrap commands
      community.network.routeros_command:
        commands:
        - '/interface wireless cap set discovery-interfaces=ether1 enabled=yes caps-man-names=WIFI_Manager02,WIFI_Manager01 interfaces=wlan1,wlan2'
        - '/interface ethernet set [ find default-name=ether1 ] auto-negotiation=yes speed=1Gbps tx-flow-control=off rx-flow-control=off'
        - '/ip service set ftp disabled=yes'
....

I guess we can close this one ?

@felixfontein
Copy link
Collaborator

I think we can. There's not much more we can do here I guess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants