Do you have an example for use a playbook with Python API ?

936 views
Skip to first unread message

Bruno Selva

unread,
Jul 3, 2019, 8:07:03 PM7/3/19
to Ansible Project
I am trying hard to create a simple script to execute a playbook with python

from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.executor.playbook_executor import PlaybookExecutor

loader = DataLoader()

inventory = InventoryManager(loader=loader, sources="localhost,")
variable_manager = VariableManager(loader=loader, inventory=inventory)
passwords={}

playbook = PlaybookExecutor(playbooks=['test.yml'],inventory=inventory,variable_manager=variable_manager,loader=loader,passwords=passwords)
playbook.run()

This is simple, but keep saying error:

Traceback (most recent call last):
  File "ansible_play.py", line 13, in <module>
    playbook.run()
  File "/Users/brunoselva/.local/share/virtualenvs/devops-lambda-sY4jyCvw/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 146, in run
    if context.CLIARGS['syntax']:
  File "/Users/brunoselva/.local/share/virtualenvs/devops-lambda-sY4jyCvw/lib/python2.7/site-packages/ansible/module_utils/common/collections.py", line 20, in __getitem__
    return self._store[key]
KeyError: 'syntax'

Someone knows what I am doing wrong ?

Brian Coca

unread,
Jul 5, 2019, 9:47:38 AM7/5/19
to Ansible Project
https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html

you are not setting required options, see the comment and line on how
to set context.CLIARGS


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages