I currently have a python project that contains python script, and azure function files. This is the structure of the project:
├── ansible-deploy-pipelines.yml
├── ansibleazurefunc
│ ├── __init__.py
│ ├── function.json
│ └── sample.dat
├── config
│ ├── env
│ │ ├── envvars
│ │ ├── extravars
│ │ ├── passwords
│ │ ├── settings
│ │ └── ssh_key
│ ├── inventory
│ │ └── hosts
│ └── project
│ ├── roles
│ │ └── linux-vms
│ │ ├── README.md
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ └── vars
│ │ └── main.yml
│ └── test.yml
├── host.json
├── local.settings.json
├── proxies.json
├── requirements.txt
└── scripts
├── deploy_config.py
├── hosts
└── playbook.yml
The python script:
#!/usr/bin/env python3
import os
import sys
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.executor.playbook_executor import PlaybookExecutor
loader = DataLoader()
def main()
inventory = InventoryManager(loader=loader, sources='hosts')
variable_manager = VariableManager(loader=loader, inventory=inventory)
playbook_path = 'playbook.yml'
if not os.path.exists(playbook_path):
print("[INFO] The playbook does not exist")
sys.exit()
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check','diff'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='devadmin', private_key_file='../config/env/ssh_key', ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method='sudo', become_user='root', verbosity=None, check=False, diff=False)
passwords = {}
tp = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
results = tp.run()
if __name__ == '__main__':
main()
The Azure function init.py file:
import logging
import json
import azure.functions as func
from scripts import deploy_config
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python script for calling Ansible playbook.')
deploy_config.main()
return func.HttpResponse(f"Ansible deploy - Completed")
The entire project is deployed using azure pipelines, which successfully complete the build and release pipelines. However, the script fails to execute. I have had the script executed standalone to confirm it works.
The confusion for me is how to configure the path to the playbook.yml and hosts files within Azure function. I am unable to debug within azure functions to see any possible errors that might be helpful.
How do I set this path within Azure functions? The following paths obviously work locally:
private_key_file='../config/env/ssh_key'
inventory = InventoryManager(loader=loader, sources='hosts')
playbook_path = 'playbook.yml'
I'm quite new to Azure functions, but I believe setting the path correctly might be the solution to this issue. I am not sure what I'm missing.
I have used the following links for references: https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
https://ansible-runner.readthedocs.io/en/latest/python_interface/
osorsubprocess)? Please pay attention to the notes on top of the ansible api doc page:This API is intended for internal Ansible use. Ansible may make changes to this API at any time that could break backward compatibility with older versions of the API. Because of this, external use is not supported by Ansible. If you want to use Python API only for executing playbooks or modules, consider ansible-runner first.If you want to use Python API only for executing playbooks or modules, consider ansible-runner firston the API doc!