Two development of Python Saltstack (2)

Source: Internet
Author: User
Tags saltstack

Review the previous section:

Grains and pillar are all definitions of their properties.

Grains defined on the Minion side (the Minion must be restarted to take effect after the definition)

Pillar defined on the master side (takes effect without rebooting)

Saltstack's API

There are two ways of Salt-api:

The first: It is the form of the function, Renfrew defined function, we can directly call, directly write Python code call function or class can be.

The second: The form is SALT-API with the encapsulated HTTP protocol, we need to start a server.



Log in to view documents:


The contents of the document are as follows:


Install Salt-api:

Yum Install–y Salt-api


1. Load the master configuration file

Import salt.configmaster_opts = Salt.config.client_config ('/etc/salt/master ') print (' master_opts ')


Master side, if you want to view the parameter properties of the configuration file:


2. Load the Minion configuration file

Import salt.configminion_opts = Salt.config.minion_config ('/etc/salt/minion ') print (' minion_opts ')


Minion side, want to see the parameters in the configuration file properties:


3. Perform various modules on master:

>>> Import Salt.client>>> local = salt.client.LocalClient ('/etc/salt/minion ') >>> Local.cmd (' * ', "test.ping")

Return:

{' 192.168.48.129 ': True}


Execute command:

>> local.cmd (' * ', ' cmd.run ', ' W ')

"Returned is a dictionary form that is easy to post-process data with"

{' 192.168.48.129 ': ' 12:17:38 up 5:58, 1 user, load average:0.00, 0.01, 0.05\nuser TTY from [Emai L protected] IDLE jcpu PCPU what\nroot pts/0 192.168.48.1 11:14 2:50 0.89s 0.89s python '}


If you want to execute more than one module at a time:

One way:

>>> local.cmd (' * ', [' test.ping ', ' Cmd.run '], [[], [' WhoAmI ']])

Results:

{' 192.168.48.129 ': {' test.ping ': True, ' cmd.run ': ' Root '}

"Test.ping corresponds to [] (empty list), Cmd.run corresponds to WhoAmI command"

Another way: (not recommended for such use, it is better to tune two times, so logic is not good)

>>> local.cmd (' * ', [' test.ping ', ' Cmd.run '], [[], [' W; Df-h ']])


Custom modules:

The module directory must be created:

Mkdir-p/srv/salt/_modulescd/srv/salt/_modules

To create a jd.py file:

Vim jd.py

#!/usr/bin/python#coding:utf-8def Hello (name): return {"name": Name}

After writing all the module, remember to sync:

Salt ' * ' Saltutil.sync_all #同步所有

Or

Salt ' * ' saltutil.sync_modules #只同步modules



Execute command to get results:

# salt ' * ' Jd.hello ajing

Result output,


In Python this gets:



Two development of Python Saltstack (2)

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.