Skip to content

Salt grains

grains are used for targeting in salt they can set from the master or can be set into the minion. Based on the value of grains from the minion. We can target specific minion where a particular salt state has to be applied.

setup grains in a minion

In the minion create a file /etc/salt/grains and add the following grains which can be later used to target this minion.

roles:
  - storage
environment: production
region: kathmandu

restart the minion

Once the grains have been setup restart the minion. In an ideal situation these are populated via cloud-init or metadata from cloud providers are used to discover and target salt-minions.

sudo systemctl restart salt-minion

test using grains

sudo salt -G 'roles:storage' test.ping
The minion with the grain will return a response while others will not return a response.
sudo salt -G 'region:np-kathmandu' test.ping