# Running ansible module from python (Cannot use remote\_user defined globally)

**URL:** <https://forum.ansible.com/t/running-ansible-module-from-python-cannot-use-remote-user-defined-globally/22465>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [September 2, 2016, 11:25am UTC](https://forum.ansible.com/t/running-ansible-module-from-python-cannot-use-remote-user-defined-globally/22465 "2016-09-02T11:25:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 2, 2016, 11:25am UTC](https://forum.ansible.com/t/running-ansible-module-from-python-cannot-use-remote-user-defined-globally/22465/1 "2016-09-02T11:25:13Z")

</div>

When I am trying to run an ansible module from Python API (AWS EC2 instances are my target machines), the private\_key\_file is read from /etc/ansible/ansible.cfg but I have to pass remote\_user in Options for running the module. (I have not set environment variable ANSIBLE\_CONFIG)  
Here is my python:  
…  
Options = namedtuple(‘Options’, [‘connection’,’remote\_user’, ‘module\_path’, ‘forks’, ‘become’, ‘become\_method’, ‘become\_user’, ‘check’, ‘verbosity’])  
options = Options(connection=’ssh’, remote\_user=’centos’, module\_path=’./library’, forks=100, become=None, become\_method=None, become\_user=None, check=False, verbosity=None)  
…

# create inventory and pass to var manager

inventory = Inventory(loader=loader, variable\_manager=variable\_manager, host\_list=’inventory/ec2.py’)  
variable\_manager.set\_inventory(inventory)

# create play with tasks

play\_source = dict(  
name = “Ansible Play”,  
hosts = ‘all’,  
gather\_facts = ‘no’,  
tasks = [  
dict(action=dict(module=’running\_services’, args=dict(deploy\_dir=”/home/centos/test/”)), register=’running\_services’)  
]  
)  
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=results\_callback #Custom callback  
)  
result = tqm.run(play)  
finally:  
if tqm is not None:  
tqm.cleanup()

I am failed to understand why it takes key from global ansible.cfg and not remote\_host? I do not want to pass remote\_user in code and want to use it from /etc/ansible/ansible.cfg

/etc/ansible/ansible.cfg:  
…  
[defaults]  
host\_key\_checking = False  
remote\_user = centos  
private\_key\_file = ~/.ssh/key.pem

P.S. Even if I export ANSIBLE\_CONFIG, it doesn’t make any difference.  
Any help would be appreciated.
