Ansible Routeros Module Quirk With Commas
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.
First here’s my example playbook:
1 2 3 4 5 6 7 8 9 10 11 | --- - name: wlan2 radio tuning connection: network_cli gather_facts: false hosts: mtk3 tasks: - name: Add vlan config back for user vlans routeros_command: commands: "/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2,ether3,ether4,ether5,wlan2 vlan-ids={{ hostvars[inventory_hostname]['user_vlan'] }}" |
When I run this it just fails.
When I run it with debugging -vvv I get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | fatal: [mtk3]: FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "invocation": { "module_args": { "commands": [ "/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2", "ether3", "ether4", "ether5", "wlan2 vlan-ids=201" ], "interval": 1, "match": "all", "retries": 10, "wait_for": null } }, "msg": "command timeout triggered, timeout value is 30 secs.\nSee the timeout setting options in the Network Debug and Troubleshooting Guide." } |
You can see that it interprets the “untagged=ether2,ether3,ether4,ether5,wlan2” portion as a list and tries to put each one on a separate line, which totally breaks the command.
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:
1 2 3 | - name: Add vlan config back for user vlans routeros_command: commands: "/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged={{ my_joined_list | join(',') }} vlan-ids={{ hostvars[inventory_hostname]['user_vlan'] }}" |
But the output shows it does the same thing…:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | fatal: [mtk3]: FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "invocation": { "module_args": { "commands": [ "/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2", "ether3", "ether4", "ether5", "wlan2 vlan-ids=201" ], |
I eventually found this bug report, 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 *sigh*.
Here’s the working version of the task:
1 2 3 4 | - name: Add vlan config back for user vlans routeros_command: commands: - "/interface bridge vlan add bridge=bridge1 tagged=ether1 untagged=ether2,ether3,ether4,ether5,wlan2 vlan-ids={{ hostvars[inventory_hostname]['user_vlan'] }}" |
Notice that all I did was move the command itself to a new line and add the “-” before it. Now it interprets everything as a single line, ignoring the commas.
I hope you find this to be useful, and possibly save you a good chunk of time!
Thanks Greg! It was indeed very helpful and did save me a lot of time and effort!
Maaaaan…I was about to break my keyboard!