Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pyventory for Ansible

Pyventory for Ansible

A brief "dive in" for Pyventory project — Ansible Inventory implementation which uses Python syntax.

Serge Matveenko

December 12, 2017
Tweet

More Decks by Serge Matveenko

Other Decks in Programming

Transcript

  1. A classic Ansible Inventory • .ini files for groups and

    hosts (YAML is also possible now) • .yml files for groups configuration all in one dir • .yml files for hosts configuration all in another dir • Messy inheritance • Messy mixins • No cross-referencing
  2. A classic Ansible Inventory [atlanta] host1 host2 [raleigh] host2 host3

    [southeast:children] atlanta raleigh [southeast:vars] some_server=foo.southeast.example.com halon_system_timeout=30 self_destruct_countdown=60 escape_pods=2 [usa:children] southeast northeast southwest northwest all: children: usa: children: southeast: children: atlanta: hosts: host1: host2: raleigh: hosts: host2: host3: vars: some_server: foo.southeast.example.com halon_system_timeout: 30 self_destruct_countdown: 60 escape_pods: 2 northeast: northwest: southwest:
  3. Requirements • Multiple inheritance & mixins • Required attributes •

    Calculated attributes • Cross-references between assets • Easy to extend • Autocomplete in IDE
  4. Pyventory #!/usr/bin/env python from pyventory import Asset, ansible_inventory class All(Asset):

    run_tests = False use_redis = False redis_host = 'localhost' minify = False version = 'develop' class Staging(All): run_tests = True staging = Staging() ansible_inventory(locals())
  5. More Pyventory #!/usr/bin/env python from pyventory import ansible_inventory from inventory

    import * develop = DevelopHost() develop_sidebranch = DevelopHost( ansible_host='sidebranch_hostname', version='sidebranch_name') staging = StagingHost() prod_backend1 = ProdBackEnd(num=1) # app001.prod.dom prod_backend2 = ProdBackEnd(num=2) # app002.prod.dom prod_frontend1 = ProdFrontEnd(num=1) # www001.prod.dom prod_frontend2 = ProdFrontEnd(num=2) # www002.prod.dom ansible_inventory(locals(), indent=4) class DevelopHost(Staging, BackEnd, FrontEnd): ansible_host = 'develop_hostname' version = 'develop' class StagingHost(Staging, BackEnd, FrontEnd): ansible_host = 'master_hostname' version = 'master' class ProdBackEnd(Production, BackEnd): redis_host = 'prod_redis_hostname' ansible_hostname = 'app{num:03}.prod.dom' class ProdFrontEnd(Production, FrontEnd): ansible_hostname = 'www{num:03}.prod.dom'