{"id":6254,"date":"2020-04-29T10:19:45","date_gmt":"2020-04-29T16:19:45","guid":{"rendered":"http:\/\/gregsowell.com\/?p=6254"},"modified":"2020-04-29T10:19:45","modified_gmt":"2020-04-29T16:19:45","slug":"ansible-routeros-module-quirk-with-commas","status":"publish","type":"post","link":"https:\/\/gregsowell.com\/?p=6254","title":{"rendered":"Ansible Routeros Module Quirk With Commas"},"content":{"rendered":"<p>When you are using commas in a playbook with Ansible on the routeros module you will have a bad time unless you do this one special trick.<\/p>\n<p>First here&#8217;s my example playbook:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">---\r\n- name: wlan2 radio tuning\r\n  connection: network_cli\r\n  gather_facts: false\r\n  hosts: mtk3\r\n\r\n  tasks:\r\n\r\n    - name: Add vlan config back for user vlans\r\n      routeros_command:\r\n        commands: &quot;\/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2,ether3,ether4,ether5,wlan2 vlan-ids={{ hostvars[inventory_hostname][&#039;user_vlan&#039;] }}&quot;<\/code><\/pre>\n<p>When I run this it just fails.<br \/>\nWhen I run it with debugging -vvv I get:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">fatal: [mtk3]: FAILED! =&gt; {\r\n    &quot;ansible_facts&quot;: {\r\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\r\n    },\r\n    &quot;changed&quot;: false,\r\n    &quot;invocation&quot;: {\r\n        &quot;module_args&quot;: {\r\n            &quot;commands&quot;: [\r\n                &quot;\/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2&quot;,\r\n                &quot;ether3&quot;,\r\n                &quot;ether4&quot;,\r\n                &quot;ether5&quot;,\r\n                &quot;wlan2 vlan-ids=201&quot;\r\n            ],\r\n            &quot;interval&quot;: 1,\r\n            &quot;match&quot;: &quot;all&quot;,\r\n            &quot;retries&quot;: 10,\r\n            &quot;wait_for&quot;: null\r\n        }\r\n    },\r\n    &quot;msg&quot;: &quot;command timeout triggered, timeout value is 30 secs.\\nSee the timeout setting options in the Network Debug and Troubleshooting Guide.&quot;\r\n}<\/code><\/pre>\n<p>You can see that it interprets the &#8220;untagged=ether2,ether3,ether4,ether5,wlan2&#8221; portion as a list and tries to put each one on a separate line, which totally breaks the command.<\/p>\n<p>I tried a million different things including putting the interface list into an actual list, then joining it back together in the command with the join command:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">   - name: Add vlan config back for user vlans\r\n      routeros_command:\r\n        commands: &quot;\/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged={{ my_joined_list | join(&#039;,&#039;) }} vlan-ids={{ hostvars[inventory_hostname][&#039;user_vlan&#039;] }}&quot;<\/code><\/pre>\n<p>But the output shows it does the same thing&#8230;:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">fatal: [mtk3]: FAILED! =&gt; {\r\n    &quot;ansible_facts&quot;: {\r\n        &quot;discovered_interpreter_python&quot;: &quot;\/usr\/libexec\/platform-python&quot;\r\n    },\r\n    &quot;changed&quot;: false,\r\n    &quot;invocation&quot;: {\r\n        &quot;module_args&quot;: {\r\n            &quot;commands&quot;: [\r\n                &quot;\/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2&quot;,\r\n                &quot;ether3&quot;,\r\n                &quot;ether4&quot;,\r\n                &quot;ether5&quot;,\r\n                &quot;wlan2 vlan-ids=201&quot;\r\n            ],<\/code><\/pre>\n<p>I eventually found <a href=\"https:\/\/github.com\/ansible\/ansible\/issues\/63802\">this bug report<\/a>, which explains that the routeros module expects to see a list of commands, so if you just slightly modify the command structure it will interpret things correctly <strong>*sigh*<\/strong>.<\/p>\n<p>Here&#8217;s the working version of the task:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">    - name: Add vlan config back for user vlans\r\n      routeros_command:\r\n        commands: \r\n          - &quot;\/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2,ether3,ether4,ether5,wlan2 vlan-ids={{ hostvars[inventory_hostname][&#039;user_vlan&#039;] }}&quot;<\/code><\/pre>\n<p>Notice that all I did was move the command itself to a new line and add the &#8220;-&#8221; before it.  Now it interprets everything as a single line, ignoring the commas.<\/p>\n<p>I hope you find this to be useful, and possibly save you a good chunk of time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are using commas in a playbook with Ansible on the routeros module you will have a bad time unless you do this\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"class_list":["post-6254","post","type-post","status-publish","format-standard","hentry","category-mikrotik","category-networking"],"_links":{"self":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6254"}],"version-history":[{"count":1,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6254\/revisions"}],"predecessor-version":[{"id":6257,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6254\/revisions\/6257"}],"wp:attachment":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}