SaltStack and Python- More Operations

Various operations of SaltStack with Python

This post talks about doing various operations using SaltStack and Python. If you are looking for the basics, please refer here.

A basic ls command example

import salt.client
client = salt.client.LocalClient()

and the output would be :

"{'Seshan@fractalio': 'Desktop\nDocuments\nDownloads\nMusic\nPictures\nPublic\nTemplates\nVideos\ntest'}"

What does the above code do ? The above code in the terminal is executed as

salt '*' ls

Lets look at the various components of the code.

- client is the LocalClient object.

- '\*' -- tells to target this command at all the minions. You can replace the * with things like '\*' which will target all the minions with key 20*. This is called gobbling. SaltStack also supports regular expressions.

- -- this is a salt module, that is used to run a command in the terminal and get the specified output from the client. Eg  of other modules,, state.highstate etc.

- ['ls'] is the parameter or the command that is to be passed to Other examples are, "service network restart" or any command that you execute can be a parameter.

So what happens when you want to pass a parameter to ls ?? then you can say ,


Most of the things you can accomplish by the above, but have a list of all the available modules in saltstack here :

I am sure you would have also heard about grains in SaltStack. If you want to target minions by the grains, then here is what you have to do.

client.cmd('roles:primary', '', ['ls'], expr_form='grain')

So instead of using '*', we use the grain key and value, and say explicitly that the expr_form='grain'.

All wrongs reserved !! Content unless notified is under Creative Commons by SA 4.0