@@ -0,0 +1,2556 @@
+#!/usr/bin/env python%0A#%0A# (c) 2015-16 Florian Haas, hastexo Professional Services GmbH%0A# %3Cflorian@hastexo.com%3E%0A# Based in part on:%0A# libvirt_lxc.py, (c) 2013, Michael Scherer %3Cmisc@zarb.org%3E%0A#%0A# This file is part of Ansible,%0A#%0A# Ansible is free software: you can redistribute it and/or modify%0A# it under the terms of the GNU General Public License as published by%0A# the Free Software Foundation, either version 3 of the License, or%0A# (at your option) any later version.%0A#%0A# Ansible is distributed in the hope that it will be useful,%0A# but WITHOUT ANY WARRANTY; without even the implied warranty of%0A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A# GNU General Public License for more details.%0A#%0A# You should have received a copy of the GNU General Public License%0A# along with Ansible.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A%22%22%22%0AAnsible inventory script for LXC containers. Requires Python%0Abindings for LXC API.%0A%0AIn LXC, containers can be grouped by setting the lxc.group option,%0Awhich may be found more than once in a container's%0Aconfiguration. So, we enumerate all containers, fetch their list%0Aof groups, and then build the dictionary in the way Ansible expects%0Ait.%0A%22%22%22%0Afrom __future__ import print_function%0A%0Aimport sys%0Aimport lxc%0Aimport json%0A%0Adef build_dict():%0A    %22%22%22Returns a dictionary keyed to the defined LXC groups. All%0A    containers, including the ones not in any group, are included in the%0A    %22all%22 group.%22%22%22%0A    # Enumerate all containers, and list the groups they are in. Also,%0A    # implicitly add every container to the 'all' group.%0A    containers = dict(%5B(c,%0A                        %5B'all'%5D +%0A                        (lxc.Container(c).get_config_item('lxc.group') or %5B%5D))%0A                       for c in lxc.list_containers()%5D)%0A%0A    # Extract the groups, flatten the list, and remove duplicates%0A    groups = set(sum(%5Bg for g in containers.values()%5D, %5B%5D))%0A%0A    # Create a dictionary for each group (including the 'all' group%0A    return dict(%5B(g, %7B'hosts': %5Bk for k, v in containers.items() if g in v%5D,%0A                      'vars': %7B'ansible_connection':'lxc'%7D%7D) for g in groups%5D)%0A%0Adef main(argv):%0A    %22%22%22Returns a JSON dictionary as expected by Ansible%22%22%22%0A    result = build_dict()%0A    if len(argv) == 2 and argv%5B1%5D == '--list':%0A        json.dump(result, sys.stdout)%0A    elif len(argv) == 3 and argv%5B1%5D == '--host':%0A        json.dump(%7B'ansible_connection': 'lxc'%7D, sys.stdout)%0A    else:%0A        print(%22Need an argument, either --list or --host %3Chost%3E%22, file=sys.stderr)%0A%0Aif __name__ == '__main__':%0A    main(sys.argv)%0A

Add inventory script for LXC containers