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

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?

#Sure work with RouterOS 6.48.6
- name: Auto-Upgrade-and-Update-Mikrotik-Script
  hosts: mikrotik
  #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 scp routeros-mipsbe-6.49.8.npk admin@
  - name: UPGRADE FIRMWARE with Mikrotik scripts and scheduler
     - /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 \
     - /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\
  #Simple Reboot
  - name: Reboot
        - :execute {/sys reboot;} 

Two unrelated comments:

  1. Instead of command with scp, you should be able to use the net_put module.
  2. The module was moved and renamed to community.routeros.command a long time ago. If you update the FQCN, you don’t need the collection anymore, but only community.routeros (you needed the latter one anyway since 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.


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