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
  
  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;} 

Two unrelated comments:

  1. Instead of command with scp, you should be able to use the 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.

3 Likes

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