繁体   English   中英

如何使用AWS开发工具包从YAML文件到Python重复调用带有成对参数列表的函数?

[英]how to repeatedly call a function with a list of paired arguments from YAML file to Python using AWS SDK?

我想编写一个脚本,该脚本使用python boto 3从YAML文件中获取AWS账户的值,并在AWS组织下创建多个账户。 请找到我要执行的以下步骤: 步骤1:我在YAML文件中具有以下AWS账户值列表:(config.yaml)

Name:
   test1
   test2
Email:
    test1@gmail.com
    test2@gmail.com

第2步:编写python脚本以自动执行该过程

import yaml

with open("config.yml", 'r') as ymlfile:
    account = yaml.safe_load(ymlfile)

for section in cfg:
    print(section)
    print(account['Name'])
    print(account['Email'])
  • Pyhon对我来说是新的...我尝试使用以上代码从文件中加载值,但仅打印值
  • 谁能帮忙,如何在下面的代码中加载YAML值?

    • 我只能使用以下简单脚本创建一个帐户:

       import json import boto3 client = boto3.client('organizations') response = client.create_account( Email="test1@gmail.com", AccountName= "Test1" ) 

从我的角度来看,您的配置文件看起来不正确。 拥有两个“平行”列表很少是一个好主意(我想这是您的意图,即使没有破折号也是如此)。 我会给它这样的结构:

accounts:
- name: test1
  email: test1@gmail.com
- name: test2
  email: test2@gmail.com

并以类似以下方式阅读:

import yaml

with open("config.yml", 'r') as ymlfile:
    config = yaml.safe_load(ymlfile)
accounts = config['accounts']
for account in accounts:
    print()
    print(account['name'])
    print(account['email'])


UPDATE

也许您需要做这样的事情?

 # ... for account in accounts: response = client.create_account( AccountName = account['name'], Email = account['email']) 

(boto3具有非Python的命名约定!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM