SaltStack – Python API


SaltStack – Python API


”;


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.client_config(''/etc/salt/master'')

Here, the client_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.

Client Module

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

The most important python classes are as follows −

  • salt.client.LocalClient
  • salt.client.Caller
  • salt.client.ssh.client.SSHClient

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

LocalClient

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

import salt.client

local = salt.client.LocalClient()
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.client
caller = salt.client.Caller()
caller.cmd(''test.ping'')

It will produce the following output

True

SSHClient

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

import salt.client.ssh.client
ssh = salt.client.ssh.client.SSHClient()
ssh.cmd(''*'', ''test.ping'')

It will produce the following output

{''minion1'': True, ''minion2'': True }

CloudClient

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

client = salt.cloud.CloudClient(path = ''/etc/salt/cloud'')

Cloud module provides functions to create VMs (create), to destroy VMs (destroy), list images provided by a cloud provider (list_images), list locations of a cloud provider (list_locations), list machine sizes of a cloud provider (list_sizes), etc.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *