[comment]: # ({984854ac-984854ac})
# 创建

[comment]: # ({/984854ac-984854ac})

[comment]: # ({3e4ca937-3ddb6b5d})
### 描述

`object hostprototype.create(object/array hostPrototypes)`

这种方法可以创建新的主机原型。

::: noteclassic
此方法仅适用于 *Admin* 和 *Super admin* 用户类型。调用该方法的权限可在用户角色设置中撤销。详情请参阅 [用户角色](/manual/web_interface/frontend_sections/users/user_roles)。
:::

[comment]: # ({/3e4ca937-3ddb6b5d})

[comment]: # ({26303e78-c4127cc1})
### 参数

`(object/array)` 要创建的主机原型。

除了[标准主机原型属性](object#host-prototype)之外，该方法还接受以下参数。

|参数|[类型](/manual/api/reference_commentary#data-types)|说明|
|--|--|------|
|groupLinks|array|要为主机原型创建的[组链接](/manual/api/reference/hostprototype/object#group-link)。<br><br>[参数行为](/manual/api/reference_commentary#parameter-behavior)：<br>- *必需*|
|ruleid|ID|主机原型所属的 LLD 规则 ID。<br><br>[参数行为](/manual/api/reference_commentary#parameter-behavior)：<br>- *必需*|
|groupPrototypes|array|要为主机原型创建的[组原型](/manual/api/reference/hostprototype/object#group-prototype)。|
|macros|object/array|要为主机原型创建的[用户宏](/manual/api/reference/usermacro/object)。|
|tags|object/array|[主机原型标签](/manual/api/reference/hostprototype/object#host-prototype-tag)。|
|interfaces|object/array|主机原型的[自定义接口](/manual/api/reference/hostprototype/object#custom-interface)。|
|templates|object/array|要链接到主机原型的[模板](/manual/api/reference/template/object)。<br><br>这些模板必须仅定义 `templateid` 属性。|

[comment]: # ({/26303e78-c4127cc1})

[comment]: # ({b9613119-b9613119})
### 返回值

`(object)` 返回一个对象，其中在 `hostids` 属性下包含创建的主机原型的 ID。返回 ID 的顺序与传递的主机原型的顺序一致。

[comment]: # ({/b9613119-b9613119})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({3f491456-0ce023ac})
#### 创建主机原型

在 LLD 规则“23542”上创建主机原型“{#VM.NAME}”，并设置组原型“{#HV.NAME}”、标签对“datacenter”: “{#DATACENTER.NAME}”，以及自定义 SNMPv2 接口 127.0.0.1:161，community 为 {$SNMP_COMMUNITY}。将其链接到主机组“2”。

[请求](/manual/api#performing-requests)：

```json
{
    "jsonrpc": "2.0",
    "method": "hostprototype.create",
    "params": {
        "host": "{#VM.NAME}",
        "ruleid": "23542",
        "custom_interfaces": "1",
        "groupLinks": [
            {
                "groupid": "2"
            }
        ],
        "groupPrototypes": [
            {
                "name": "{#HV.NAME}"
            }
        ],
        "tags": [
            {
                "tag": "datacenter",
                "value": "{#DATACENTER.NAME}"
            }
        ],
        "interfaces": [
            {
                "main": "1",
                "type": "2",
                "useip": "1",
                "ip": "127.0.0.1",
                "dns": "",
                "port": "161",
                "details": {
                    "version": "2",
                    "bulk": "1",
                    "community": "{$SNMP_COMMUNITY}"
                }
            }
        ]
    },
    "id": 1
}
```

响应：

```json
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10103"
        ]
    },
    "id": 1
}
```

[comment]: # ({/3f491456-0ce023ac})

[comment]: # ({47e0dfb2-c4a38bf3})
### 另请参阅

-   [组链接](object#group-link)
-   [组原型](object#group-prototype)
-   [主机原型标签](object#host-prototype-tag)
-   [自定义接口](object#custom-interface)
-   [用户宏](/manual/api/reference/usermacro/object#hosttemplate-level-macro)

[comment]: # ({/47e0dfb2-c4a38bf3})

[comment]: # ({4d10a354-4d10a354})
### 来源

CHostPrototype::create() 在
*ui/include/classes/api/services/CHostPrototype.php*。

[comment]: # ({/4d10a354-4d10a354})
