Using grains with SALT: Difference between revisions

From Jwiki
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..."
 
No edit summary
Line 45: Line 45:


To change a grain value, simply set it again with <code>grains.setval</code>.
To change a grain value, simply set it again with <code>grains.setval</code>.
=== 5. Set Grains Manually on the Minion ===
Alternatively, set static grains in <code>/etc/salt/grains</code> in YAML:
<syntaxhighlight lang="yaml">
roles:
  - worker
  - promtail-node
location: budapest
</syntaxhighlight>
Apply changes by restarting the salt-minion:
<syntaxhighlight lang="bash">
systemctl restart salt-minion
</syntaxhighlight>


----
----


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

Revision as of 18:12, 16 July 2025


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.