# How to improve this automatic RouterOS and Mikrotik Firmware update script?

**URL:** <https://forum.ansible.com/t/how-to-improve-this-automatic-routeros-and-mikrotik-firmware-update-script/3828>\
**Category:** Get Help\
**Tags:** mikrotik\
**Created:** [February 12, 2024, 3:03pm UTC](https://forum.ansible.com/t/how-to-improve-this-automatic-routeros-and-mikrotik-firmware-update-script/3828 "2024-02-12T15:03:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GG000888](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@GG000888](https://forum.ansible.com/u/GG000888)\
**Post date:** [February 12, 2024, 3:03pm UTC](https://forum.ansible.com/t/how-to-improve-this-automatic-routeros-and-mikrotik-firmware-update-script/3828/1 "2024-02-12T15:03:40Z")

</div>

Good evening, after several searches for a program to automatically update RouterOS and its firmware to a specific version I found Ansible, a tool that is working well for now.

I created this script and it is working but it would be much more convenient to be able to send commands to the routerboard after a certain number of seconds  
using a delay in this way it should be possible to send the various commands to the Routerboard without having to use /system script and /system scheduler, could such a configuration be possible?

I was thinking of using the form

“ansible.builtin.wait\_for module”  
oppure “loop (until) e when”

What do you think?

```auto
#Sure work with RouterOS 6.48.6
---
- name: Auto-Upgrade-and-Update-Mikrotik-Script
  hosts: mikrotik
  
  tasks:
  #Upload the .npk file (or any other file) of the operating system to the root "/" on Mikrotik (ip mikrotik:folder root)
  - name: Execute SCP Secure Copy Protocol
    ansible.builtin.shell: scp routeros-mipsbe-6.49.8.npk admin@192.168.88.1:/
    
  - name: UPGRADE FIRMWARE with Mikrotik scripts and scheduler 
    community.network.routeros_command:
     commands:
     - /system scheduler add name=Start-Upgrade-and-reboot-script on-event=":delay 8s;\r\
       \n/system scheduler disable Start-Upgrade-and-reboot-script \r\
       \n/system script run upgrade-and-reboot\r\
       \n" policy=\
       ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
       start-time=startup
     - /system script add dont-require-permissions=no name=upgrade-and-reboot owner=admin policy=\
       ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
       "/sys routerboard upgrade\r\
       \n:delay 8s;\r\
       \n/sys reboot\r\
       \n"
  #Simple Reboot
  - name: Reboot
    community.routeros.command:
      commands:
        - :execute {/sys reboot;} 

```

---

<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 13, 2024, 8:56pm UTC](https://forum.ansible.com/t/how-to-improve-this-automatic-routeros-and-mikrotik-firmware-update-script/3828/2 "2024-02-13T20:56:14Z")

</div>

Two unrelated comments:

1. Instead of `command` with `scp`, you should be able to use the [`net_put` module](https://docs.ansible.com/ansible/devel/collections/ansible/netcommon/net_put_module.html#ansible-collections-ansible-netcommon-net-put-module).
2. The `community.network.routeros_command` module was moved and renamed to `community.routeros.command` a long time ago. If you update the FQCN, you don’t need the `community.network` collection anymore, but only `community.routeros` (you needed the latter one anyway since `community.network` only contains a redirect for that module).

Regarding your question: the `community.routeros.command` module has a `wait_for` option that might be useful to wait for a specific message in the output, like `Firmware upgraded successfully, please reboot for changes to take effect`. I’ve never tried out `wait_for` myself, but I think that’s one of the use-cases this was added for.

---

<div class="post-metadata">

**Author:** ![GG000888](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@GG000888](https://forum.ansible.com/u/GG000888)\
**Post date:** [February 14, 2024, 11:44am UTC](https://forum.ansible.com/t/how-to-improve-this-automatic-routeros-and-mikrotik-firmware-update-script/3828/3 "2024-02-14T11:44:34Z")

</div>

Excellent, thank you very much, I will look into your considerations
