Skip to content

Instantly share code, notes, and snippets.

@DavidWittman
Last active April 18, 2023 18:16
Show Gist options
  • Select an option

  • Save DavidWittman/10688924 to your computer and use it in GitHub Desktop.

Select an option

Save DavidWittman/10688924 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidWittman revised this gist Apr 15, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions mongo-ansible.py
    Original file line number Diff line number Diff line change
    @@ -69,8 +69,8 @@ def show_host(db, hostname):
    def main():
    parser = ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--list', dest='do_list', action='store_true')
    group.add_argument('--host', dest='do_host', metavar='<hostname>')
    group.add_argument('--list', dest='list', action='store_true')
    group.add_argument('--host', dest='host', metavar='<hostname>')
    args = parser.parse_args()

    connection = pymongo.Connection(MONGO_HOST, MONGO_PORT)
    @@ -79,10 +79,10 @@ def main():
    if MONGO_USER and MONGO_PASS:
    db.authenticate(MONGO_USER, MONGO_PASS)

    if args.do_list:
    if args.list:
    result = list_groups(db)
    elif args.do_host:
    result = show_host(db, args.do_host)
    elif args.host:
    result = show_host(db, args.host)

    print(result)

  2. DavidWittman revised this gist Apr 14, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mongo-ansible.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    MONGO_USER = "ansible"
    MONGO_PASS = "mongogogo"

    def list_hosts(db, query={}):
    def list_groups(db, query={}):
    """Returns a dict of all the available groups to be managed
    Group documents in Mongo are in the form:
    @@ -80,7 +80,7 @@ def main():
    db.authenticate(MONGO_USER, MONGO_PASS)

    if args.do_list:
    result = list_hosts(db)
    result = list_groups(db)
    elif args.do_host:
    result = show_host(db, args.do_host)

  3. DavidWittman created this gist Apr 14, 2014.
    90 changes: 90 additions & 0 deletions mongo-ansible.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/usr/bin/env python
    #
    # MongoDB-backed dynamic inventory script for Ansible
    # http://docs.ansible.com/intro_dynamic_inventory.html

    import os

    from argparse import ArgumentParser

    import pymongo
    from bson import json_util

    MONGO_HOST = "localhost"
    MONGO_PORT = 27017
    MONGO_DB = "ansible"

    MONGO_USER = "ansible"
    MONGO_PASS = "mongogogo"

    def list_hosts(db, query={}):
    """Returns a dict of all the available groups to be managed
    Group documents in Mongo are in the form:
    {
    name: 'foo',
    hosts: ['bar.example.com', 'baz.example.com'],
    vars: {
    foo: 42,
    bar: 'baz'
    },
    children: ['bar']
    }
    """
    result = {}

    cursor = db.groups.find(query, {
    '_id': False,
    'name': True,
    'hosts': True,
    'vars': True,
    'children': True
    })

    for group in cursor:
    result[group['name']] = {
    'hosts': group.get('hosts', []),
    'vars': group.get('vars', {}),
    'children': group.get('children', [])
    }

    return json_util.dumps(result)

    def show_host(db, hostname):
    """Returns a dict containing the variables for a specific host
    Host documents in Mongo are in the form:
    {
    hostname: 'foo.example.com',
    vars: {
    foo: 42,
    bar: 'baz'
    }
    }
    """
    cursor = db.hosts.find_one({'hostname': hostname},
    {'_id': False, 'vars': True})
    return json_util.dumps(cursor.get('vars', {})) if cursor else '{}'

    def main():
    parser = ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--list', dest='do_list', action='store_true')
    group.add_argument('--host', dest='do_host', metavar='<hostname>')
    args = parser.parse_args()

    connection = pymongo.Connection(MONGO_HOST, MONGO_PORT)
    db = connection[MONGO_DB]

    if MONGO_USER and MONGO_PASS:
    db.authenticate(MONGO_USER, MONGO_PASS)

    if args.do_list:
    result = list_hosts(db)
    elif args.do_host:
    result = show_host(db, args.do_host)

    print(result)

    if __name__ == '__main__':
    main()