Coder Social home page Coder Social logo

ansible-dns-inventory's Introduction

Ansible DNS dynamic inventory script

Overview

This Python script, based upon Remie Bolte's Node.js script to the same purpose, generates a dynamic inventory from specially formatted DNS TXT records. Output is in JSON.

It works by querying the specified domain for any TXT records matching two types of strings. The first specifies a hostname and any groups that host belongs to, using the following format:

 "hostname=tomcat01.example.com;groups=tomcat,webserver,texas"

Hosts without any specified groups will be added to the "ungrouped" group

The second string specifies any group_vars for a given group:

"group=webserver;vars=foo_var:foo,bar_var:bar"

You can also use subgroups (children):

"group=dbservers;children=mysqlservers,pgservers,oraservers"

And a subgroup (child) can have a group variable too:

"group=pgservers;vars=foo_var:foo,bar_var:bar"

You can optionally specify host_vars on a hostname line, like so:

 "hostname=mysql.example.com;hostvars=foo_var:foo,bar_var:bar"
 "hostname=lab3.example.com;groups=lab;hostvars=foo_var:foo"

If you want a variable to be an array of values:

"group=webserver;vars=foo_var:[bar1|bar2|bar3]"

Output might look something like this:

{
    "_meta": {
        "hostvars": {
            "mysql.example.com": {
                "foo_var": "foo",
                "bar_var": "bar"
            }
        }
    },
    "tomcat": {
        "hosts": [
            "tomcat01.example.com",
            "tomcat02.ptsteams.lab"
        ]
    },
    "lab": {
        "hosts": [
            "lab1.example.com"
            "lab2.example.com"
            "lab3.example.com"
        ],
        "vars": {
            "ansible_port": "22",
            "ansible_user": "matt"
        }
    },
    "ungrouped": {
        "hosts": [
            "mysql.example.com"
        ]
    },
    "webservers": {
        "hosts": [
            "webserver1.example.com"
            "webserver2.example.com"
        ]
    }
}

Some things to keep in mind:

  1. In an inventory, host_vars take precedence over group_vars.

  2. Child group_vars take precedence over group_vars (and are not merged)

  3. Strings in TXT records are limited to 255 characters, but an individual record can be composed of multiple strings enclosed in double quotation marks and separated by a space. Per RFC 4408 and RFC 1035, this script treats multiple strings as if they are concatenated together. So a TXT record like

      "group=db;vars=ansible_port:22" ",bar_var:bar"
    

    will be read as

    group=db;vars=ansible_port:22,bar_var:bar
    
  4. DNS propagation can take time, as determined by a record's TTL value.

  5. Do not to list sensitive information in TXT records.

  6. You can get a listing of TXT records with: dig +short -t TXT example.com

ansible-dns-inventory's People

Contributors

lo78cn avatar mattkeeler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ansible-dns-inventory's Issues

seems very useful cannot run it

Hi,
this seems very useful, unfortunatelly I cannot run it, Tried on a MAC OS

import dns.resolver ImportError: No module named resolver

pip show dnspython Name: dnspython Version: 1.15.0 Summary: DNS toolkit Home-page: http://www.dnspython.org Author: Bob Halley Author-email: [email protected] License: BSD-like Location: /usr/local/lib/python2.7/site-packages Requires:

and on a CentOS 7.1

`
Installed /usr/lib/python2.7/site-packages/dnspython-1.16.0-py2.7.egg

python dns.py
Traceback (most recent call last):
File "dns.py", line 41, in
import dns.resolver
File "/root/ansible-dns-inventory/dns.py", line 41, in
import dns.resolver
ImportError: No module named resolver
`
Any hint on how to load the library?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.