Python integration

Python has good integration with YAML and Jinja2 through pre-built libraries.

Taking the same example (of creating configurations for each of the hosts), here is how we call the playbook from Python:

-sh-4.2$ more checkpython.py#import librariesimport jsonimport sysfrom collections import namedtuplefrom ansible.parsing.dataloader import DataLoaderfrom ansible.vars.manager import VariableManagerfrom ansible.inventory.manager import InventoryManagerfrom ansible.playbook.play import Playfrom ansible.executor.playbook_executor import PlaybookExecutordef ansible_part():    playbook_path = "checktemplate.yml"    inventory_path = "hosts" Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', ...

Get Practical Network Automation - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.