使用Python执行Azure CLI命令的接口
az.cli的Python项目详细描述
亚利桑那州在
Pythonazure.cli.core接口,在Python中执行az
Azure CLI命令。在
该方法返回一个命名元组AzResult = namedtuple('AzResult', ['exit_code', 'result_dict', 'log'])
。0==成功的^{result_dict
。失败时(error_code
>;0)在log
中以字符串形式显示日志消息。在
使用
安装程序包
pip install az.cli
使用az login
登录,请参见sign in using a service principal。在
在幕后,包使用~/.azure文件夹来持久化和检索配置。在
示例
^{pr2}$交互式
您可以交互式地运行该命令来遍历字典。
导航到src
并运行python3
。
导入库from az.cli import az
,并通过执行方法az("<my command>")
来调用Azure CLI来运行任何命令。在
# cd src# python3fromaz.cliimportaz# on Success, the `error_code` is 0 and the result_dict contains the outputaz("group list")# list return tuple (exit_code, result_dict, log)az("group list")[0]# 0az("group list")[1]# print result_dictaz("group list")[1][0]['id']# enumerate the id of the first element in dictionary# On Error, the `error_code` will be != 1 and the log is presentaz("group show -n does-not-exsist")# list return tuple (exit_code, result_dict, log)az("group show -n does-not-exsist")[0]# 3az("group show -n does-not-exsist")[2]# print the log
建造
本地开发
- 安装python3
- 使用
make init
安装REQUIREMENTS.txt
我建议使用Python3 virtual environments。在
# sets up environment make env # installs requirements make init
Docker
要构建映像,请按顺序运行以下操作。在
# Runs docker build & create
make create
运行
创建容器后,您可以交互式地运行az.cli
。在
# Run docker run
make run
- 项目
标签: