Created
February 9, 2018 15:50
-
-
Save hypergig/6e9a1a56df9a36cc064114dbb58009a1 to your computer and use it in GitHub Desktop.
ansible dynamic inventory script generates inventory based off of running docker containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
This file is an ansible dynamic inventory script used to help resolve docker | |
containers for ansible. | |
It only considers running docker containers that have the label | |
'ansible_managed_container=true'. Other labels will become host groups. eg. | |
(docker-compose) | |
... | |
labels: | |
ansible_managed_container: "true" | |
role: "webservers" | |
... | |
(resulted host group) | |
... | |
[label_role_webservers] | |
.... | |
Do not rename this script as docker.py, as it will conflict with the docker | |
python library. | |
''' | |
# (c) 2017, Jordan Cohen | |
# | |
# This file is part of Ansible, | |
# | |
# Ansible is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# Ansible is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | |
###################################################################### | |
import json | |
from collections import defaultdict | |
import docker | |
def convert_unsafe_chars(val): | |
unsafe_chars = ['=', '-', '.'] | |
rep_char = '_' | |
ret_str = val | |
for char in unsafe_chars: | |
ret_str = ret_str.replace(char, rep_char) | |
return ret_str | |
def docker_inventory(): | |
# the docker label that identifies a container for ansible management | |
docker_label_identifier = 'ansible_managed_container=true' | |
# ansible host group prefix for docker labels | |
group_prefix = 'label_' | |
# default host vars to apply to all docker hosts | |
default_host_vars = {'ansible_connection': 'docker'} | |
# main | |
client = docker.from_env() | |
ansible_managed = client.containers.list( | |
filters={'label': docker_label_identifier}) | |
# return dict | |
inv = defaultdict(list) | |
# group ansible managed docker hosts as docker_hosts and add default vars | |
default_tag = group_prefix + convert_unsafe_chars(docker_label_identifier) | |
inv['docker_hosts'] = {'vars': default_host_vars, | |
'children': [default_tag]} | |
# add all ansible managed docker hosts to the inventory | |
for container in ansible_managed: | |
for k, v in container.attrs['Config']['Labels'].iteritems(): | |
key = convert_unsafe_chars('%s%s_%s' % (group_prefix, k, v)) | |
inv[key].append(container.name) | |
# return | |
print json.dumps(inv, sort_keys=True, indent=2) | |
if __name__ == '__main__': | |
docker_inventory() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment