-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
interface bridge vlan only works for non-existing vlans #325
Comments
Looks like I should perhaps use This gives no matches: - name: Configure vlans on bridge # noqa args[module]
community.routeros.api_find_and_modify:
path: "interface bridge vlan"
find:
bridge: "{{ item.bridge }}"
vlan-ids: "{{ item.vlan_id }}"
values:
vlan-ids: "{{ item.vlan_id }}"
bridge: "{{ item.bridge }}"
tagged: "{{ item.tagged }}"
loop: "{{ __vlans }}"
vars:
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether2 whereas this gives a match: - name: Configure vlans on bridge # noqa args[module]
community.routeros.api_find_and_modify:
path: "interface bridge vlan"
find:
bridge: "{{ item.bridge }}"
vlan-ids: 2
values:
vlan-ids: 2
bridge: "{{ item.bridge }}"
tagged: "{{ item.tagged }}"
loop: "{{ __vlans }}"
vars:
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether2 I thought perhaps it might be that the vlan ids in the loop were interpreted as a string and not int but forcing the int data type with Also, using variables does not seem to be the issue, as this works just fine: - name: Configure vlans on bridge # noqa args[module]
community.routeros.api_find_and_modify:
path: "interface bridge vlan"
find:
bridge: "{{ bridge }}"
vlan-ids: "{{ vlan_id }}"
values:
vlan-ids: "{{ vlan_id }}"
bridge: "{{ bridge }}"
tagged: "{{ tagged }}"
vars:
bridge: bridge0
vlan_id: 2
tagged: ether2 Looking at the verbose output I notice that {
"ansible_loop_var": "item",
"changed": false,
"invocation": {
"module_args": {
"allow_no_matches": true,
"ca_path": null,
"encoding": "UTF-8",
"find": {
"bridge": "bridge0",
"vlan-ids": "2"
}, But when not using a loop it is unquoted. {
"ansible_loop_var": "item",
"changed": false,
"invocation": {
"module_args": {
"allow_no_matches": true,
"ca_path": null,
"encoding": "UTF-8",
"find": {
"bridge": "bridge0",
"vlan-ids": 2
}, |
Looks like this could be the same issue with the loops: #169 |
Found a workaround for the - name: Configure vlans on bridge # noqa args[module]
community.routeros.api_find_and_modify:
path: "interface bridge vlan"
find: >-
{{
dict([
['vlan-ids', item.vlan_id],
['bridge', item.bridge]
])
}}
values:
vlan-ids: "{{ item.vlan_id }}"
bridge: "{{ item.bridge }}"
tagged: "{{ item.tagged }}"
loop: "{{ __vlans }}"
vars:
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether2 The same data type issue also affects the - name: Configure vlans on bridge # noqa args[module]
community.routeros.api_find_and_modify:
path: "interface bridge vlan"
find: >-
{{
dict([
['vlan-ids', item.vlan_id],
['bridge', item.bridge]
])
}}
values: >-
{{
dict([
['vlan-ids', item.vlan_id],
['bridge', item.bridge],
['tagged', item.tagged]
])
}}
loop: "{{ __vlans }}"
vars:
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether2 I tried few approaches, using the dict filter, using to/from json/yaml filters, creating the dict with inline jinja2, using block scalar to avoid the quotes around the variable, etc. but only I'm not sure if this issue is tied directly to the - name: "Debug vlans"
ansible.builtin.debug:
msg: { vlan-id: "{{ item.vlan_id }}" }
loop: "{{ __vlans }}"
vars:
vlan_id: 2
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether3 Incorrectly returns a string: {
"msg": {
"vlan-id": "2"
}
} - name: "Debug vlans"
ansible.builtin.debug:
msg: { vlan-id: "{{ vlan_id }}" }
loop: "{{ __vlans }}"
vars:
vlan_id: 2
__vlans:
- bridge: bridge0
vlan_id: 2
tagged: ether3 Correctly returns a int: {
"msg": {
"vlan-id": 2
}
} |
Maybe it's best to do all value comparisons by first converting both values to strings. The Python library we use to communicate with the API does some conversions: when receiving values from the API, it changes So if we do a similar value to string conversion before comparison, these problems should go away. |
Yes that would be logical, but I'm still baffled why the behavior is different between a loop and a non-loop |
That's a result of how templating works in Ansible. IMO the main problem is that it's hard (or next to impossible) to distinguish between templating and expression evaluation. I've once wrote some ideas down here: https://forum.ansible.com/t/4386 Apparently everything will get better with data tagging - assuming that ever happens. (It's now supposed to come in ansible-core 2.19, but then it was already supposed to be there in several previous versions, so 🤷 It will likely also break a lot of existing playbooks/roles if it is supposed to fix things like the above, since you can bet on that someone actually depends on the behavior that we think is wrong...) |
thanks for the workaround - was messing around at the same location and nearly went crazy |
SUMMARY
It's only possible to use the
interface bridge vlan
path with theapi_modify
module if the vlan does not already exist.It errors out if the vlan exists, both when attempting to modify the vlan but also when the config matches the existing vlan and it should not be attempting to modify it.
ISSUE TYPE
COMPONENT NAME
api_modify
interface bridge vlan
ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
STEPS TO REPRODUCE
EXPECTED RESULTS
The vlan should be modified if the
data
is different than the configuration on the device. If it's the same then it should not change the interface and give ok status.ACTUAL RESULTS
The text was updated successfully, but these errors were encountered: