Invoke a pre-defined Python function with arguments specified in the state declaration. This function is mainly used by the :mod:`salt.renderers.pydsl` renderer. In addition, the ``stateful`` argument has no effects here. The return value of the invoked function will be interp
(
name,
func,
args=(),
kws=None,
output_loglevel="debug",
hide_output=False,
use_vt=False,
**kwargs,
)
| 1251 | |
| 1252 | |
| 1253 | def call( |
| 1254 | name, |
| 1255 | func, |
| 1256 | args=(), |
| 1257 | kws=None, |
| 1258 | output_loglevel="debug", |
| 1259 | hide_output=False, |
| 1260 | use_vt=False, |
| 1261 | **kwargs, |
| 1262 | ): |
| 1263 | """ |
| 1264 | Invoke a pre-defined Python function with arguments specified in the state |
| 1265 | declaration. This function is mainly used by the |
| 1266 | :mod:`salt.renderers.pydsl` renderer. |
| 1267 | |
| 1268 | In addition, the ``stateful`` argument has no effects here. |
| 1269 | |
| 1270 | The return value of the invoked function will be interpreted as follows. |
| 1271 | |
| 1272 | If it's a dictionary then it will be passed through to the state system, |
| 1273 | which expects it to have the usual structure returned by any salt state |
| 1274 | function. |
| 1275 | |
| 1276 | Otherwise, the return value (denoted as ``result`` in the code below) is |
| 1277 | expected to be a JSON serializable object, and this dictionary is returned: |
| 1278 | |
| 1279 | .. code-block:: python |
| 1280 | |
| 1281 | { |
| 1282 | 'name': name |
| 1283 | 'changes': {'retval': result}, |
| 1284 | 'result': True if result is None else bool(result), |
| 1285 | 'comment': result if isinstance(result, str) else '' |
| 1286 | } |
| 1287 | """ |
| 1288 | ret = {"name": name, "changes": {}, "result": False, "comment": ""} |
| 1289 | |
| 1290 | cmd_kwargs = { |
| 1291 | "cwd": kwargs.get("cwd"), |
| 1292 | "runas": kwargs.get("user"), |
| 1293 | "shell": kwargs.get("shell") or __grains__["shell"], |
| 1294 | "env": kwargs.get("env"), |
| 1295 | "use_vt": use_vt, |
| 1296 | "output_loglevel": output_loglevel, |
| 1297 | "hide_output": hide_output, |
| 1298 | } |
| 1299 | |
| 1300 | if not kws: |
| 1301 | kws = {} |
| 1302 | result = func(*args, **kws) |
| 1303 | if isinstance(result, dict): |
| 1304 | ret.update(result) |
| 1305 | return ret |
| 1306 | else: |
| 1307 | # result must be JSON serializable else we get an error |
| 1308 | ret["changes"] = {"retval": result} |
| 1309 | ret["result"] = True if result is None else bool(result) |
| 1310 | if isinstance(result, str): |