Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active February 11, 2020 05:29
Show Gist options
  • Save luckylittle/2cb098867c7ac21cb58145378136829a to your computer and use it in GitHub Desktop.
Save luckylittle/2cb098867c7ac21cb58145378136829a to your computer and use it in GitHub Desktop.
AWS Lambda for reactive processes triggered by AWS CloudWatch Events - triggers Ansible execution in Red Hat Ansible Tower
# Handler: lambda.main
# Makes an API call to Red Hat Ansible Tower to launch an Ansible job
from botocore.vendored import requests # This dependency was removed from Botocore and will be removed from Lambda after 2020/03/31
import urllib3
import json
def main(event,context):
"""
AWS Lambda uses 2 parameters (event, context) to provide data to the handler, but we don't really need them for this
"""
urllib3.disable_warnings() # Adding certificate verification is strongly advised
res1 = requests.post('https://1.2.3.4/api/v2/tokens/', verify=False, auth=('username', 'password')) # login to Tower to get the token
response_dict = json.loads(res1.text)
token = response_dict['token']
url = 'https://1.2.3.4/api/v2/job_templates/15/launch/' # This is the URL of the job template in Tower (ID of the job is 15)
res2 = requests.post(url, verify=False, data={},headers={'Content-Type':'application/json','Authorization': 'Bearer {}'.format(token)})
print(res2) # Should return "<Response [201]>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment