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:
inventory_hostnames and the corresponding group 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 like unix:///var/run/docker.sock or https://127.0.0.1:2376.image_[*repository*/]<*image*>[:*tag*], which results in groups like image_milo/centos:7This causes a couple of problems.
host_vars and group_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 and group_vars need to be set in the host file, instead of managed through per host/group files and/or directories.localhost is listed in the inventory, plays targeting all, for example, will be run twice on the Ansible host (i.e. once against localhost and again against unix:///var/run/docker.sock). This could raise problems, and it also highlights again the issue it creates with host_vars and group_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 and https://foo.example.com:2376)[WARNING]: Found both group and host with same name: unix:///var/run/docker.sockIn 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_hostnames and group names, or possibly a dynamic inventory test script to verify that dynamic inventory scripts are not violating any such naming rules/conventions?
[unix://var/run/docker.sock:vars]
ansible_connection=docker