I have the agent write a script that is aimed to be idempotent and then run the script.
You can take this one step further and have the agent write Terraform configs [1]. I did this (including having the agent import all the initial resources from the live device), works great and is generally more robust than a script.
[1] https://github.com/terraform-routeros/terraform-provider-rou...