Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Developing Ansible Dynamic Inventory Script - Nov 2017


Published on

A session about my experience with writing an external inventory script from scratch for "Netbox" (IPAM and DCIM tool from DigitalOcean network engineering team) and push it to upstream to became an official inventory script.


The "Dynamic inventory" is one of nice features in Ansible, where you can use an external service as inventory for Ansible instead the basic text-based ini file. So you can use AWS EC2 as inventory of your hosts, or maybe OpenStack, or whatever ... you actually can use any source inventory for Ansible, and you can write your own "External Inventory Script".

Published in: Technology
  • Be the first to comment

Developing Ansible Dynamic Inventory Script - Nov 2017

  1. 1. Developing Ansible Dynamic Inventory Script Ahmed AbouZaid @aabouzaid
  2. 2. About Ahmed AbouZaid A passionate DevOps Engineer, author and Free/Open source geek who loves the community. Automation, data, and metrics are my preferred areas. I have a built-in monitoring chip, and too lazy to do anything manually :D Blog | Github | Twitter
  3. 3. Intro ● Ansible inventory. ● Ansible dynamic inventory. ● Write your own inventory script. ● Netbox external inventory script. ● Tips and tricks.
  4. 4. Ansible inventory ● Static file (default is INI-like, but could be YAML too). ● Define hosts, groups, and vars. ● Could be a directory has multiple inventory sources. ● Has many limitations and hard to manage.
  5. 5. Ansible dynamic inventory ● Dynamic data source (obviously). ● External data source like EC2 or OpenStack or whatever. ● Almost any source could be a dynamic inventory for ansible.
  6. 6. Ansible dynamic inventory script ● Scripts written in any programming language. ● Should return data in JSON format. ● Use a convention (”--list” to get all hosts, and “--host” to get one host). ● Example: ansible -i all -m ping
  7. 7. Netbox dynamic inventory script ● The script gets hosts lists via Netbox API as a dynamic data source. ● Netbox is an IPAM and DCIM tool from DigitalOcean network engineering team, which serves as a "Source of Truth". ● I opened the PR on Feb 2017 and approved on Oct 2017. ● It has been reviewed by Ansible community (6 peoples were involved). ● Repos:
  8. 8. Related to script stuff: ● YAML or INI config file. ● Must be PEP8 compliant. ● Support Python 2.6/2.7 and 3.5/3.6. ● Avoid code smells (Ansible sanity, online services). ● Write unit test (e.g. pytest). Tips & Tricks
  9. 9. Tips & Tricks Around script stuff: ● Create its own repository. ● Create a file. ● Push it to PyPI (so it could be used with pip). ● Use CI to run tests (e.g. Travis CI).
  10. 10. Resources ● ● ●
  11. 11. Questions? Thanks :-)