简体   繁体   中英

Ansible Tower survey spec

I am trying to get information from tower surveys and running in a road block. I have the following code.

- name: Gather all Job Templates
  shell: awx job_templates list -f human --filter 'Id' --all --survey_enabled true | tail -n +3
  register: job_templates_all
  when: update_all is 'True'

- name: Gather survey of templates
  uri:
    url: "https://{{ tower_env }}.fqdn.com/api/v2/job_templates/{{ item }}/survey_spec/"
    method: GET
    return_content: yes
    user: "{{ tower_username }}"
    password: "{{ tower_password }}"
    force_basic_auth: yes
    body_format: json
    validate_certs: no
  register: survey
  when: update_all is 'True'
  with_items: "{{ job_templates_all.stdout }}"

I need to get a list of all job templates that has a specific key:value pair in the survey and and then loop through that list and provide my a new job template list based on ID.

Here is an example of the registered survey var

 "json": {
        "description": "",
        "name": "",
        "spec": [
            {
                "question_description": "",
                "min": null,
                "default": "",
                "max": null,
                "required": true,
                "choices": "foo\bar",
                "variable": "account",
                "question_name": "Account",
                "type": "multiplechoice"
            },
            {
                "question_description": "",
                "min": null,
                "default": "us-east-1",
                "max": null,
                "required": true,
                "choices": "us-east-1\nus-west-2",
                "new_question": true,
                "variable": "region",
                "question_name": "Region",
                "type": "multiplechoice"
            },

If it finds "variable": "account", I need to get of all job templates that have this.

If you use python(3.6 or higher), I think easy it gets.
For example, here's like.

#!/usr/bin/env python
import requests
import urllib3
urllib3.disable_warnings()
import json

tower_ip = ''
tower_usre = 'admin'
tower_pass = ''
page_size = 100 # is total template number you want to get

def main():
    template_list = []

    url = "https://%s/api/v2/job_templates?page_size=%s" % (tower_ip, page_size)
    r = requests.get(url,
                     auth=(tower_usre, tower_pass),
                     verify=False)
    all_templates = json.loads(r.text)['results']

    for template in all_templates:
        url = "https://%s%s" % (tower_ip, template['related']['survey_spec'])
        r = requests.get(url,
                         auth=(tower_usre, tower_pass),
                         verify=False)
        all_survey = json.loads(r.text)
        if all_survey:
            for survey in all_survey['spec']:
                if 'variable' in survey and survey['variable'] == 'account':
                    template_list.append({
                        'name': template['name'],
                        'survey': survey
                    })

    if template_list:
        print(json.dumps(template_list, indent=2))
    else:
        print("Not found template")

if __name__ == "__main__":
    main()

In the page_size query, the template max size can specify at you want to get.
In the above script, the page_size variable set 100, so it can get a max of 100 templates.
It script requires the request library, so please install the following procedure.

$ pip install requirests

The following an result example after the above script executed.

$ ./sample.py
[
  {
    "name": "example01",
    "survey": {
      "question_name": "Account",
      "question_description": "",
      "required": true,
      "type": "multiselect",
      "variable": "account",
      "min": null,
      "max": null,
      "default": "",
      "choices": "hoge\nfuga"
    }
  },
  {
    "name": "test-8862287",
    "survey": {
      "question_name": "Account",
      "question_description": "",
      "required": true,
      "type": "multiselect",
      "variable": "account",
      "min": null,
      "max": null,
      "default": "",
      "choices": "abc\ndef",
      "new_question": true
    }
  }
]

https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Job_Templates/Job_Templates_job_templates_list

By template['id'] adding to template_list, you can get a template id is possible.
For example, here's like.

#!/usr/bin/env python
import requests
import urllib3
urllib3.disable_warnings()
import json

tower_ip = ''
tower_usre = 'admin'
tower_pass = ''
page_size = 100 # is total template number you want to get

def main():
    template_list = []

    url = "https://%s/api/v2/job_templates?page_size=%s" % (tower_ip, page_size)
    r = requests.get(url,
                     auth=(tower_usre, tower_pass),
                     verify=False)
    all_templates = json.loads(r.text)['results']

    for template in all_templates:
        url = "https://%s%s" % (tower_ip, template['related']['survey_spec'])
        r = requests.get(url,
                         auth=(tower_usre, tower_pass),
                         verify=False)
        all_survey = json.loads(r.text)
        if all_survey:
            for survey in all_survey['spec']:
                if 'variable' in survey and survey['variable'] == 'account':
                    template_list.append({
                        'name': template['name'],
                        'template_id': template['id'],
                        'survey': survey
                    })

    if template_list:
        print(json.dumps(template_list, indent=2))
    else:
        print("Not found template")

if __name__ == "__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM