Skip to content

Instantly share code, notes, and snippets.

@liamdawson
Last active February 7, 2020 19:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liamdawson/a13d80f26337b6fe9ab2 to your computer and use it in GitHub Desktop.
Save liamdawson/a13d80f26337b6fe9ab2 to your computer and use it in GitHub Desktop.
Set a grain from an api call on a minion
#!py
# The following statefile uses Python to set a grain ('sample-token') to
# a value pulled from a HTTP call. The call will not be repeated if the
# grain is already set.
from salt.utils.http import query
import json
def run():
if __opts__['test']:
return {
'token': {
'test.configurable_test_state': [
{'name': 'sample-token'},
{'result': True},
{'changes': not grains.get('sample-token')}
]
}
}
return {
'token': {
'grains.present': [
{'name': 'sample-token'},
{'value': grains.get('sample-token') or json.loads(
query('http://jsonplaceholder.typicode.com/posts/1',
decode_type=False)['body'])['id']}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment