项目作者: sermilrod

项目描述 :
Ansible dynamic inventory interface from an static hosts file
高级语言: Python
项目地址: git://github.com/sermilrod/ansible-dynamic-inventory.git
创建时间: 2017-08-22T10:42:45Z
项目社区:https://github.com/sermilrod/ansible-dynamic-inventory

开源协议:MIT License

下载


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):
    1. [defaults]
    2. hostfile = ./inventory/inventory.py

    Usage

To verify that it works:

  1. $ chmod u+x inventory.py
  2. $ 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:

  1. node-1:
  2. ansible_ssh_host: node-1.example.com
  3. tags:
  4. - development
  5. - node
  6. node-2:
  7. ansible_ssh_host: node-2.example.com
  8. tags:
  9. - production
  10. - node

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

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

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

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

Or alternatively within your play (as an example):

  1. ---
  2. - name: foo
  3. hosts: "node:&{{ env }}"
  4. roles:
  5. - role: foo-role
  1. $ ansible-playbook -e 'env=production' myplaybook.yml

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

  1. node-1:
  2. ansible_ssh_host: node-1.example.com
  3. tags:
  4. - development
  5. - node
  6. hostvars:
  7. attr_1: value1
  8. attr_2:
  9. - value2
  10. - value3
  11. attr_3:
  12. 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:

  1. $ cd src/
  2. $ python -m unittest inventory_test