English 中文(简体)
SaltStack - Python API
  • 时间:2024-09-17

SaltStack - Python API


Previous Page Next Page  

Salt provides programmatic access to all of its commands. Salt provides different modules for every section of the Salt system. Let us learn the basics of the python API and about how to run the basic salt commands in this chapter.

Configuration

The salt.config module is used to access Salt configuration details.

import salt.config
opts = salt.config.cpent_config( /etc/salt/master )

Here, the cpent_config reads the salt configuration file and returns the configuration details as dictionary.

Loader

The salt.loader module is used to load each modules in Salt such as grains, minions, etc.

import salt.loader
opts = salt.config.minion_config( /etc/salt/minion )
grains = salt.loader.grains(opts)

Here, grains reads the details of the grains in the Salt system and returns it.

Cpent Module

The salt.cpent module is used to execute salt, salt-call and the salt-SSH commands programmatically.

The most important python classes are as follows −

    salt.cpent.LocalCpent

    salt.cpent.Caller

    salt.cpent.ssh.cpent.SSHCpent

The main function provided by most of the cpent module is cmd. This function wraps the CLI options and executes it, which is similar to the command pne and returns the results as python data structures.

LocalCpent

The LocalCpent is used to send commands from the master to the salt minions and return the results to the master.

import salt.cpent

local = salt.cpent.LocalCpent()
local.cmd( * ,  test.ping )

It will produce the following output

{ minion1 : True,  minion2 : True }

Caller

The Caller is used to run salt-call programmatically and return the results.

import salt.cpent
caller = salt.cpent.Caller()
caller.cmd( test.ping )

It will produce the following output

True

SSHCpent

The SSHCient is used to run the salt-ssh programmatically and return the results.

import salt.cpent.ssh.cpent
ssh = salt.cpent.ssh.cpent.SSHCpent()
ssh.cmd( * ,  test.ping )

It will produce the following output

{ minion1 : True,  minion2 : True }

CloudCpent

The salt.cloud module is used to execute the salt-cloud commands programmatically.

cpent = salt.cloud.CloudCpent(path =  /etc/salt/cloud )

Cloud module provides functions to create VMs (create), to destroy VMs (destroy), pst images provided by a cloud provider (pst_images), pst locations of a cloud provider (pst_locations), pst machine sizes of a cloud provider (pst_sizes), etc.

Advertisements