Return/control aspects of the grains data
New in version 0.17.0.
Append a value to a list in the grains config file
CLI Example:
salt '*' grains.append key val
New in version 0.17.0.
Delete a grain from the grains config file
CLI Example:
salt '*' grains.delval key
New in version 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'},
}) %}
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')) %}
Parameters: |
|
---|
CLI Example:
salt '*' grains.filter_by '{Debian: Debheads rule, RedHat: I love my hat}'
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 Example:
salt '*' grains.get pkg:apache
Return one or more grains
CLI Example:
salt '*' grains.item os
salt '*' grains.item os osrelease oscodename
Sanitized CLI Example:
salt '*' grains.item host sanitize=True
Return all of the minion's grains
CLI Example:
salt '*' grains.items
Sanitized CLI Example:
salt '*' grains.items sanitize=True
Return a list of all available grains
CLI Example:
salt '*' grains.ls
New in version 0.17.0.
Remove a value from a list in the grains config file
CLI Example:
salt '*' grains.remove key val
Set a grains value in the grains config file
CLI Example:
salt '*' grains.setval key val
salt '*' grains.setval key "{'sub-key': 'val', 'sub-key2': 'val2'}"