簡體   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