Return/control aspects of the grains data
salt.modules.grains.
append
(key, val, convert=False, delimiter=':')¶0.17.0 新版功能.
Append a value to a list in the grains config file. If the grain doesn't exist, the grain key is added and the value is appended to the new grain as a list item.
参数: |
|
---|
CLI 范例:
salt '*' grains.append key val
salt.modules.grains.
delval
(key, destructive=False)¶0.17.0 新版功能.
从grains的配置文件中删除一个grain
参数: | destructive -- Delete the key, too. Defaults to False. |
---|
CLI 范例:
salt '*' grains.delval key
salt.modules.grains.
filter_by
(lookup_dict, grain='os_family', merge=None, default='default', base=None)¶0.17.0 新版功能.
Look up the given grain in a given dictionary for the current OS and return the result
Although this may occasionally be useful at the CLI, the primary intent of this function is for use in Jinja to make short work of creating lookup tables for OS-specific data. For example:
{% set apache = salt['grains.filter_by']({
'Debian': {'pkg': 'apache2', 'srv': 'apache2'},
'RedHat': {'pkg': 'httpd', 'srv': 'httpd'},
}, default='Debian') %}
myapache:
pkg.installed:
- name: {{ apache.pkg }}
service.running:
- name: {{ apache.srv }}
Values in the lookup table may be overridden by values in Pillar. An example Pillar to override values in the example above could be as follows:
apache:
lookup:
pkg: apache_13
srv: apache
The call to filter_by()
would be modified as follows to reference those
Pillar values:
{% set apache = salt['grains.filter_by']({
...
}, merge=salt['pillar.get']('apache:lookup')) %}
参数: |
|
---|
CLI 范例:
salt '*' grains.filter_by '{Debian: Debheads rule, RedHat: I love my hat}'
# this one will render {D: {E: I, G: H}, J: K}
salt '*' grains.filter_by '{A: B, C: {D: {E: F,G: H}}}' 'xxx' '{D: {E: I},J: K}' 'C'
# next one renders {A: {B: G}, D: J}
salt '*' grains.filter_by '{default: {A: {B: C}, D: E}, F: {A: {B: G}}, H: {D: I}}' 'xxx' '{D: J}' 'F' 'default'
# next same as above when default='H' instead of 'F' renders {A: {B: C}, D: J}
salt.modules.grains.
get
(key, default='', delimiter=':', ordered=True)¶Attempt to retrieve the named value from grains, if the named value is not available return the passed default. The default return is an empty string.
The value can also represent a value in a nested dict using a ":" delimiter for the dict. This means that if a dict in grains looks like this:
{'pkg': {'apache': 'httpd'}}
To retrieve the value associated with the apache key in the pkg dict this key can be passed:
pkg:apache
参数: |
|
---|
CLI 范例:
salt '*' grains.get pkg:apache
salt.modules.grains.
get_or_set_hash
(name, length=8, chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')¶Perform a one-time generation of a hash and write it to the local grains. If that grain has already been set return the value instead.
This is useful for generating passwords or keys that are specific to a single minion that don't need to be stored somewhere centrally.
State Example:
some_mysql_user:
mysql_user:
- present
- host: localhost
- password: {{ salt['grains.get_or_set_hash']('mysql:some_mysql_user') }}
CLI 范例:
salt '*' grains.get_or_set_hash 'django:SECRET_KEY' 50
警告
This function could return strings which may contain characters which are reserved
as directives by the YAML parser, such as strings beginning with %
. To avoid
issues when using the output of this function in an SLS file containing YAML+Jinja,
surround the call with single quotes.
salt.modules.grains.
has_value
(key)¶Determine whether a named value exists in the grains dictionary.
Given a grains dictionary that contains the following structure:
{'pkg': {'apache': 'httpd'}}
One would determine if the apache key in the pkg dict exists by:
pkg:apache
CLI 范例:
salt '*' grains.has_value pkg:apache
salt.modules.grains.
item
(*args, **kwargs)¶Return one or more grains
CLI 范例:
salt '*' grains.item os
salt '*' grains.item os osrelease oscodename
Sanitized CLI Example:
salt '*' grains.item host sanitize=True
salt.modules.grains.
items
(sanitize=False)¶Return all of the minion's grains
CLI 范例:
salt '*' grains.items
Sanitized CLI Example:
salt '*' grains.items sanitize=True
salt.modules.grains.
ls
()¶Return a list of all available grains
CLI 范例:
salt '*' grains.ls
salt.modules.grains.
remove
(key, val, delimiter=':')¶0.17.0 新版功能.
Remove a value from a list in the grains config file
参数: | delimiter -- The key can be a nested dict key. Use this parameter to
specify the delimiter you use, instead of the default 2016.3.0 新版功能. |
---|
CLI 范例:
salt '*' grains.remove key val
salt.modules.grains.
set
(key, val='', force=False, destructive=False, delimiter=':')¶Set a key to an arbitrary value. It is used like setval but works with nested keys.
This function is conservative. It will only overwrite an entry if
its value and the given one are not a list or a dict. The force
parameter is used to allow overwriting in all cases.
2015.8.0 新版功能.
参数: |
|
---|
CLI 范例:
salt '*' grains.set 'apps:myApp:port' 2209
salt '*' grains.set 'apps:myApp' '{port: 2209}'
salt.modules.grains.
setval
(key, val, destructive=False)¶Set a grains value in the grains config file
参数: | Destructive -- If an operation results in a key being removed, delete the key, too. Defaults to False. |
---|
CLI 范例:
salt '*' grains.setval key val
salt '*' grains.setval key "{'sub-key': 'val', 'sub-key2': 'val2'}"
salt.modules.grains.
setvals
(grains, destructive=False)¶Set new grains values in the grains config file
参数: | Destructive -- If an operation results in a key being removed, delete the key, too. Defaults to False. |
---|
CLI 范例:
salt '*' grains.setvals "{'key1': 'val1', 'key2': 'val2'}"