Call an Ansible module by invoking it. :param module: the name of the module. :param args: Arguments to pass to the module :param kwargs: keywords to pass to the module CLI Example: .. code-block:: bash salt * ansible.call ping data=foobar
(module, *args, **kwargs)
| 212 | |
| 213 | |
| 214 | def call(module, *args, **kwargs): |
| 215 | """ |
| 216 | Call an Ansible module by invoking it. |
| 217 | |
| 218 | :param module: the name of the module. |
| 219 | :param args: Arguments to pass to the module |
| 220 | :param kwargs: keywords to pass to the module |
| 221 | |
| 222 | CLI Example: |
| 223 | |
| 224 | .. code-block:: bash |
| 225 | |
| 226 | salt * ansible.call ping data=foobar |
| 227 | """ |
| 228 | |
| 229 | module_args = [] |
| 230 | for arg in args: |
| 231 | module_args.append(salt.utils.json.dumps(arg)) |
| 232 | |
| 233 | _kwargs = {} |
| 234 | for _kw in kwargs.get("__pub_arg", []): |
| 235 | if isinstance(_kw, dict): |
| 236 | _kwargs = _kw |
| 237 | break |
| 238 | else: |
| 239 | _kwargs = {k: v for (k, v) in kwargs.items() if not k.startswith("__pub")} |
| 240 | |
| 241 | for key, value in _kwargs.items(): |
| 242 | module_args.append(f"{key}={salt.utils.json.dumps(value)}") |
| 243 | |
| 244 | with NamedTemporaryFile(mode="w") as inventory: |
| 245 | |
| 246 | ansible_binary_path = salt.utils.path.which("ansible") |
| 247 | log.debug("Calling ansible module %r", module) |
| 248 | try: |
| 249 | env = os.environ.copy() |
| 250 | env["ANSIBLE_DEPRECATION_WARNINGS"] = "0" |
| 251 | |
| 252 | proc_exc = subprocess.run( |
| 253 | [ |
| 254 | ansible_binary_path, |
| 255 | "localhost", |
| 256 | "--limit", |
| 257 | "127.0.0.1", |
| 258 | "-m", |
| 259 | module, |
| 260 | "-a", |
| 261 | " ".join(module_args), |
| 262 | "-i", |
| 263 | inventory.name, |
| 264 | ], |
| 265 | capture_output=True, |
| 266 | timeout=__opts__.get("ansible_timeout", DEFAULT_TIMEOUT), |
| 267 | text=True, |
| 268 | check=True, |
| 269 | shell=False, |
| 270 | env=env, |
| 271 | ) |