Last active
February 7, 2020 19:32
-
-
Save liamdawson/a13d80f26337b6fe9ab2 to your computer and use it in GitHub Desktop.
Set a grain from an api call on a minion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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