host.create

Description

object host.create(object/array **hosts**)

This method allows to create new hosts.

Parameters

(object/array) Hosts to create.

Additionally to the standard host properties, the method accepts the following parameters.

ParameterTypeDescription
groups
(required)
object/arrayHost groups to add the host to.

The host groups must have the groupid property defined.
interfaces
(required)
object/arrayInterfaces to be created for the host.
tagsobject/arrayHost tags.
templatesobject/arrayTemplates to be linked to the host.

The templates must have the templateid property defined.
macrosobject/arrayUser macros to be created for the host.
inventoryobjectHost inventory properties.

Return values

(object) Returns an object containing the IDs of the created hosts under the hostids property. The order of the returned IDs matches the order of the passed hosts.

Examples

Creating a host

Create a host called “Linux server” with an IP interface and tags, add it to a group, link a template to it and set the MAC addresses in the host inventory.

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.create",
  4. "params": {
  5. "host": "Linux server",
  6. "interfaces": [
  7. {
  8. "type": 1,
  9. "main": 1,
  10. "useip": 1,
  11. "ip": "192.168.3.1",
  12. "dns": "",
  13. "port": "10050"
  14. }
  15. ],
  16. "groups": [
  17. {
  18. "groupid": "50"
  19. }
  20. ],
  21. "tags": [
  22. {
  23. "tag": "Host name",
  24. "value": "Linux server"
  25. }
  26. ],
  27. "templates": [
  28. {
  29. "templateid": "20045"
  30. }
  31. ],
  32. "macros": [
  33. {
  34. "macro": "{$USER_ID}",
  35. "value": "123321"
  36. },
  37. {
  38. "macro": "{$USER_LOCATION}",
  39. "value": "0:0:0",
  40. "description": "latitude, longitude and altitude coordinates"
  41. }
  42. ],
  43. "inventory_mode": 0,
  44. "inventory": {
  45. "macaddress_a": "01234",
  46. "macaddress_b": "56768"
  47. }
  48. },
  49. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  50. "id": 1
  51. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "hostids": [
  5. "107819"
  6. ]
  7. },
  8. "id": 1
  9. }

Creating a host with SNMP interface

Create a host called “SNMP host” with an SNMPv3 interface with details.

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.create",
  4. "params": {
  5. "host": "SNMP host",
  6. "interfaces": [
  7. {
  8. "type": 2,
  9. "main": 1,
  10. "useip": 1,
  11. "ip": "127.0.0.1",
  12. "dns": "",
  13. "port": "161",
  14. "details": {
  15. "version": 3,
  16. "bulk": 0,
  17. "securityname": "mysecurityname",
  18. "contextname": "",
  19. "securitylevel": 1
  20. }
  21. }
  22. ],
  23. "groups": [
  24. {
  25. "groupid": "4"
  26. }
  27. ]
  28. },
  29. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  30. "id": 1
  31. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "hostids": [
  5. "10658"
  6. ]
  7. },
  8. "id": 1
  9. }

See also

Source

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