[英]how to repeatedly call a function with a list of paired arguments from YAML file to Python using AWS SDK?
I want to write a script which takes AWS account's values from YAML file using python boto 3 and create multiple accounts under AWS organization. 我想编写一个脚本,该脚本使用python boto 3从YAML文件中获取AWS账户的值,并在AWS组织下创建多个账户。 Please find the below steps which I want to execute: step 1 : I have list of AWS account's values in YAML file as below:(config.yaml)
请找到我要执行的以下步骤: 步骤1:我在YAML文件中具有以下AWS账户值列表:(config.yaml)
Name:
test1
test2
Email:
test1@gmail.com
test2@gmail.com
step 2 : write a python script to automate the process 第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'])
Can anyone help ,how can I load YAML values in below code ? 谁能帮忙,如何在下面的代码中加载YAML值?
I can create only one account using below simple script: 我只能使用以下简单脚本创建一个帐户:
import json import boto3 client = boto3.client('organizations') response = client.create_account( Email="test1@gmail.com", AccountName= "Test1" )
The way I see it, your config file does not look right. 从我的角度来看,您的配置文件看起来不正确。 Having two "parallel" lists is rarely a good idea (I suppose this was your intention, even if the dashes are missing).
拥有两个“平行”列表很少是一个好主意(我想这是您的意图,即使没有破折号也是如此)。 I would give it this structure:
我会给它这样的结构:
accounts:
- name: test1
email: test1@gmail.com
- name: test2
email: test2@gmail.com
and read it in a way similar to this: 并以类似以下方式阅读:
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'])
Maybe you need to do something like this? 也许您需要做这样的事情?
# ... for account in accounts: response = client.create_account( AccountName = account['name'], Email = account['email'])
(what an unpythonic naming convention boto3 has!) (boto3具有非Python的命名约定!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.