Skip to content

Instantly share code, notes, and snippets.

@guyc
Created April 23, 2019 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyc/8355e72722b254107f5d68203f87dd0d to your computer and use it in GitHub Desktop.
Save guyc/8355e72722b254107f5d68203f87dd0d to your computer and use it in GitHub Desktop.
Encode a bash file into base64 cloudinit user data.
#!./env/bin/python
import base64
import sys
def encode_user_data(lines):
"""
Input is an array of lines that together make a script.
Output is a base64 encoded block suitable for the userdata parameter
of an instance config.
"""
cloud_config = "#cloud-config\nruncmd:\n" + ''.join([f" - {line}" for line in lines])
return base64.b64encode(cloud_config.encode("utf-8")).decode('ascii')
if __name__ == "__main__":
print(encode_user_data(sys.stdin.readlines()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment