Using grains with SALT

From Jwiki


Using Grains with Salt

This guide demonstrates how to use grains—static, easily accessible bits of information about minions—in Salt.

1. Set Custom Grains

Assign custom grains to your minions for targeting and configuration:

salt '*ctr-1*' grains.setval roles '[ "primary-controller", "promtail-node" ]'
salt '*ctr-2*' grains.setval roles '[ "controller", "promtail-node" ]'
salt '*wkr-*'  grains.setval roles '[ "worker", "promtail-node" ]'
  • The first argument specifies the minion targeting pattern (e.g., *ctr-1*, *wkr-*).
  • grains.setval assigns a value to a custom grain (here, roles).
  • Custom grains are stored locally on the minion and persist across reboots.

2. View Grains

Confirm grain values:

salt '*' grains.item roles
salt 'minion-id' grains.items

3. Target Using Grains

Use grains to target sets of minions:

salt -G 'roles:worker' test.ping

4. Modify or Remove Custom Grains

To remove a custom grain:

salt 'minion-id' grains.delval roles

To change a grain value, simply set it again with grains.setval.


By leveraging grains, you improve targeting, organization, and automation across your SaltStack infrastructure.