0

我正在使用工头 1.15.6。

我使用以下有效负载创建了一个操作系统。但是,模板与操作系统无关。因此,我不得不手动关联 Foreman UI 中的模板。

REST API 文档https://www.theforeman.org/api/1.15/index.html 没有显示任何参数来设置操作系统的默认模板。

如何使用 REST API 设置默认模板?

我正在使用以下 JSON 有效负载来创建操作系统

{
    "operatingsystem": {
        "name": "redhat7.5",
        "major": 7,
        "family": "Redhat",
        "architecture_ids": [1],
        "medium_ids": [1],
        "ptable_ids": [1],
        "provisioning_template_ids": [11, 20]                
    }
}   
4

1 回答 1

0

Foreman API 文档并没有真正的帮助。

我使用hammer set-default-template详细模式来查找 REST API 调用。

下面是使用 REST API 为操作系统设置默认模板的示例代码

import requests
requests.packages.urllib3.disable_warnings()


FOREMAN_URL = 'https://192.168.10.20/api'
FOREMAN_CREDENTIALS = ('admin', 'foreman')

HEADERS = {'content-type': 'application/json'}


def get_template_by_id(template_id):
    url = '{}/{}/{}'.format(FOREMAN_URL, "config_templates", template_id)
    template = requests.get(url, auth=FOREMAN_CREDENTIALS, verify=False)
    return template.json()

def set_os_default_templates(os_id, template_ids):
    for template_id in template_ids:
        template = get_template_by_id(template_id)
        url = '{}/{}/{}/{}'.format(FOREMAN_URL, "operatingsystems", os_id, "os_default_templates")
        payload = {"os_default_template": {"config_template_id": str(template_id), "template_kind_name":template['template_kind_name']}}
        requests.post(url, headers=HEADERS, auth=FOREMAN_CREDENTIALS, json=payload, verify=False)

# Set templates with ids 10, 20 as default templates to OS with id 1        
os_id = 1
template_ids = (10, 20)
set_os_default_templates(os_id, template_ids)
于 2018-11-27T04:40:06.987 回答