##### SUMMARY
On a LXD cluster configuration, an ansible task using the Ans…ible module lxd_container for container creation will end up in failure if the container is created on a remote LXD cluster node. The container is however created, but stopped.
As a result, in the case of a 2 nodes cluster configuration, and knowing that the LXD cluster create the new containers on the node with the least amount of containers, the task will fail each 2 runs.
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME
lxd_container
##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.7.8
config file = /home/toto/.ansible.cfg
configured module search path = [u'/home/toto/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
```
##### CONFIGURATION
```
$ ansible-config dump --only-changed
ANSIBLE_PIPELINING(/home/toto/.ansible.cfg) = True
ANSIBLE_SSH_ARGS(/home/toto/.ansible.cfg) = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
DEFAULT_BECOME(/home/toto/.ansible.cfg) = True
DEFAULT_BECOME_METHOD(/home/toto/.ansible.cfg) = sudo
DEFAULT_BECOME_USER(/home/toto/.ansible.cfg) = root
DEFAULT_HOST_LIST(/home/toto/.ansible.cfg) = [u'/etc/ansible/hosts_xlab', u'/home/toto/ansible/hosts']
DEFAULT_LOG_PATH(/home/toto/.ansible.cfg) = /var/log/ansible.log
DEFAULT_PRIVATE_KEY_FILE(/home/toto/.ansible.cfg) = /home/toto/.ssh/id_rsa_ansible
DEFAULT_REMOTE_USER(/home/toto/.ansible.cfg) = ansible
DEFAULT_ROLES_PATH(/home/toto/.ansible.cfg) = [u'/home/toto/ansible/roles', u'/home/toto/roles']
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /home/toto/.vault_pass.txt
RETRY_FILES_ENABLED(/home/toto/.ansible.cfg) = False
```
##### OS / ENVIRONMENT
```
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
```
```
$ lxd version
3.10
$ lxc version
Client version: 3.10
Server version: 3.10
```
##### STEPS TO REPRODUCE
Enough to have a 2 nodes LXD cluster configuration:
```
$ lxc cluster list
+------------+---------------------------+----------+--------+-------------------+
| NAME | URL | DATABASE | STATE | MESSAGE |
+------------+---------------------------+----------+--------+-------------------+
| space01 | https://10.33.254.21:8443 | YES | ONLINE | fully operational |
+------------+---------------------------+----------+--------+-------------------+
| space01-fr | https://10.33.254.22:8443 | YES | ONLINE | fully operational |
+------------+---------------------------+----------+--------+-------------------+
```
A simple task to create containers:
```
---
- name: Create new container TEST
connection: local
lxd_container:
name: "{{ container }}"
state: started
source:
type: image
alias: centos7
profiles: ["default"]
wait_for_ipv4_addresses: true
```
Run the task twice, both from the same cluster node, space01-fr in my case.
mycontainer will then be created on space01-fr, and mycontainer2 on space01:
```
ansible-playbook ~/ansible/playbooks/create_container.yml --extra-vars "container=mycontainer" --limit debian9 -vvvv
ansible-playbook ~/ansible/playbooks/create_container.yml --extra-vars "container=mycontainer2" --limit debian9 -vvvv
```
##### EXPECTED RESULTS
Both playbook runs should successfully end up by creating first the running "mycontainer" container on space01-fr and another running one, mycontainer2 on space01
##### ACTUAL RESULTS
First run correctly work, task successfully ends up:
```
TASK [container : Create new container TEST] ************************************************************************************************************************************
task path: /home/toto/roles/container/tasks/main.yml:2
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/lxd/lxd_container.py
<10.0.3.210> ESTABLISH LOCAL CONNECTION FOR USER: toto
<10.0.3.210> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-kowkhphfkqcrepmoevartsayavowlvxb; /usr/bin/python'"'"' && sleep 0'
ok: [debian9] => {
"actions": [],
"addresses": {
"eth0": [
"10.0.3.141"
]
},
"changed": false,
"invocation": {
"module_args": {
"architecture": null,
"cert_file": "/root/.config/lxc/client.crt",
"config": null,
"description": null,
"devices": null,
"ephemeral": null,
"force_stop": false,
"key_file": "/root/.config/lxc/client.key",
"name": "mycontainer",
"profiles": [
"default"
],
"source": {
"alias": "centos7",
"type": "image"
},
"state": "started",
"timeout": 30,
"trust_password": null,
"url": "unix:/var/lib/lxd/unix.socket",
"wait_for_ipv4_addresses": true
}
},
"log_verbosity": 4,
"logs": [
{
"request": {
"json": null,
"method": "GET",
"timeout": null,
"url": "/1.0/containers/mycontainer"
},
"response": {
"json": {
"error": "",
"error_code": 0,
"metadata": {
"architecture": "x86_64",
"config": {
"volatile.base_image": "e95126069b4318f855b5785d1152920911001a5ecd7b20ca965c17076d6b7cc2",
"volatile.eth0.hwaddr": "00:16:3e:72:e2:83",
"volatile.idmap.base": "0",
"volatile.idmap.next": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536}]",
"volatile.last_state.idmap": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536}]",
"volatile.last_state.power": "RUNNING"
},
"created_at": "2019-03-07T00:08:33.191688456+01:00",
"description": "",
"devices": {},
"ephemeral": false,
"expanded_config": {
"volatile.base_image": "e95126069b4318f855b5785d1152920911001a5ecd7b20ca965c17076d6b7cc2",
"volatile.eth0.hwaddr": "00:16:3e:72:e2:83",
"volatile.idmap.base": "0",
"volatile.idmap.next": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536}]",
"volatile.last_state.idmap": "[{\"Isuid\":true,\"Isgid\":false,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536},{\"Isuid\":false,\"Isgid\":true,\"Hostid\":1000000,\"Nsid\":0,\"Maprange\":65536}]",
"volatile.last_state.power": "RUNNING"
},
"expanded_devices": {
"eth0": {
"name": "eth0",
"nictype": "bridged",
"parent": "lxcbr0",
"type": "nic"
},
"root": {
"path": "/",
"pool": "local",
"type": "disk"
}
},
"last_used_at": "2019-03-07T00:08:46.07437376+01:00",
"location": "space01-fr",
"name": "mycontainer",
"profiles": [
"default"
],
"stateful": false,
"status": "Running",
"status_code": 103
},
"operation": "",
"status": "Success",
"status_code": 200,
"type": "sync"
}
},
"type": "sent request"
},
{
"request": {
"json": null,
"method": "GET",
"timeout": null,
"url": "/1.0/containers/mycontainer/state"
},
"response": {
"json": {
"error": "",
"error_code": 0,
"metadata": {
"cpu": {
"usage": 1626940399
},
"disk": {},
"memory": {
"swap_usage": 0,
"swap_usage_peak": 0,
"usage": 15630336,
"usage_peak": 16789504
},
"network": {
"eth0": {
"addresses": [
{
"address": "10.0.3.141",
"family": "inet",
"netmask": "24",
"scope": "global"
},
{
"address": "fe80::216:3eff:fe72:e283",
"family": "inet6",
"netmask": "64",
"scope": "link"
}
],
"counters": {
"bytes_received": 3102,
"bytes_sent": 2736,
"packets_received": 16,
"packets_sent": 20
},
"host_name": "vethURUBCA",
"hwaddr": "00:16:3e:72:e2:83",
"mtu": 1500,
"state": "up",
"type": "broadcast"
},
"lo": {
"addresses": [
{
"address": "127.0.0.1",
"family": "inet",
"netmask": "8",
"scope": "local"
},
{
"address": "::1",
"family": "inet6",
"netmask": "128",
"scope": "local"
}
],
"counters": {
"bytes_received": 0,
"bytes_sent": 0,
"packets_received": 0,
"packets_sent": 0
},
"host_name": "",
"hwaddr": "",
"mtu": 65536,
"state": "up",
"type": "loopback"
}
},
"pid": 6860,
"processes": 12,
"status": "Running",
"status_code": 103
},
"operation": "",
"status": "Success",
"status_code": 200,
"type": "sync"
}
},
"type": "sent request"
}
],
"old_state": "started"
}
META: ran handlers
META: ran handlers
PLAY RECAP *****************************************************************************************************************************************************************************
debian9 : ok=2 changed=0 unreachable=0 failed=0
```
Container up and running:
```
$ lxc list
+--------------+---------+-------------------+------+------------+-----------+------------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | LOCATION |
+--------------+---------+-------------------+------+------------+-----------+------------+
| mycontainer | RUNNING | 10.0.3.141 (eth0) | | PERSISTENT | | space01-fr |
+--------------+---------+-------------------+------+------------+-----------+------------+
```
But the second playbook run unexpectedly fails, the container is created but stopped:
```
task path: /home/toto/roles/container/tasks/main.yml:2
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/lxd/lxd_container.py
<10.0.3.210> ESTABLISH LOCAL CONNECTION FOR USER: toto
<10.0.3.210> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-lomqbbiwaftywxrwshfxyzqixwhznesf; /usr/bin/python'"'"' && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_lxd_container_payload_sUBNvM/__main__.py", line 509, in run
action()
File "/tmp/ansible_lxd_container_payload_sUBNvM/__main__.py", line 394, in _started
self._create_container()
File "/tmp/ansible_lxd_container_payload_sUBNvM/__main__.py", line 339, in _create_container
self.client.do('POST', '/1.0/containers', config)
File "/tmp/ansible_lxd_container_payload_sUBNvM/ansible_lxd_container_payload.zip/ansible/module_utils/lxd.py", line 97, in do
self._raise_err_from_json(resp_json)
File "/tmp/ansible_lxd_container_payload_sUBNvM/ansible_lxd_container_payload.zip/ansible/module_utils/lxd.py", line 132, in _raise_err_from_json
raise LXDClientException(self._get_err_from_resp_json(resp_json), **err_params)
fatal: [debian9]: FAILED! => {
"actions": [],
"changed": false,
"invocation": {
"module_args": {
"architecture": null,
"cert_file": "/root/.config/lxc/client.crt",
"config": null,
"description": null,
"devices": null,
"ephemeral": null,
"force_stop": false,
"key_file": "/root/.config/lxc/client.key",
"name": "mycontainer2",
"profiles": [
"default"
],
"source": {
"alias": "centos7",
"type": "image"
},
"state": "started",
"timeout": 30,
"trust_password": null,
"url": "unix:/var/lib/lxd/unix.socket",
"wait_for_ipv4_addresses": true
}
},
"logs": [
{
"request": {
"json": null,
"method": "GET",
"timeout": null,
"url": "/1.0/containers/mycontainer2"
},
"response": {
"json": {
"error": "not found",
"error_code": 404,
"type": "error"
}
},
"type": "sent request"
},
{
"request": {
"json": {
"name": "mycontainer2",
"profiles": [
"default"
],
"source": {
"alias": "centos7",
"type": "image"
}
},
"method": "POST",
"timeout": null,
"url": "/1.0/containers"
},
"response": {
"json": {
"error": "",
"error_code": 0,
"metadata": {
"class": "task",
"created_at": "2019-03-07T00:09:53.791817652+01:00",
"description": "Creating container",
"err": "",
"id": "44397294-5375-4f5c-88fb-07aac4efaec0",
"may_cancel": false,
"metadata": null,
"resources": {
"containers": [
"/1.0/containers/mycontainer2"
]
},
"status": "Running",
"status_code": 103,
"updated_at": "2019-03-07T00:09:53.791817652+01:00"
},
"operation": "/1.0/operations/44397294-5375-4f5c-88fb-07aac4efaec0?project=default",
"status": "Operation created",
"status_code": 100,
"type": "async"
}
},
"type": "sent request"
},
{
"request": {
"json": null,
"method": "GET",
"timeout": null,
"url": "/1.0/operations/44397294-5375-4f5c-88fb-07aac4efaec0?project=default/wait"
},
"response": {
"json": {
"error": "",
"error_code": 0,
"metadata": {
"class": "task",
"created_at": "2019-03-07T00:09:53.791817652+01:00",
"description": "Creating container",
"err": "",
"id": "44397294-5375-4f5c-88fb-07aac4efaec0",
"may_cancel": false,
"metadata": null,
"resources": {
"containers": [
"/1.0/containers/mycontainer2"
]
},
"status": "Running",
"status_code": 103,
"updated_at": "2019-03-07T00:09:53.791817652+01:00"
},
"operation": "",
"status": "Success",
"status_code": 200,
"type": "sync"
}
},
"type": "sent request"
}
],
"msg": ""
}
PLAY RECAP *****************************************************************************************************************************************************************************
debian9 : ok=1 changed=0 unreachable=0 failed=1
```
```
$ lxc list
+--------------+---------+-------------------+------+------------+-----------+------------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | LOCATION |
+--------------+---------+-------------------+------+------------+-----------+------------+
| mycontainer | RUNNING | 10.0.3.141 (eth0) | | PERSISTENT | | space01-fr |
+--------------+---------+-------------------+------+------------+-----------+------------+
| mycontainer2 | STOPPED | | | PERSISTENT | | space01 |
+--------------+---------+-------------------+------+------------+-----------+------------+
```