繁体   English   中英

Ansible Tower 调查规范

[英]Ansible Tower survey spec

我试图从塔调查中获取信息并在路障中运行。 我有以下代码。

- 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 }}"

我需要获取调查中具有特定键:值对的所有作业模板的列表,然后遍历该列表并根据 ID 提供我的新作业模板列表。

这是注册调查变量的示例

 "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"
            },

如果它找到“变量”:“帐户”,我需要获取所有具有此功能的作业模板。

如果您使用 python(3.6 或更高版本),我认为这很容易。
例如,这里就像。

#!/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()

在 page_size 查询中,模板最大大小可以指定在您想要获取的位置。
在上面的脚本中,page_size 变量设置为 100,因此最多可以获取 100 个模板。
它的脚本需要请求库,所以请安装以下程序。

$ pip install requirests

以下是上述脚本执行后的结果示例。

$ ./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

通过将 template['id'] 添加到 template_list 中,您可以获得模板 id 是可能的。
例如,这里就像。

#!/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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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