{"id":5688,"date":"2024-02-21T17:15:00","date_gmt":"2024-02-21T23:15:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5688"},"modified":"2024-02-27T17:33:34","modified_gmt":"2024-02-27T23:33:34","slug":"ansible-playbook-for-updating-mikrotik-routeros","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/ansible-playbook-for-updating-mikrotik-routeros\/","title":{"rendered":"Ansible Playbook for Updating Mikrotik RouterOS"},"content":{"rendered":"\n<p>This playbook is for updating Mikrotik routers.  It will update both the RouterOS version and the firmware.  <\/p>\n\n\n\n<p>The playbook executes in the following order.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Check for RouterOS Updates<\/li>\n\n\n\n<li>Update RouterOS (Router will reboot if there is an update)<\/li>\n\n\n\n<li>Sleep 120 seconds to allow the router(s) to boot up<\/li>\n\n\n\n<li>Check current firmware version, and if there is an available upgrade<\/li>\n\n\n\n<li>Update firmware<\/li>\n\n\n\n<li>Reboot router to apply firmware upgrade<\/li>\n<\/ol>\n\n\n\n<p>This playbook attempts to be smart and will not reboot a router if there is not an update available.  Routers that have updates available will reboot twice.  Once to apply the RouterOS version, and the second time to apply the firmware.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>You should already have an inventory file and the Ansible RouterOS collection installed.  If not, check out the following post.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-incredigeek wp-block-embed-incredigeek\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"4iTR4M0KRM\"><a href=\"https:\/\/www.incredigeek.com\/home\/setup-ansible-and-mikrotik-routeros\/\">Setup Ansible and Mikrotik RouterOS<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Setup Ansible and Mikrotik RouterOS&#8221; &#8212; Incredigeek\" src=\"https:\/\/www.incredigeek.com\/home\/setup-ansible-and-mikrotik-routeros\/embed\/#?secret=XvSAIEcLe6#?secret=4iTR4M0KRM\" data-secret=\"4iTR4M0KRM\" width=\"584\" height=\"329\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><figcaption class=\"wp-element-caption\">Setup Ansible host file and RouterOS collection<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Playbook<\/h2>\n\n\n\n<p>Here is the playbook.  <br>A quick command syntax note, RouterOS 7 and newer typically use slashes \/ between commands. i.e. <code>\/system\/package\/update\/install<\/code>. Older versions of RouterOS have spaces in the command path i.e. <code>\/system package update install<\/code> Since this still works on newer versions, we use it here.<\/p>\n\n\n\n<pre title=\"Mikrotik Update Playbook\" class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"yaml\" class=\"language-yaml line-numbers\">---\n- name: Mikrotik RouterOS and Firmware Upgrades\n  hosts: routers\n  gather_facts: false\n  tasks:\n\n# Update RouterOS version.  Mikrotik update\/install command automatically reboots the router\n  - name: Check for RouterOS updates\n    community.routeros.command:\n      commands:\n        - \/system package update check-for-updates\n    register: system_update_print\n\n  - name: Update RouterOS version\n    community.routeros.command:\n      commands:\n        - \/system package update install\n    when: system_update_print is not search('System is already up to date')\n\n# Check if firmware needs an upgrade, upgrade and reboot.\n  - name: Sleeping for 120 seconds.  Giving time for routers to reboot.\n    ansible.builtin.wait_for:\n      timeout: 120\n    delegate_to: localhost\n      \n  - name: Check Current firmware\n    community.routeros.command:\n      commands:\n        - ':put [\/system routerboard get current-firmware]'\n    register: firmware_current\n\n  - name: Check Upgrade firmware \n    community.routeros.command:\n      commands:\n        - ':put [\/system routerboard get upgrade-firmware]'\n    register: firmware_upgrade\n\n  - name: Upgrade firmware\n    community.routeros.command:\n      commands:\n        - ':execute script=\"\/system routerboard upgrade\"'\n    when: firmware_current != firmware_upgrade\n\n  - name: Wait for firmware upgrade and then reboot\n    community.routeros.command:\n      commands:\n        - \/system routerboard print\n    register: Reboot_Status\n    until: \"Reboot_Status is search(\\\"please reboot\\\")\"\n    notify:\n      - Reboot Mikrotik\n    retries: 3\n    delay: 15\n    when: firmware_current != firmware_upgrade\n\n  handlers:\n    - name : Reboot Mikrotik\n      community.routeros.command:\n        commands:\n          - ':execute script=\"\/system reboot\"'\n<\/code><\/pre>\n\n\n\n<p>Run the playbook with <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ansible-playbook -i routers.ini mikrotik_update.yaml<\/pre>\n\n\n\n<p>Change routers.ini to your router inventory.<br>mikrotik_update.yaml to whatever you end up calling the playbook.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This playbook is for updating Mikrotik routers. It will update both the RouterOS version and the firmware. The playbook executes in the following order. This playbook attempts to be smart and will not reboot a router if there is not &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/ansible-playbook-for-updating-mikrotik-routeros\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1646,452],"tags":[1193,244,301,388],"class_list":["post-5688","post","type-post","status-publish","format-standard","hentry","category-ansible","category-mikrotik","tag-ansible","tag-automation","tag-mikrotik","tag-routeros"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5688"}],"version-history":[{"count":7,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5688\/revisions"}],"predecessor-version":[{"id":5731,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5688\/revisions\/5731"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}