Allow for options containing commas

* instead of the boolean forcelist use force=option or force=list
This commit is contained in:
Tobias Klaus 2019-12-01 01:03:29 +01:00
parent 88fcad3e29
commit cdc207061b
2 changed files with 13 additions and 11 deletions

View file

@ -172,8 +172,8 @@ parameters:
A special warning about types: UCI has two types for values internally: `list` A special warning about types: UCI has two types for values internally: `list`
and `option`. The module tries to infer the type by looking for `,` in the and `option`. The module tries to infer the type by looking for `,` in the
input. If you need to force a singleentry list, please be sure to set the input. If you need to force a single entry list, please be sure to set `force=list`.
`forcelist=yes` parameter. On the other hand setting `force=option` allows setting options containing commas.
| parameter | required | default | choices | comments | | parameter | required | default | choices | comments |
|-----------|----------|---------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------|----------|---------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@ -181,7 +181,7 @@ input. If you need to force a singleentry list, please be sure to set the
| value | no | | | For set: value to set the property to | | value | no | | | For set: value to set the property to |
| match | no | | | When present in a set or get op: properties a section must have to be modified or returned | | match | no | | | When present in a set or get op: properties a section must have to be modified or returned |
| values | no | | | For set with match: values to set on matching section | | values | no | | | For set with match: values to set on matching section |
| forcelist | no | false | Boolean | The module trys to guess the uci config type (list or string) from the supplied value via the existance of `,` in the input. Single entry lists require `forcelist=yes` to be recognized correctly | | force | no | | list, option | The module tries to guess the uci config type (list or option) from the supplied value via the existance of `,` in the input. Single entry lists require `force=list` to be recognized correctly to be able to set options containing commas `force=option` is needed. |
| state | no | present | present, absent, set, unset | State of the property | | state | no | present | present, absent, set, unset | State of the property |
| op | no | | configs, commit, revert, get| If specified, instead of enforcing a value, either list the available configurations, execute a commit/revert operation, or query properties. | | op | no | | configs, commit, revert, get| If specified, instead of enforcing a value, either list the available configurations, execute a commit/revert operation, or query properties. |
| reload | no | | Boolean | Whether to reload the configuration from disk before executing. _Aliases: reload_configs, reload-configs_ | | reload | no | | Boolean | Whether to reload the configuration from disk before executing. _Aliases: reload_configs, reload-configs_ |
@ -218,11 +218,11 @@ uci: name=uhttpd.test state="absent" autocommit=true'
uci: op=configs uci: op=configs
``` ```
An more complex example showing the usage of forcelist: A more complex example showing the usage of force:
```yaml ```yaml
- name: Securing uhttpd - Disable listening on wan - name: Securing uhttpd - Disable listening on wan
uci: name={{ item.key }} value={{ uci.state.network.lan.ipaddr }}:{{ item.port }} forcelist=true autocommit=false uci: name={{ item.key }} value={{ uci.state.network.lan.ipaddr }}:{{ item.port }} force=list autocommit=false
with_items: with_items:
- { key: 'uhttpd.main.listen_http', port: '80' } - { key: 'uhttpd.main.listen_http', port: '80' }
- { key: 'uhttpd.main.listen_https', port: '443' } - { key: 'uhttpd.main.listen_https', port: '443' }

View file

@ -183,10 +183,12 @@ function set_value(module)
local conf, sec = check_config(module, conn, path["config"], path["section"]) local conf, sec = check_config(module, conn, path["config"], path["section"])
local target = p["value"] local target = p["value"]
local forcelist = p["forcelist"] local force = p["force"]
if type(target) == "table" and #target == 1 and not forcelist then if (force == "option" and type(target) == "table") or type(target) == "table" and #target == 1 then
target = target[1] target = table.concat(target, ',')
elseif force == "list" and (type(target) == "string" or type(target) == "number" ) then
target = { target }
end end
local values = {} local values = {}
@ -347,8 +349,8 @@ function check_parameters(module)
module:fail_json({msg="When deleting options, no value can be set"}) module:fail_json({msg="When deleting options, no value can be set"})
end end
if nil ~= p["forcelist"] and ("unset" == p["state"] or "absent" == p["state"]) then if nil ~= p["force"] and ("unset" == p["state"] or "absent" == p["state"]) then
module:fail_json({msg="'forcelist' only applies to set operations"}) module:fail_json({msg="'force' only applies to set operations"})
end end
end end
@ -362,7 +364,7 @@ function main(arg)
op = { choices={"configs", "commit", "revert", "get"} }, op = { choices={"configs", "commit", "revert", "get"} },
reload = { aliases = {"reload_configs", "reload-configs"}, type='bool'}, reload = { aliases = {"reload_configs", "reload-configs"}, type='bool'},
autocommit = { default=true, type="bool" }, autocommit = { default=true, type="bool" },
forcelist = { default=false, type="bool" }, force = { choices={"list", "option" } },
type = { aliases = {"section-type"}, type="str" }, type = { aliases = {"section-type"}, type="str" },
socket = { type="path" }, socket = { type="path" },
timeout = { type="int"}, timeout = { type="int"},