我目前有一个包含 python 脚本和 azure 函数文件的 python 项目。这是项目的结构:
├── 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
蟒蛇脚本:
#!/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()
Azure 函数初始化.py 文件:
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")
整个项目使用azure管道进行部署,成功完成了构建和发布管道。但是,脚本无法执行。我已经让脚本独立执行以确认它有效。
我的困惑是如何在 Azure 函数中配置 playbook.yml 和 hosts 文件的路径。我无法在 azure 函数中调试以查看任何可能有用的错误。
如何在 Azure 函数中设置此路径?以下路径显然在本地工作:
private_key_file='../config/env/ssh_key'
inventory = InventoryManager(loader=loader, sources='hosts')
playbook_path = 'playbook.yml'
我对 Azure 功能很陌生,但我相信正确设置路径可能是解决此问题的方法。我不确定我错过了什么。
我已使用以下链接作为参考: https ://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
https://ansible-runner.readthedocs.io/en/latest/python_interface/