【问题标题】:How to associate default templates to an OS in Foreman 1.15.6?如何将默认模板与 Foreman 1.15.6 中的操作系统相关联?
【发布时间】:2019-04-27 18:17:20
【问题描述】:

我正在使用 Foreman 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]                
    }
}   

【问题讨论】:

    标签: theforeman


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多