[comment]: # translation:outdated

[comment]: # ({3ab1bd41-c572a02a})
# 更新

[comment]: # ({/3ab1bd41-c572a02a})

[comment]: # ({559de78a-7ee0d5db})
### 描述

`object host.update(object/array hosts)`

该方法用来更新已存在的主机。

[comment]: # ({/559de78a-7ee0d5db})

[comment]: # ({new-3a8eac6a})
### 参数

`(object/array)` 要更新的主机属性。

必须为每个主机定义`hostid`属性，所有其他属性都是可选的。当只更新指定的属性，其他属性将保持不变。

但是请注意，更新主机名称还会通过主机的名称值来更新主机的可见名称（如果未提供或为空）。

另外，对于[标准主机属性](object#host)，此方法接受如下参数。

|参数               [|型](//zh/manual/api/reference_commentary#data_types)   描述|<|
|----------------------|--------------------------------------------------------------|-|
|groups|对象/数组                                                 替换主|所属的当前[主机组](//zh/manual/api/reference/hostgroup/object#host_group)。<br><br>主机组必须具有定义的`groupid`属性。请求中未列出的所有主机组将被取消链接。|
|interfaces|对象/数组                                                 替换当|的主机[接口](/manual/api/reference/hostinterface/object)。|
|tags|object/array|替换当前的主机[标签](//zh/manual/api/reference/host/object#host_tag)。<br><br>请求中未列出的所有标签将被删除。|
|inventory|对象                                                      主|[资产清单](//zh/manual/api/reference/host/object#host_inventory)属性。|
|macros|对象/数组                                                 替换当|[用户宏](//zh/manual/api/reference/usermacro/object)。|
|templates|对象/数组                                                 替换当|链接的[模板](//zh/manual/api/reference/template/object)。请求中未列出的所有模板将仅取消链接。<br><br>模板必须已定义过`templateid`属性。|
|templates\_clear|对象/数组                                                 从主机|删除[模板](//zh/manual/api/reference/template/object)链接并清除。<br><br>模板必须已定义过`templateid`属性。|

<note
tip>相对于Zabbix前端，当`name`和`host`一致，更新`host`的时候不会自动更新`name`。两个属性需要明确的更新。这两个属性都需要显式地更新。
:::

[comment]: # ({/new-3a8eac6a})

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

`(object)` 在`hostids`属性中返回包含已更新主机ID对象。

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

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

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

[comment]: # ({3bc6a350-ce50c453})
#### 启用主机

启用主机，将status设置为0。

请求：

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10126",
        "status": 0
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

响应：

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}
```

[comment]: # ({/3bc6a350-ce50c453})

[comment]: # ({d4f38148-bea1db7e})
#### 删除模板链接

从主机中删除链接并清除两个模板。

请求：

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10126",
        "templates_clear": [
            {
                "templateid": "10124"
            },
            {
                "templateid": "10125"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

响应：

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}
```

[comment]: # ({/d4f38148-bea1db7e})

[comment]: # ({588992a1-98dcfd68})
#### 更新主机宏

用两个新的宏替换主机所有的宏。

请求：

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10126",
        "macros": [
            {
                "macro": "{$PASS}",
                "value": "password"
            },
            {
                "macro": "{$DISC}",
                "value": "sda"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

响应：

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10126"
        ]
    },
    "id": 1
}
```

[comment]: # ({/588992a1-98dcfd68})

[comment]: # ({d63c1fb1-da99f3b8})
#### 更新主机资产清单

更改资产清单模式并添加位置。

请求：

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10387",
        "inventory_mode": 0,
        "inventory": {
            "location": "Latvia, Riga"
        }
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

响应:

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10387"
        ]
    },
    "id": 2
}
```

[comment]: # ({/d63c1fb1-da99f3b8})

[comment]: # ({119137d4-5cc5d950})
#### 更新主机标签

用一个新的标签替换所有的标签。

请求：

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10387",
        "tags": {
            "tag": "OS",
            "value": "CentOS 7"
        }
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

响应：

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10387"
        ]
    },
    "id": 1
}
```

[comment]: # ({/119137d4-5cc5d950})

[comment]: # ({new-d2b03b9b})
#### Updating host encryption

Update the host "10590" to use PSK encryption only for connections from host to Zabbix server, and change the PSK identity and PSK key.
Note that the host has to be [pre-configured to use PSK](/manual/encryption/using_pre_shared_keys#configuring-psk-for-server-agent-communication-example).

Request:

``` {.java}
{
    "jsonrpc": "2.0",
    "method": "host.update",
    "params": {
        "hostid": "10590",
        "tls_connect": 1,
        "tls_accept": 2,
        "tls_psk_identity": "PSK 002",
        "tls_psk": "e560cb0d918d26d31b4f642181f5f570ad89a390931102e5391d08327ba434e9"
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```

Response:

``` {.java}
{
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10590"
        ]
    },
    "id": 1
}
```

[comment]: # ({/new-d2b03b9b})

[comment]: # ({53d498ec-23501347})
### 参考

-   [主机.批量创建](massadd)
-   [主机.批量更新](massupdate)
-   [主机.批量删除](massremove)
-   [主机组](/zh/manual/api/reference/hostgroup/object#host_group)
-   [模板](/zh/manual/api/reference/template/object#template)
-   [用户宏](/zh/manual/api/reference/usermacro/object#hosttemplate_level_macro)
-   [主机接口](/zh/manual/api/reference/hostinterface/object#host_interface)
-   [主机资产清单](object#host_inventory)
-   [主机标签](object#host_tag)

[comment]: # ({/53d498ec-23501347})

[comment]: # ({d409d155-a39b5a01})
### 来源

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

[comment]: # ({/d409d155-a39b5a01})
