# ansible 2.0 python api sample code not working

**URL:** <https://forum.ansible.com/t/ansible-2-0-python-api-sample-code-not-working/20985>\
**Category:** Ansible Developer\
**Created:** [March 25, 2016, 4:49am UTC](https://forum.ansible.com/t/ansible-2-0-python-api-sample-code-not-working/20985 "2016-03-25T04:49:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Huan\_He](https://avatars.discourse-cdn.com/v4/letter/h/13edae/32.png) [@Huan\_He](https://forum.ansible.com/u/Huan_He)\
**Post date:** [March 25, 2016, 4:49am UTC](https://forum.ansible.com/t/ansible-2-0-python-api-sample-code-not-working/20985/1 "2016-03-25T04:49:21Z")

</div>

I copied the sample code from ansible website and changed some parameters

from collections import namedtuple  
from ansible.parsing.dataloader import DataLoader  
from ansible.vars import VariableManager  
from ansible.inventory import Inventory  
from ansible.playbook.play import Play  
from ansible.executor.task\_queue\_manager import TaskQueueManager

Options = namedtuple(‘Options’, [‘connection’, ‘module\_path’, ‘forks’, ‘become’, ‘become\_method’, ‘become\_user’, ‘check’])

# initialize needed objects

variable\_manager = VariableManager()  
loader = DataLoader()  
options = Options(connection=‘ssh’, module\_path=‘/home/huhe/ansible/lib/ansible/modules’, forks=100, become=None, become\_method=None, become\_user=None, check=False)  
passwords = dict(vault\_pass=‘secret’)

# create inventory and pass to var manager

inventory = Inventory(loader=loader, variable\_manager=variable\_manager, host\_list=‘/etc/ansible/hosts’)  
variable\_manager.set\_inventory(inventory)

# create play with tasks

play\_source = dict(  
name = “Ansible Play”,  
hosts = ‘linux’,  
gather\_facts = ‘no’,  
tasks = [  
dict(action=dict(module=‘shell’, args=‘ls’), register=‘shell\_out’),  
dict(action=dict(module=‘debug’, args=dict(msg=‘{{shell\_out.stdout}}’)))  
]  
)  
play = Play().load(play\_source, variable\_manager=variable\_manager, loader=loader)

# actually run it

tqm = None  
try:  
tqm = TaskQueueManager(  
inventory=inventory,  
variable\_manager=variable\_manager,  
loader=loader,  
options=options,  
passwords=passwords,  
stdout\_callback=‘default’,  
)  
result = tqm.run(play)  
finally:  
if tqm is not None:  
tqm.cleanup()

when i ran it, i got errors below. Any idea what is wrong?

Traceback (most recent call last):  
File “/home/huhe/workspace/ansible/src/uptime.py”, line 48, in   
result = tqm.run(play)  
File “/home/huhe/ansible/lib/ansible/executor/task\_queue\_manager.py”, line 187, in run  
self.load\_callbacks()  
File “/home/huhe/ansible/lib/ansible/executor/task\_queue\_manager.py”, line 156, in load\_callbacks  
for callback\_plugin in callback\_loader.all(class\_only=True):  
File “/home/huhe/ansible/lib/ansible/plugins/ **init**.py”, line 349, in all  
self.\_module\_cache[path] = self.\_load\_module\_source(name, path)  
File “/home/huhe/ansible/lib/ansible/plugins/ **init**.py”, line 313, in \_load\_module\_source  
module = imp.load\_source(name, path, module\_file)  
File “/home/huhe/ansible/lib/ansible/plugins/callback/slack.py”, line 26, in   
from **main** import cli  
ImportError: cannot import name cli

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 25, 2016, 9:56pm UTC](https://forum.ansible.com/t/ansible-2-0-python-api-sample-code-not-working/20985/2 "2016-03-25T21:56:57Z")

</div>

that should be fixed in current devel

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [December 15, 2016, 6:18pm UTC](https://forum.ansible.com/t/ansible-2-0-python-api-sample-code-not-working/20985/3 "2016-12-15T18:18:40Z")

</div>

Should this be in v2.1.1.0? I get the following error just importing the modules

`  
from ansible.parsing.dataloader import DataLoader\r\nImportError: No module named parsing.dataloader

`
