Using grains with SALT

From Jwiki
Revision as of 18:12, 16 July 2025 by Gyurci08 (talk | contribs) (Created page with "Category:SALT == 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: <syntaxhighlight lang="bash"> salt '*ctr-1*' grains.setval roles '[ "primary-controller", "promtail-node" ]' salt '*ctr-2*' grains.setval roles '[ "controller", "promtail-node" ]' salt '*wkr-*' grains.setval...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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.

5. Set Grains Manually on the Minion

Alternatively, set static grains in /etc/salt/grains in YAML:

roles:
  - worker
  - promtail-node
location: budapest

Apply changes by restarting the salt-minion:

systemctl restart salt-minion

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