I was thinking of creating an issue & submitting a PR to address a core issue with the docker dynamic inventory script (ansible/contrib/inventory/docker.py). In its current state, it adds one or more docker hosts to the inventory, and any of the containers on those docker hosts. It also adds a group for each of the same, such that:
- The
inventory_hostname
s and the correspondinggroup
name for each docker hosts is set to the URI used to connect to the Docker API, which results in Ansible inventory hosts with names likeunix:///var/run/docker.sock
orhttps://127.0.0.1:2376
. - Similarly, for containers, among the groups automatically created for each is one which takes the format
image_[*repository*/]<*image*>[:*tag*]
, which results in groups likeimage_milo/centos:7
This causes a couple of problems.
- Managing
host_vars
andgroup_vars
for these hosts becomes very painful due to filesystem file naming restrictions, which generally do not permit filenames containing a/
. As a consequence,host_vars
andgroup_vars
need to be set in the host file, instead of managed through per host/group files and/or directories. - As is likely often the case, if one of the docker hosts is running locally on the Ansible host, and
localhost
is listed in the inventory, plays targetingall
, for example, will be run twice on the Ansible host (i.e. once againstlocalhost
and again againstunix:///var/run/docker.sock
). This could raise problems, and it also highlights again the issue it creates withhost_vars
andgroup_vars
management, since you’d need to ensure common variables are set in the hosts file. This problem also arises if one of the inventory hosts is also one of the docker hosts (e.g.foo.example.com
andhttps://foo.example.com:2376
) - As of v.2.4.0 (I think), Ansible emits a warning for all of the dynamically added docker hosts & containers, for example:
[WARNING]: Found both group and host with same name: unix:///var/run/docker.sock
In addition to these issues, I think semantically it doesn’t make sense for the URI scheme (https://, unix://) to be part of the inventory_hostname
in Ansible, since Ansible is ostensibly for managing hosts, where a unique host should generally be addressable under a single DNS hostname or IP address (I think there are some exceptions but not sure this needs to be one). Nor does it make sense for the docker API port to be part of the inventory hostname, which may be confused for a non-standard ssh port.
Thoughts?
Also, is there documentation/guidance somewhere which clearly defines what constitutes valid inventory_hostname
s and group
names, or possibly a dynamic inventory test script to verify that dynamic inventory scripts are not violating any such naming rules/conventions?