# How to configure routeros with ansible?

**URL:** <https://forum.ansible.com/t/how-to-configure-routeros-with-ansible/40516>\
**Category:** Get Help\
**Created:** [February 22, 2025, 9:09am UTC](https://forum.ansible.com/t/how-to-configure-routeros-with-ansible/40516 "2025-02-22T09:09:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tchangalo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tchangalo/32/7679_2.png) [@Tchangalo](https://forum.ansible.com/u/Tchangalo)\
**Post date:** [February 22, 2025, 9:09am UTC](https://forum.ansible.com/t/how-to-configure-routeros-with-ansible/40516/1 "2025-02-22T09:09:17Z")

</div>

Hi, has anyone ever configured a MikroTik with Ansible? If so, it would be great if you could show me what the ansible.cfg should look like and which variables I need to specify in the playbook.  
The Mikrotik is set up on PVE, where I have set up ansible in a .venv  
I’ve tried many variations, but it always fails because the SSH connection doesn’t work. The MikroTik already has the public key from the PVE, it is also reachable via ping from the PVE, and SSH from the PVE to the MikroTik works via the terminal as well.  
At the moment I have:

```auto
---
- name: Configure MTK
  gather_facts: false
  connection: ansible.netcommon.network_cli
  vars:
    ansible_network_os: community.network.routeros
    ansible_user: admin
    ansible_ssh_private_key_file: ~/.ssh/id_ed25519
    ansible_command_timeout: 120
    ansible_ssh_timeout: 120
    ansible_password: mypassword
    ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q bastion01"'
    ansible_become: true
    ansible_become_method: enable
    ansible_become_password: mypassword
    # ansible_ssh_private_key_file: ~/.ssh/id_rsa
    # ansible_connection: ansible.netcommon.httpapi
    # ansible_httpapi_use_ssl: false
    # ansible_httpapi_validate_certs: false
    # ansible_python_interpreter: /usr/bin/python3
    # ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/tmp/ansible-ssh-%h-%p-%r"'
    
  hosts: router

  tasks:
    - name: set_interface_mikrotik
      community.network.routeros_command:
        commands:
          - /ip address add address={{ item.ipv4_address }} interface={{ item.interface }}
      loop: "{{ interfaces }}"
      loop_control:
        label: "{{ item.interface }}"
      tags: interfaces
      when: os == "mikrotik"

```

and ansible.cfg:

```auto
[defaults]
inventory = /home/user/ansible/inventories/
host_key_checking = False
deprecation_warnings = False
callback_plugins = ./callbacks
stdout_callback = yaml

[ssh_connection]
pipelining = False
ssh_args = -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=10
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
retries = 3

[persistent_connection]
ssh_type = libssh

```

and I get:

```auto
 msg: 'ssh connection failed: ssh connect failed: Socket error: Connection reset by peer'

```

the mtk-log is:

```auto

[admin@MikroTik] > /log print
 2025-02-22 11:15:19 system,info router rebooted
 2025-02-22 11:15:19 interface,info lo link up
 2025-02-22 11:15:19 interface,info ether2 link up
 2025-02-22 11:15:19 interface,info ether3 link up
 2025-02-22 11:15:19 interface,info ether4 link up
 2025-02-22 11:15:24 dhcp,info dhcp-client on ether1 got IP address 10.20.3
0.110
 2025-02-22 11:15:34 system,info,account user admin logged in from 10.20.30
.254 via ssh
 2025-02-22 11:15:34 system,info system identity changed by ssh-cmd:admin@1
0.20.30.254 (/system identity set name=MikroTik)
 2025-02-22 11:15:34 system,info user admin changed by ssh-cmd:admin@10.20.
30.254/action:0 (/user set admin)
 2025-02-22 11:15:34 system,info ip service changed by ssh-cmd:admin@10.20.
30.254/action:1 (/ip service set ssh disabled=no)
 2025-02-22 11:15:34 system,info,account user admin logged out from 10.20.3
0.254 via ssh
 2025-02-22 11:15:41 system,info,account user admin logged in from 10.20.30
.254 via ssh
 2025-02-22 11:15:41 system,info,account user admin logged out from 10.20.3
0.254 via ssh
 2025-02-22 11:15:43 system,info,account user admin logged in from 10.20.30
.254 via ssh
 2025-02-22 11:15:43 ssh,info 256bit public key imported, key-owner = user@
node1
 2025-02-22 11:15:43 system,info,account user admin logged out from 10.20.3
0.254 via ssh
 2025-02-22 11:15:43 ssh,info publickey accepted for user: admin
 2025-02-22 11:15:43 system,info,account user admin logged in from 10.20.30
.254 via ssh
 2025-02-22 11:15:43 system,info,account user admin logged out from 10.20.3
0.254 via ssh
 2025-02-22 11:15:43 ssh,info publickey accepted for user: admin
 2025-02-22 11:15:43 system,info,account user admin logged in from 10.20.30
.254 via ssh
 2025-02-22 11:15:43 interface,info p1r8v link up
 2025-02-22 11:15:43 system,info device added by ssh-cmd:admin@10.20.30.254
 (*8 = /interface vlan add interface=ether3 name=p1r8v vlan-id=810)
 2025-02-22 11:15:43 interface,info p1r9v link up
 2025-02-22 11:15:43 system,info device added by ssh-cmd:admin@10.20.30.254
 (*9 = /interface vlan add interface=ether2 name=p1r9v vlan-id=910)
 2025-02-22 11:15:43 system,info,account user admin logged out from 10.20.3
0.254 via ssh
 2025-02-22 11:15:53 system,info,account user admin logged in from fe80::51
6d:ee13:3f25:2555 via winbox
 2025-02-22 11:15:57 system,info,account user admin logged in from fe80::51
6d:ee13:3f25:2555 via winbox

```

At the moment when my create-mtk.sh stops and the playbook starts, nothing more appears in this mtk-log anymore.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 24, 2025, 5:52am UTC](https://forum.ansible.com/t/how-to-configure-routeros-with-ansible/40516/2 "2025-02-24T05:52:58Z")

</div>

I’m using Ansible to configure RouterOS. I have no special settings in ansible.cfg, the following variables set up in the inventory:

```yaml
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: community.routeros.routeros
ansible_network_cli_ssh_type: libssh
ansible_ssh_pass: "{{ router_password }}"
ansible_user: "{{ router_username }}+cte512w"
ansible_host: 192.168.1.1

```

I mostly use the API modules, I only use the SSH-based modules to ‘bootstrap’ a new device (enable API, install PKI, switch to right IP range).

---

<div class="post-metadata">

**Author:** ![herman45](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@herman45](https://forum.ansible.com/u/herman45)\
**Post date:** [August 25, 2026, 3:16pm UTC](https://forum.ansible.com/t/how-to-configure-routeros-with-ansible/40516/5 "2026-08-25T15:16:52Z")

</div>

The SSH itself looks fine since the MikroTik log shows the Ansible host successfully authenticating with the public key. The problem is more likely the Ansible `network_cli`/RouterOS connection setup.

Remove the [_`ProxyCommand`_](https://19216881ip.com/), `become`, and most of the SSH overrides first and test with the simplest configuration. Also, for newer RouterOS versions, use `ansible.netcommon.network_cli` with `community.routeros` rather than the old `community.network.routeros` collection.

Something along these lines should be enough:

```auto
- hosts: router

```

gather\_facts: false

connection: ansible.netcommon.network\_cli

vars:

```auto
ansible_network_os: community.routeros.routeros

ansible_user: admin

ansible_private_key_file: \~/.ssh/id_ed25519

```

tasks:

```auto
- name: Test connection

  community.routeros.command:

    commands:

      - /system identity print

```

Then test it with `ansible-playbook -vvv`. If the normal [_`SSH command`_](https://speedport-ips.de/) works but this still gives `Connection reset by peer`, check the RouterOS SSH service settings and make sure Ansible isn’t opening multiple connections too quickly.

The log is actually useful here: there are successful SSH logins immediately before the playbook starts, but nothing when Ansible fails. That suggests the failure may be happening in Ansible’s persistent connection layer rather than authentication itself.
