This is related to issue of this subject. I am trying to figure out how does LXD API behaves with creating instances and targets.
Here is my JSON file (names edited), data.json:
{
"name": "test-4",
"source": {"type": "image",
"alias": "my-template"}
}
curl command:
curl -XPOST -k --cert x.crt --key x.key https://host-01:8443/1.0/instances?target=host-01 --data @data.json | jq
Instance created in first host, here is JSON result:
{
"type": "async",
"status": "Operation created",
"status_code": 100,
"operation": "/1.0/operations/84b22a5d-1c6e-48be-af95-d8e70ac31d8a",
"error_code": 0,
"error": "",
"metadata": {
"id": "84b22a5d-1c6e-48be-af95-d8e70ac31d8a",
"class": "task",
"description": "Creating container",
"created_at": "2020-07-13T21:19:10.866729854+01:00",
"updated_at": "2020-07-13T21:19:10.866729854+01:00",
"status": "Running",
"status_code": 103,
"resources": {
"containers": [
"/1.0/containers/test-4"
],
"instances": [
"/1.0/instances/test-4"
]
},
"metadata": null,
"may_cancel": false,
"err": "",
"location": "host-01"
}
}
lxs ls
:
$ lxc ls
+-----------+---------+------------------------+------+-----------+-----------+----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | LOCATION |
+-----------+---------+------------------------+------+-----------+-----------+----------+
| test-4 | STOPPED | | | CONTAINER | 0 | host-01 |
+-----------+---------+------------------------+------+-----------+-----------+----------+
Now I will try to repeat the command, and specify different target. Normally, if I do it with lxc
, it will not allow me, regardless of target. With curl
it is different, and that is what I do not understand, what is happening. If LXC clsuter tried move the container to another host - that does not happen. And If ansible will try to do same, using LXD API, and achieve nothing, then it will confuse the user. So, what am I doing wrong or misunderstanding?
First, repeating the same command:
curl -XPOST -k --cert x.crt --key x.key https://host-01:8443/1.0/instances?target=host-01 --data @data.json | jq
{
"type": "async",
"status": "Operation created",
"status_code": 100,
"operation": "/1.0/operations/76638686-bd4f-45b3-93ba-33e8c1737df5",
"error_code": 0,
"error": "",
"metadata": {
"id": "76638686-bd4f-45b3-93ba-33e8c1737df5",
"class": "task",
"description": "Creating container",
"created_at": "2020-07-13T21:19:19.911966704+01:00",
"updated_at": "2020-07-13T21:19:19.911966704+01:00",
"status": "Running",
"status_code": 103,
"resources": {
"containers": [
"/1.0/containers/test-4"
],
"instances": [
"/1.0/instances/test-4"
]
},
"metadata": null,
"may_cancel": false,
"err": "",
"location": "host-01"
}
}
repeating same command, but with different target:
curl -XPOST -k --cert x.crt --key x.key https://host-01:8443/1.0/instances?target=host-02 --data @data.json | jq
{
"type": "async",
"status": "Operation created",
"status_code": 100,
"operation": "/1.0/operations/07b7b322-3b84-4086-85bd-1b823f1d9742?project=default",
"error_code": 0,
"error": "",
"metadata": {
"id": "07b7b322-3b84-4086-85bd-1b823f1d9742",
"class": "task",
"description": "Creating container",
"created_at": "2020-07-13T21:19:31.264413533+01:00",
"updated_at": "2020-07-13T21:19:31.264413533+01:00",
"status": "Running",
"status_code": 103,
"resources": {
"containers": [
"/1.0/containers/test-4"
],
"instances": [
"/1.0/instances/test-4"
]
},
"metadata": null,
"may_cancel": false,
"err": "",
"location": "host-02"
}
}
However, lxc ls disagrees with this:
$ lxc ls
+-----------+---------+------------------------+------+-----------+-----------+----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | LOCATION |
+-----------+---------+------------------------+------+-----------+-----------+----------+
| test-4 | STOPPED | | | CONTAINER | 0 | host-01 |
+-----------+---------+------------------------+------+-----------+-----------+----------+
What I am trying to achieve is to test, in case Ansible user tries to run same script again - same API call will be executed. But API does not behave same as LXC command would. Should ansible developer perform additional checks before running API, to check whether instance exists already, and if yes - instead of POST, decide whether to do UPDATE?
Let’s say there is ansible scenario described by ansible user:
Instance A to be deployed on nodeA.
Ansible developer will decide to have these rules then.
- check if container with this name already exists
- if not, create container - send POST API call
- if container already exists, then check if it is already exists on requested node - do nothing
- if container already exists, but not on the node it is requested - move it to another node requested. Should above POST worked for this, if not - then what?
Just FYI, my storage type is directory.
Thanks