Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

python - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...