'In salt states, we use "grains.host" to get the host details. How do we get the AWS EC2 availability zone name using salt grains?

In salt states, we use "grains.host" to get the host details. How do we get the AWS EC2 availability zone name using salt grains?

To get the hostname I give the below:

{%- if 'aws-dev-01' in grains.host %}
{%- set device = '.....' %}

To get the EC2 AZ name, are the below lines correct?

{%- if 'us-east-1a' in grains.availability_zone %}
{%- set device = '.....' %}


Solution 1:[1]

That is correct, but you need to set metadata_server_grains: true in the minion config first.
https://docs.saltproject.io/en/latest/ref/grains/all/salt.grains.metadata.html

In addition, only IMDSv1 is currently supported.
https://github.com/saltstack/salt/issues/57514

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OrangeDog