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.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4e6fcd08-586f-4ab7-96b3-1377e9346f0b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to