Docker inventory

Similar to the OpenStack and EC2 inventory plugins detailed earlier in this book, a Docker inventory script is also available. The Docker script is located at contrib/inventory/docker.py, within the Ansible source repository, with an associated configuration file at contrib/inventory/docker.yml. To make use of this script, simply copy the .py file to the playbook directory that expects to use it, or to a path accessible to all users/playbooks on the system that will be executing Ansible. For our example, I'll copy it to the playbook directory. The configuration file, which can be used to define how to connect to one or more Docker daemons, does not need to be used for this example, as we'll simply be connecting to the local Docker daemon.

The help output for the script shows many possible arguments; however, the ones that Ansible will use are --list and --host:

If the previously built container is still running when this script is executed to list hosts, it should appear in the output (grep has been used to make this more obvious in the screenshot):

Like earlier, a number of groups are presented, which have the running container as a member. The two groups that were shown earlier are the short container ID and the long container ID. Many variables are also defined as a part of the output, which has been heavily truncated in the preceding screenshot. The tail end of the output reveals a few more groups:

The additional groups are as follows:

  • docker_hosts: All of the hosts running the Docker daemon that the dynamic inventory script has communicated with and queried for containers
  • image_name: A group for each image used by discovered containers
  • container name: A group that matches the name of the container
  • running: A group of all the running containers
  • stopped: A group of all the stopped containers

This inventory plugin, and the groups and data provided by it, can be used by playbooks to target various selections of containers available, in order to interact without the need for manual inventory management or the use of add_hosts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.5.239