.. versionadded:: 0.17.0 Append a value to a list in the grains config file. If the grain doesn't exist, the grain key is added and the value is appended to the new grain as a list item. key The grain key to be appended to val The value to append to the gr
(key, val, convert=False, delimiter=DEFAULT_TARGET_DELIM)
| 326 | |
| 327 | |
| 328 | def append(key, val, convert=False, delimiter=DEFAULT_TARGET_DELIM): |
| 329 | """ |
| 330 | .. versionadded:: 0.17.0 |
| 331 | |
| 332 | Append a value to a list in the grains config file. If the grain doesn't |
| 333 | exist, the grain key is added and the value is appended to the new grain |
| 334 | as a list item. |
| 335 | |
| 336 | key |
| 337 | The grain key to be appended to |
| 338 | |
| 339 | val |
| 340 | The value to append to the grain key |
| 341 | |
| 342 | convert |
| 343 | If convert is True, convert non-list contents into a list. |
| 344 | If convert is False and the grain contains non-list contents, an error |
| 345 | is given. Defaults to False. |
| 346 | |
| 347 | delimiter |
| 348 | The key can be a nested dict key. Use this parameter to |
| 349 | specify the delimiter you use, instead of the default ``:``. |
| 350 | You can now append values to a list in nested dictionary grains. If the |
| 351 | list doesn't exist at this level, it will be created. |
| 352 | |
| 353 | .. versionadded:: 2014.7.6 |
| 354 | |
| 355 | CLI Example: |
| 356 | |
| 357 | .. code-block:: bash |
| 358 | |
| 359 | salt '*' grains.append key val |
| 360 | """ |
| 361 | grains = get(key, [], delimiter) |
| 362 | if convert: |
| 363 | if not isinstance(grains, list): |
| 364 | grains = [] if grains is None else [grains] |
| 365 | if not isinstance(grains, list): |
| 366 | return f"The key {key} is not a valid list" |
| 367 | if val in grains: |
| 368 | return f"The val {val} was already in the list {key}" |
| 369 | if isinstance(val, list): |
| 370 | for item in val: |
| 371 | grains.append(item) |
| 372 | else: |
| 373 | grains.append(val) |
| 374 | |
| 375 | while delimiter in key: |
| 376 | key, rest = key.rsplit(delimiter, 1) |
| 377 | _grain = get(key, _infinitedict(), delimiter) |
| 378 | if isinstance(_grain, dict): |
| 379 | _grain.update({rest: grains}) |
| 380 | grains = _grain |
| 381 | |
| 382 | return setval(key, grains) |
| 383 | |
| 384 | |
| 385 | def remove(key, val, delimiter=DEFAULT_TARGET_DELIM): |
nothing calls this directly
no test coverage detected