# Trying to use the Python modules to integrate in my deployment scripts.  MODULE FAILURE

**URL:** <https://forum.ansible.com/t/trying-to-use-the-python-modules-to-integrate-in-my-deployment-scripts-module-failure/26711>\
**Category:** Ansible Project\
**Created:** [February 12, 2018, 9:20pm UTC](https://forum.ansible.com/t/trying-to-use-the-python-modules-to-integrate-in-my-deployment-scripts-module-failure/26711 "2018-02-12T21:20:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_H](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@David\_H](https://forum.ansible.com/u/David_H)\
**Post date:** [February 12, 2018, 9:20pm UTC](https://forum.ansible.com/t/trying-to-use-the-python-modules-to-integrate-in-my-deployment-scripts-module-failure/26711/1 "2018-02-12T21:20:02Z")

</div>

Hi,  
I have been using ansible for a while with the yml’s and love the simplicity of the tool.  
Now, I have used a snippet of a script to try run various ansible tasks on a host via Python.

Would one of you be able to point me in the right direction to get the script working? I copied it off another site and adjusted. Ultimatly, I would like to send it some VARS instead.  
If you have a better way of running the script, it would be great also as I feel this is much longer then asking python to run the ansible executable…  
Thanks  
D

I believe the SSH works well as when I run the script :

_$ ansible -i “192.168.47.157,” all -m ping -become --connection=ssh --user=bobvilla --ask-become-pass_  
_SUDO password:_  
_192.168.47.157 | SUCCESS =\> {_  
_“changed”: false,_  
_“ping”: “pong”_  
_}_

When I run my script, it throws this error…

_$ ./runner.py_  
_UP \*\*\*\*\*\*\*\*\*\*\*_  
_FAILED \*\*\*\*\*\*\*_  
_192.168.47.183 \>\>\> MODULE FAILURE_  
_192.168.47.157 \>\>\> MODULE FAILURE_  
_DOWN \*\*\*\*\*\*\*\*\*_

Here are a few details on my env.

_$ python --version_  
_Python 2.7.12_  
_$ python3 --version_  
_Python 3.5.2_  
_$ ansible --version_  
_ansible 2.4.3.0_  
_config file = None_  
_configured module search path = [‘/home/dave/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]_  
_ansible python module location = /usr/local/lib/python3.5/dist-packages/ansible_  
_executable location = /usr/local/bin/ansible_  
_python version = 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]_

Script Bellow.

_#!/usr/bin/env python3_  
_# -- coding: utf-8 --_

_from collections import namedtuple_  
_from ansible.executor.task\_queue\_manager import TaskQueueManager_  
_from ansible.inventory.manager import InventoryManager_  
_from ansible.parsing.dataloader import DataLoader_  
_from ansible.playbook.play import Play_  
_from ansible.plugins.callback import CallbackBase_  
_from ansible.vars.manager import VariableManager_

_# Create a callback object so we can capture the output_  
_class ResultsCollector(CallbackBase):_

_def **init** (self, \*args, \*\*kwargs):_  
_super(ResultsCollector, self). **init** (\*args, \*\*kwargs)_  
_self.host\_ok = {}_  
_self.host\_unreachable = {}_  
_self.host\_failed = {}_

_def v2\_runner\_on\_unreachable(self, result):_  
_self.host\_unreachable[result.\_host.get\_name()] = result_

_def v2\_runner\_on\_ok(self, result, \*args, \*\*kwargs):_  
_self.host\_ok[result.\_host.get\_name()] = result_

_def v2\_runner\_on\_failed(self, result, \*args, \*\*kwargs):_  
_self.host\_failed[result.\_host.get\_name()] = result_

_def main():_  
_host\_list = [‘192.168.47.157’,‘192.168.47.183’]_  
_Options = namedtuple(‘Options’, [‘connection’,_  
_‘module\_path’,_  
_‘forks’,_  
_‘remote\_user’,_  
_#‘private\_key\_file’,_  
_#‘ssh\_common\_args’,_  
_#‘ssh\_extra\_args’,_  
_#‘sftp\_extra\_args’,_  
_#‘scp\_extra\_args’,_  
_‘ansible\_user’,_  
_‘become’,_  
_‘become\_method’,_  
_‘become\_user’,_  
_‘become\_pass’,_  
_‘verbosity’,_  
_‘check’,_  
_‘diff’])_  
_# required for_  
_# [https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L204](https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L204)_  
_sources = ‘,’.join(host\_list)_  
_if len(host\_list) == 1:_  
_sources += ‘,’_

_# initialize needed objects_  
_loader = DataLoader()_  
_options = Options(connection=‘ssh’,_  
_#module\_path=‘/usr/local/lib/python3.5/dist-packages/ansible/modules’,_  
_module\_path=‘’,_  
_forks=100,_  
_remote\_user=‘bobvilla’,_  
_#private\_key\_file=None,_  
_#ssh\_common\_args=None,_  
_#ssh\_extra\_args=None,_  
_#sftp\_extra\_args=None,_  
_#scp\_extra\_args=None,_  
_ansible\_user=‘nikita’,_  
_become=‘yes’,_  
_become\_method=‘sudo’,_  
_become\_user=‘root’,_  
_become\_pass=‘password’,_  
_verbosity=3,_  
_check=False,_  
_diff=False)_

_passwords = dict()_

_# create inventory and pass to var manager_  
_inventory = InventoryManager(loader=loader, sources=sources)_  
_variable\_manager = VariableManager(loader=loader, inventory=inventory)_

_# create play with tasks_  
_play\_source = dict(_  
_name=“Ansible Play”,_  
_hosts=host\_list,_  
_gather\_facts=‘yes’,_  
_#tasks=[dict(action=dict(module=‘command’, args=dict(cmd=‘tail -10 /var/log/syslog’)))]_  
_tasks=[dict(action=dict(module=‘ping’))]_  
_)_  
_play = Play().load(play\_source, variable\_manager=variable\_manager, loader=loader)_

_# actually run it_  
_tqm = None_  
_callback = ResultsCollector()_  
_try:_  
_tqm = TaskQueueManager(_  
_inventory=inventory,_  
_variable\_manager=variable\_manager,_  
_loader=loader,_  
_options=options,_  
_passwords=passwords,_  
_)_  
_tqm.\_stdout\_callback = callback_  
_result = tqm.run(play)_  
_finally:_  
_if tqm is not None:_  
_tqm.cleanup()_

_print(“UP \*\*\*\*\*\*\*\*\*\*\*”)_  
_for host, result in callback.host\_ok.items():_  
_print(‘{0} \>\>\> {1}’.format(host, result.\_result[‘stdout’]))_

_print(“FAILED \*\*\*\*\*\*\*”)_  
_for host, result in callback.host\_failed.items():_  
_print(‘{0} \>\>\> {1}’.format(host, result.\_result[‘msg’]))_

_print(“DOWN \*\*\*\*\*\*\*\*\*”)_  
_for host, result in callback.host\_unreachable.items():_  
_print(‘{0} \>\>\> {1}’.format(host, result.\_result[‘msg’]))_

_if **name** == ‘ **main** ’:_  
_main()_

---

<div class="post-metadata">

**Author:** ![David\_H](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@David\_H](https://forum.ansible.com/u/David_H)\
**Post date:** [February 12, 2018, 11:04pm UTC](https://forum.ansible.com/t/trying-to-use-the-python-modules-to-integrate-in-my-deployment-scripts-module-failure/26711/2 "2018-02-12T23:04:34Z")

</div>

Well, I answered my own question on the train. Hope this silly mistake can help someone else later.

_become\_user=‘root’, \>_ _become\_user=‘bobvilla’,_

_On the other hand, I believe i found a slight bug._  
_When I run ping test, I get this error._

_./runner.py_  
_UP \*\*\*\*\*\*\*\*\*\*\*_  
_Traceback (most recent call last):_  
_File “./runner.py”, line 133, in_   
_main()_  
_File “./runner.py”, line 122, in main_  
_print(‘{0} \>\>\> {1}’.format(host, result.\_result[‘stdout’]))_  
_KeyError: ‘stdout’_

I guess stdout doesn’t exist on the ping function.  
As for the other functions like ‘command’, it works just fine and outputs the information.

Cheers.  
D
