Grains: minion facts
Static data for targeting and logic.
Grains are static facts about a minion, gathered by the minion itself: OS and version, CPU and memory, network, virtualization, and any custom grains you set (commonly a role or environment). They are the Salt equivalent of Puppet facts or Ansible facts, used two ways — to target commands and states (-G ‘os:Ubuntu’, top-file matching) and to make states portable by branching on them.
$ salt 'web1' grains.items | heados: Ubuntuosrelease: 22.04cpuarch: x86_64mem_total: 7950$ salt -G 'roles:web' state.apply nginx # target by a custom grain
Setting and using custom grains
You set custom grains in the minion config or with grains.setval, most often to tag a minion’s role or datacenter so you can target it. In states and templates you read grains via the grains dictionary to pick OS-appropriate package names and paths. The important distinction from pillar: grains live on and are reported by the minion, so they are not a place for secrets or for anything a compromised minion should not be able to influence.
{% set pkg = 'apache2' if grains['os_family'] == 'Debian' else 'httpd' %}webserver:pkg.installed:- name: {{ pkg }}