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.