Giter VIP home page Giter VIP logo

ansible-dynamic-inventory's Introduction

ansible-dynamic-inventory

Do you have Bare Metal or virtualized platform without a good API? If so, would you like to use a dynamic inventory like it was AWS, Azure or OpenStack?

This project provides a dynamic inventory interface that allows you to simplify your static management of the hosts file and also manage your legacy platform inventory like it was a cloud provider.

Setup

  1. Copy src/inventory.py file into your inventory folder in Ansible
  2. Copy the src/hosts.yml sample into your inventory folder, at the same level of the inventory.py, and customize it to your needs.
  3. Configure Ansible to use the inventory.py (an example through ansible.cfg):
    [defaults]
    hostfile = ./inventory/inventory.py
    

Usage

To verify that it works:

$ chmod u+x inventory.py
$ python inventory.py --list

To take advance of this inventory script you will need to generate your hosts.yml according to some patterns. First of all, you don't want to generate all the possible groups of your machines manually. For that we use tags:

node-1:
  ansible_ssh_host: node-1.example.com
  tags:
    - development
    - node
node-2:
  ansible_ssh_host: node-2.example.com
  tags:
    - production
    - node

Given that list of hosts the inventory script will generate all the possible groups based on the tags:

{
  'development': ['node-1.example.com'],
  'production': ['node-2.example.com'],
  'node': ['node-1.example.com', 'node-2.example.com']
}

That will allow you to run your playbook by using intersections without taking the effort of thinking of all possible combinations of tags:

$ ansible-playbook --limit 'node:&development' myplaybook.yml
$ ansible-playbook --limit 'node:&production' myplaybook.yml
$ ansible-playbook --limit 'node' myplaybook.yml

Or alternatively within your play (as an example):

---
- name: foo
  hosts: "node:&{{ env }}"

  roles:
    - role: foo-role
$ ansible-playbook -e 'env=production' myplaybook.yml

If you want to include host variables you have to add a key to the host properties:

node-1:
  ansible_ssh_host: node-1.example.com
  tags:
    - development
    - node
  hostvars:
    attr_1: value1
    attr_2:
      - value2
      - value3
    attr_3:
      something: value4

Ultimately, as you still don't have an API that can provide you with an organized result like this, you will have to maintain a simple list of tagged nodes, which is better than an unorganized and full of duplication plain hosts file. All the possible combinations are handled by the inventory script and returned to Ansible.

Developer considerations:

The dynamic inventory script comes with a tests that can be run:

$ cd src/
$ python -m unittest inventory_test

ansible-dynamic-inventory's People

Contributors

sermilrod avatar

Watchers

James Cloos avatar  avatar

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.