[comment]: # translation:outdated

[comment]: # ({66c5a546-66c5a546})
# 批量添加

[comment]: # ({/66c5a546-66c5a546})

[comment]: # ({d60cc608-55030171})
### 描述

`object host.massadd(object parameters)`

此方法允许同时添加多个相关对象到所有指定的主机。

::: noteclassic
这个方法仅允许 *管理员* 和 *超级管理员* 用户类型。可以在用户角色中撤销调用方法的权限设置。详情参考[用户角色](/manual/web_interface/frontend_sections/users/user_roles)获取更多信息。
:::

[comment]: # ({/d60cc608-55030171})

[comment]: # ({abd953a7-c25e2be4})
### 参数

`(object)` 参数包含要更新的主机id和要添加到所有主机的对象。

该方法接受以下参数。

|参数|[类型](/manual/api/reference_commentary#数据类型)|描述|
|--|--|------|
|hosts|object/array|指定需要更新的[主机](/manual/api/reference/host/object)。<br><br>主机只能使用`hostid` 属性定义。<br><br>[参数行为](/manual/api/reference_commentary#参数行为):<br>- *必须*|
|groups|object/array|指定需要添加的[主机组](/manual/api/reference/hostgroup/object) 。<br><br>主机组只能使用 `groupid` 属性定义。|
|interfaces|object/array|为指定的主机创建[主机接口](/manual/api/reference/hostinterface/object) 。|
|macros|object/array|为指定的主机创建[用户宏](/manual/api/reference/usermacro/object) 。|
|templates|object/array|链接到给定主机的[模板](/manual/api/reference/template/object)。<br><br>模板只能使用 `templateid` 属性定义。|

[comment]: # ({/abd953a7-c25e2be4})

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

`(object)` 返回一个对象，该对象包含更新主机的 `hostids` 属性。

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

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

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

[comment]: # ({b2ef6e4f-453a1298})
#### 添加宏

给两台主机添加两个新的宏。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "host.massadd",
    "params": {
        "hosts": [
            {
                "hostid": "10160"
            },
            {
                "hostid": "10167"
            }
        ],
        "macros": [
            {
                "macro": "{$TEST1}",
                "value": "MACROTEST1"
            },
            {
                "macro": "{$TEST2}",
                "value": "MACROTEST2",
                "description": "Test description"
            }
        ]
    },
    "id": 1
}
```

响应:

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

[comment]: # ({/b2ef6e4f-453a1298})

[comment]: # ({ae4115e5-ae4115e5})
### 另见

-   [更新](update)
-   [主机组](/manual/api/reference/hostgroup/object#主机组)
-   [模板](/manual/api/reference/template/object#模板)
-   [用户宏](/manual/api/reference/usermacro/object#主机模板级别宏)
-   [主机接口](/manual/api/reference/hostinterface/object#主机接口)

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

[comment]: # ({d5e419cc-d5e419cc})
### 源码

CHost::massAdd() in *ui/include/classes/api/services/CHost.php*.

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