简体   繁体   English

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

[英]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'])
  • Pyhon is new for me...I tried with above code to load value from file but it prints only values Pyhon对我来说是新的...我尝试使用以上代码从文件中加载值,但仅打印值
  • 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'])


UPDATE UPDATE

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.

相关问题 Python:如何使用一系列参数重复调用函数? - Python: How to call a function repeatedly with a range of arguments? 配对参数的调用函数 - Call function for paired arguments 如何从使用python解析的yaml文件中调用和迭代值? - How to call and iterate values from a yaml file parsed using python? Python 3:如何从另一个文件调用函数并将参数传递给该函数? - Python 3: How to call function from another file and pass arguments to that function ? 如何重复将参数传递给python文件 - How to repeatedly pass arguments to a python file 如何在不为键定义特定值的情况下使用变量重复编辑 python 中的 YAML 文件? - How to edit a YAML file in python repeatedly using a variable without defining a specific value for the key? 读取 python dict 或 yaml 并作为带有参数和对象的 python 函数调用 - Read python dict or yaml and call as python function with arguments and objects 如何使用 function 调用列表中的 function arguments - How to use function arguments from list for function call 如何从python中的yaml文件加入列表 - How to join list from th yaml file in python 如何调用 function 和 arguments 在 ZA7F5F35426B9237411FCZ2317B 中使用另一个 function - How to call a function with arguments using another function in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM