Previously mentioned writing State.sls files using the Python language
The Saltstack system supports many ways to extend it, and renderer is one of them.
The default Saltstack renderers is Yaml+jinja, which can be learned through the obstetric master file that the current renderer
Currently Saltstack supports many kinds of renderer, because Yaml Jinja Python has a wide range of three renderer applications
The first time we wrote the state was using Yaml Jinja, which we started by explaining how to use the Python language to define the state file
Cat/src/salt/test.sls
#!py
def run ():
example={}
example['/tmp/test ']={
' File.managed ': [
{' source ': ' Salt://test '},
{' mode ': ' 644 '},
{' user ': ' Root '},
{' template ': ' Jinja '},
{' Group ': ' Root '},
{' context ': {
' A ': __grains__[' OS '],
' B ': __pillar__[' a '],
},
},
],
}
return example
The first line of the file here is the specified renderer, which we use in the Python language
The purpose of the State.sls file is to use the Jinja template to synchronize/tmp/test files
Define a run function in the Python language, and the function will eventually return a dict.
Read and print through the Python syntax, and you'll soon be familiar with
Cat/srv/salt/one.sls
/tmp/foo.conf:
File.managed:
-Source:salt://foo.conf
-User:root
-Group:root
-mode:644
-Backup:minion
>>> Import Yaml,json
>>> with open (' One.sls ', ' R ') as F:
... print Yaml.safe_load (f)
...
{'/tmp/foo.conf ': {' file.managed ': [{' Source ': ' salt://foo.conf '}, {' User ': ' root '}, {' Group ': ' Root '}, {' Mode ': 644}, { ' Backup ': ' Minion '}}}
>>> with open (' One.sls ', ' R ') as F:
... print json.dumps (Yaml.safe_load (f), indent=4)
...
{
"/tmp/foo.conf": {
"File.managed": [
{
"Source": "Salt://foo.conf"
},
{
"User": "Root"
},
{
"Group": "Root"
},
{
"Mode": 644
},
{
"Backup": "Minion"
}
]
}
}
Through the above two print, I believe you can understand this output and we above that in the Python language defined by the state. The state written in Python is actually the state we wrote with Yaml, using the Python Yaml module as the result of the load. About calling Pillar Grains in the state, we can use the Python language to directly refer to the two Python methods of __grains____pillar__. and using Yaml to write the state inside the introduction of Jinja to tune grains[' key ']pillar[' key ' is not the same. Let's execute the state of the Python language.
Salt ' minion1 ' State.sls one
Minion1:
----------
ID:/tmp/foo.conf
Function:file.managed
Result:true
Comment:file/tmp/foo.conf is in the correct state
started:11:54:37.123594
duration:15.802 ms
Changes:
Summary
------------
Succeeded:1
failed:0
------------
Total states Run:1
To view Minion files after synchronization
Then look at the pillar value defined here on the master side.
Cat Top.sls
Base
‘*‘:
-Packages
-Services
[email protected] pillar]# cat Packages.sls
Zabbix
Package-name:zabbix
version:2.2.4
Writing state with Pyhton is exactly the same as our renderer authoring function using the default Yaml jinja. You can choose render on the basis of your favorite. Of course, Saltstack also supports other renderer, and Saltstack also supports custom renderer, which we'll cover later in the chapter.
This article from "Eight Miles" blog, declined reprint!
Saltstack Study Notes 11-renderer