Search code examples
pythonansibleansible-templateansible-awx

Launching a workflow template with extra vars


I'm trying to launch a workflow template from a python script in Ansible AWX, my script looks like this

#!/usr/bin/env python
import requests
import json
from requests.auth import HTTPBasicAuth
import base64
import os


def main():
    URL = 'https://myawx/api/v2/'
    USER = 'username'
    PASSWORD = 'password'
    session = object
    session = requests.Session()
    session.auth = (USER, PASSWORD)
    session.verify = False
    myvars={
        "test": "test"
    }

    AWX_JOB_TEMPLATES_API="https://myawx.com/api/v2/workflow_job_templates/10/launch/"
    response = session.post(url=AWX_JOB_TEMPLATES_API,json=myvars, verify=False)
    print(response)


if __name__ == '__main__':
    main()

This launches the workflow fine, but it doesn't include the extra vars from the net_vars variable. My workflow_job_template has "Prompt on launch" enabled for the EXTRA VARIABLES field from the frontend of AWX.

Is it possible to launch a workflow with extra vars?


Solution

  • If you want to use extra vars via REST API, myvars needs to have extra_vars key.
    For example, here's like.

    #!/usr/bin/env python
    import requests
    
    
    def main():
        url = "http://awx_server/api/v2/job_templates/14/launch/"
        username = "admin"
        passwd = "secret"
    
        headers = {
            "Content-Type": "application/json",
        }
    
        data = {}
        data['extra_vars'] = {
            "extra_string": "test message"
        }
    
        req = requests.post(url,
                            headers=headers,
                            auth=(username, passwd),
                            json=data,
                            verify=False)
    
        print(req.status_code)
        print(req.text)
    
    
    if __name__ == "__main__":
        main()
    

    When the above sample, set test message to extra_string variable.
    The extra_string is to be given as extra vars via REST API.