PYTHON CLIENT API

Source: Internet
Author: User
Tags deprecated glob

Salt provides several entry points to the Python application interface, which are often referred to as the * client () API. Each client accesses different parts of the salt, whether from master or minion, each client is detailed as follows:

1. Programmatic access to salt
2. Salt can be used in CLI scripts or through the rest interface.
3. The outputter system of salt retrieves structured data from salt for JSON, or for shell-friendly text, or many other formats.
4, look at the State.event,salt event system
5. The Salt NETAPI module provides an external rest interface for accessing the salt extension

SALT ' S OPTS DICTIONARY

Some clients need access to the Salt opts dictionary, which contains the configuration of the master or minion side

Salt.config.client_config (Path, env_var= ' Salt_client_config ', Defaults=none)
Loading the configuration on the master side
Example

1 Import Salt.config 2 master_opts = salt.config.client_config ('/etc/salt/master '

Salt.config.minion_config (Path, env_var= ' Salt_minion_config ', Defaults=none, Cache_minion_id=false)
Load the configuration of the Minion side and operate on the Minion side
Example:

1 Import Salt.client 2 minion_opts = salt.config.minion_config ('/etc/salt/minion ')
SALT ' S LOADER INTERFACE

Salt Loader interface
Modules in the salt ecosystem are loaded into memory using a custom loading system. This allows the module to have conditional (OS, OS version, installed libraries, etc.) to allow salt to inject special variables, each module type should have corresponding load function

Salt.loader.minion_mods (opts, Context=none, Utils=none, Whitelist=none, Include_errors=false, Initial_load=false, Loaded_base_name=none, Notify=false, Static_modules=none, Proxy=none)
Load the execution module to return a dictionary of the execution modules applicable to the current system by evaluating the __virtual __ () function in each module.
Parameters:

OPTs (dict) A Dictionary of--salt configuration options
Context (DICT)-The salt context that exists in the module that should be generated in __context__
Utils (dict)--Utility functions which should is made available to Salt modules in __utils__. See Utils_dir in Salt.config For additional information about configuration.
Whitelist (list)--A list of modules which should be whitelisted.
Include_errors (BOOL)--Deprecated flag! Unused.
Initial_load (BOOL)--Deprecated flag! Unused.
Loaded_base_name (str)--A string marker for the loaded base name.
Notify (BOOL)--Flag indicating that a event should be fired upon completion of module loading.

Example:

1 Import Salt.config 2 Import Salt.loader 3 4 __opts__ = salt.config.minion_config ('/etc/salt/minion ') 5 __grains__ = Salt.loader.grains (__opts__) 6 __opts__[' grains '] = __grains__ 7 __salt__ = salt.loader.minion_mods (__opts__)    #获取执行模块函数的字典 8 __salt__[' test.ping '] ()

Salt.loader.raw_mod (opts, name, functions, mod= ' modules ')
Returns a single module, bypassing the __virtual__ function
Example:

1 Import Salt.config 2 Import Salt.loader 3 4 __opts__ = salt.config.minion_config ('/etc/salt/minion ') 5 testmod = Salt.loader.raw_mod (__opts__, ' test ', None) 6 testmod[' test.ping '] ()

Salt.loader.states (opts, Functions, Utils, serializers, Whitelist=none)
Back to States module
Example:

1 Import Salt.config 2 Import Salt.loader 3 4 __opts__ = salt.config.minion_config ('/etc/salt/minion ') 5 statemods = Salt.loader.states (__opts__, none, none, none)

Salt.loader.grains (opts, Force_refresh=false, Proxy=none)
Returns the value of a dynamic or static grains
Example:

1 Import Salt.config 2 Import Salt.loader 3 4 __opts__ = salt.config.minion_config ('/etc/salt/minion ') 5 __grains__ = Salt.loader.grains (__opts__) 6 print __grains__[' id ']

Salt.loader.grain_funcs (opts)
Returns the Grains function
Example:

1 Import Salt.config 2 Import Salt.loader 3 4 __opts__ = salt.config.minion_config ('/etc/salt/minion ') 5 Grainfuncs = Salt.loader.grain_funcs (__opts__)
SALT ' S CLIENT INTERFACES

Class Salt.client.LocalClient (c_path= '/etc/salt/master ', Mopts=none, Skip_perm_errors=false, Io_loop=none)
This interface serves as a tool for the master-side Salt command-line interface
Localclient is used to send a command to the salt minion to execute, and returns a result to the Salt master.
Import and use localclient must be in the same machine as the Salt master, it must be done with the same user as the Salt master, unless the External_auth has the relevant configuration.
Example:

1 Import Salt.client 2 3 local = salt.client.LocalClient () 4 local.cmd (' * ', ' test.fib ', [10])

How to use the correlation function of localclient;

CMD (TGT, Fun, arg= (), Timeout=none, expr_form= ' glob ', ret= ', jid= ', Kwarg=none, **kwargs)
The timeout value of the CMD setting is the time to wait after the last minion return result

1 local.cmd (' * ', ' cmd.run ', [' WhoAmI ']) 2 {' Jerry ': ' Root '}

With the use of parameters:
Local.cmd (' * ', ' test.arg ', [' arg1 ', ' arg2 '], kwarg={' foo ': ' Bar '})
Composite commands can be used for multiple executions in a single publication. The function names and functions parameters are provided in a separate list, but the index values must be associated, and if no arguments are required, an empty list must be used.
Example:

1 local.cmd (' * ', [  2        ' Grains.items ' , 3        ' Sys.doc ',  4         ' Cmd.run ',  5    ],  6    [  7        [ ],  8        [],  9        [' uptime '],ten     ])

Usage Description:

TGT (string or list): Select the tag that is executed minion, if it is a group, use the Expr_form option
Fun (string or List of strings): Specifies the function module to execute and can use the compound command
ARG (list or list-of-lists): Executes the parameters used by the module, and the composite command needs to execute a parameter list
Timeout: Specifies the time-out, the timeout to wait before all Minion are returned, and does not refer to the time-out of the command execution
Expr_form: Specifies the type of TGT, which can be the following value:
Glob-bash Glob Completion-default
Pcre-perl-style regular expressions
List-designated as host lists
Grain-Match grains variable at minion end
Grain_pcre-a regular-based matching grains variable at the minion end
Pillar-pillar Data Comparison
Pillar_pcre-pillar data comparison with a regex
Nodegroup-Use Nodegroup information
Range-Specify a server scope
Compound-pass a compound match string
RET: Does not understand, a bit like setting the meaning of the return value
Kwarg: Setting Parameters for function execution

Using External_auth:
For Example:local.cmd (' * ', ' test.ping ', username= ' Saltdev ', password= ' Saltdev ', eauth= ' Pam ').
Or:local.cmd (' * ', ' test.ping ', token= ' 5871821ea51754fdcea8153c1c745433 ')

How to use each function:

Cmd_async (TGT, Fun, arg= (), expr_form= ' Glob ', ret= ', jid= ', Kwarg=none, **kwargs)
Asynchronous execution command, return value for job ID, execution failure return 0

Cmd_batch (TGT, Fun, arg= (), expr_form= ' Glob ', ret= ', Kwarg=none, batch= ' 10% ', **kwargs)
Returns an iterator that executes the result

returns = Local.cmd_batch (' * ', ' state.highstate ', bat= ' 10% ')
{' 10.64.1.39 ': ' xd-asp-nginx-1064139 '}

Cmd_iter (TGT, Fun, arg= (), Timeout=none, expr_form= ' glob ', ret= ', Kwarg=none, **kwargs)
Returns an iterator that contains the status of the result
{' 10.64.1.43 ': {' Retcode ': 0, ' ret ': ' xd-asp-nginx-1064143 '}}

Cmd_iter_no_block (TGT, Fun, arg= (), Timeout=none, expr_form= ' glob ', ret= ', Kwarg=none, **kwargs)
Returns a generator with no returned minion result will be set to none

Cmd_subset (TGT, Fun, arg= (), expr_form= ' Glob ', ret= ', Kwarg=none, sub=3, Cli=false, Progress=false,
Randomly pick a target execution in the execution target.

Get_cli_returns (Jid, Minions, Timeout=none, tgt= ' * ', tgt_type= ' glob ', Verbose=false, Show_jid=false, **kwargs)
Not quite clear at the moment, it should be the result of returning a task ID, but the format is strange, there are so many \ n

Local.cmd_async (' 10.64.1.35 ', ' cmd.run ', [' ls/'])
A = Local.get_cli_returns (' 20170817145623282510 ', ' 10.64.1.35 ')

Get_event_iter_returns (Jid, Minions, Timeout=none)
Support for task interruption, returned information is better processed

Run_job (TGT, Fun, arg= (), expr_form= ' Glob ', ret= ', Timeout=none, jid= ', Kwarg=none, **kwargs)
Asynchronously sends commands to the Minion side, returning Jid and corresponding Minion, which facilitates the asynchronous sending of commands before results are collected

A = Local.get_event_iter_returns (' 20170817150543850815 ', ' bj-sn-10.64 ')
#但是返回了一些并不匹配minion的响应信息

SALT CALLER

Class Salt.client.Caller (c_path= '/etc/salt/minion ', Mopts=none)
Caller on the salt-minion side and the Salt-call command line interface, adding the same cmd method as the other salt clients, the import and use must be performed on the same salt minion.
Example:

1 Import Salt.client 2 caller = Salt.client.Caller () 3 caller.cmd (' test.ping ') 4 running Salt-call--local is equivalent to setting the File_client value in the Minion-side configuration file, which is remote by default.

CMD (fun, *args, **kwargs)
Calling an execution module needs to provide some parameters
Example:

1 caller.cmd (' Test.arg ', ' Foo ', ' Bar ', baz= ' baz ') 2 3 caller.cmd (' event.send ', ' myco/myevent/something ',4     data={' foo ': ' foo '}, with_env=[' Git_commit '], with_grains=true)
Runnerclient

Class Salt.runner.RunnerClient (opts)
The command-line interface that is used to use Salt-run on the Salt master side.
It executes the execution module running on the Salt Master host, and the Runnerclient import and use is required on the Salt Master machine.

Async (fun, Low, user= ' UNKNOWN ')
Executes a function in a multi-process and returns an event token for monitoring return

CMD (fun, Arg=none, Pub_data=none, Kwarg=none)
Execute a function
Example:

1 >>> opts = salt.config.master_config ('/etc/salt/master ')2 >>> runner = salt.runner.RunnerClient (opts)3 >>> runner.cmd (' jobs.list_jobs ', [])4 {5 ' 20131219215650131543 ': {6 ' Arguments ': [+],7 ' Function ': ' Test.sleep ',8 ' StartTime ': ', Dec 21:56:50.131543 ',9 ' Target ': ' * ',Ten ' target-type ': ' Glob ', One ' User ': ' Saltdev ' A     }, - ' 20131219215921857715 ': { - ' Arguments ': [+], the ' Function ': ' Test.sleep ', - ' StartTime ': ', Dec 21:59:21.857715 ', - ' Target ': ' * ', - ' target-type ': ' Glob ', + ' User ': ' Saltdev ' -     }, +}

Cmd_async (Low)
Execute a function in an asynchronous manner
Example:

1 Runner.eauth_async ({2    ' fun ': ' Jobs.list_jobs ',3    ' username ': ' Saltdev ',4    ' password ': ' Saltdev ',5    ' eauth ': ' Pam ', 6 })

Cmd_sync (Low, Timeout=none)
Executing functions in a synchronous manner
Example:

1 Runner.eauth_sync ({2    ' fun ': ' Jobs.list_jobs ',3    ' username ': ' Saltdev ',4    ' password ': ' Saltdev ',5    ' eauth ': ' Pam ',6  })
Wheelclient

Class Salt.wheel.WheelClient (Opts=none)
Salt ' s Wheel Module interface
Examples of Use:

1 Import Salt.config 2 Import Salt.wheel 3 opts = salt.config.master_config ('/etc/salt/master ') 4 wheel = salt.wheel.WheelClient (opts)

Async (fun, Low, user= ' UNKNOWN ')

CMD (fun, Arg=none, Pub_data=none, Kwarg=none)
Example:

1 >>> wheel.cmd (' Key.finger ', [' Jerry ']) 2 {' Minions ': {' Jerry ': ' 5D:F6:79:43:5E:D4:42:3F:57:B8:45:A8:7E:A4:6E:CA '}}

Cmd_async (Low)
Asynchronous execution functions
Example:

1 >>> wheel.cmd_async ({2    ' fun ': ' Key.finger ',3    ' match ': ' Jerry ',4    ' eauth ': ' Auto ',5    ' username ': ' Saltdev ',6      ' password ': ' Saltdev ',7})8 {' Jid ': ' 20131219224744416681 ' , ' tag ': ' salt/wheel/20131219224744416681 '}

Cmd_sync (Low, Timeout=none)
Synchronous Execute function command

1 >>> wheel.cmd_sync ({2' fun ': ' Key.finger ',3' match ': ' Jerry ', 4 ' eauth ': ' Auto ', 5 ' username ': ' Saltdev ', 6 ' password ': ' Saltdev ', 7 })8 {' Minions ': {' Jerry ': ' 5D:F6:79:43:5E:D4:42:3F:57:B8:45:A8:7E:A4:6E:CA '}}
Sshclient

Class Salt.client.ssh.client.SSHClient (c_path= '/etc/salt/master ', Mopts=none)
Creates a client object for execution through the Salt-ssh backend

CMD (TGT, Fun, arg= (), Timeout=none, expr_form= ' Glob ', Kwarg=none, **kwargs)
Executes a single command via the salt SSH subsystem and returns all results immediately

Cmd_iter (TGT, Fun, arg= (), Timeout=none, expr_form= ' glob ', ret= ', Kwarg=none, **kwargs)
Executes a single command through the salt SSH subsystem and returns the result as a generator

PYTHON CLIENT API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.