http_call

This task plugin is to interact with devices over HTTP and is capable of calling any method of Python built-in requests library.

http_call sample usage

Sample code to run http_call task:

from nornir_salt import http_call
from nornir import InitNornir

nr = InitNornir(config_file="nornir_config.yaml")

res = nr.run(
    task=http_call,
    method="get",
    url="https://google.com",
)

http_call returns

Returns requests result string in XML or JSON format.

http_call reference

nornir_salt.plugins.tasks.http_call.http_call(task: nornir.core.task.Task, method: str, url: Optional[str] = None, **kwargs) nornir.core.task.Result

Task function to call one of the supported requests library methods.

Parameters
  • method – (str) requests method to call

  • url – (str) relative to base_url or absolute URL to send request to

  • kwargs – (dict) any **kwargs to use with requests method call

Returns

(str) attempt to return JSON formatted string if json string pattern found in response’s Content-type header, returns response text otherwise

http_call follows these rules to form URL to send request to:

  1. Use url attribute if provided and it absolute - starts with http:// or https://

  2. If url attribute provided but is relative - does not starts with http:// or https:// - it is merged with inventory base_url using formatter {base_url}/{url} if base_url is absolute - starts with http:// or https://

  3. If url attribute provided and base_url listed in inventory and they are both relative - does not startswith http:// or https:// - target URL formed using formatter: {transport}://{hostname}:{port}/{base_url}/{url}

  4. If url attribute provided and is relative and no base_url defined in inventory extras sections, http_call uses this formatter {transport}://{hostname}:{port}/{url} to form final URL if transport parameter defined in inventory extras sections

  5. If no url attribute provided use base_url to send the request if base_url defined in inventory extras sections and is absolute - starts with http:// or https://

  6. If no url attribute provided and no base_url defined in inventory

    extras sections use this formatter {transport}://{hostname}:{port}/ if transport parameter defined in inventory extras sections

  7. Raise error, unable to form URL

Default headers added to HTTP request:

'Content-Type': 'application/yang-data+json',
'Accept': 'application/yang-data+json'

If no auth attribute provided in task kwargs, host’s username and password inventory parameters used to form auth tuple.