简体   繁体   中英

Unable to parse yaml file in python

I am trying to parse the below yaml file, tried using different yaml parsers but i'm unable to parse the yaml file.Is there way to parse this yaml file using any other python library.

policies:
  - name: test
    resource: ec2
    mode:
      type: periodic
      schedule: rate(1 day)
      role: arn:aws:iam::{account_id}:role/role_name
      tags:
        Name: ${tag_name}
        Project: ${tag_project}
    filters:
      - and:
        - type: value
          key: testname
          value: "${aws_account_id}"
        - type: value
          whitelist: %{ for acct_id in split (",", resource_list) }
            - "${acct_id}" %{ endfor }

python script:

import glob
from pathlib import Path
import ruamel.yaml

def print_table(filepath):
 file_name = Path(filepath)
 yaml = ruamel.yaml.YAML()
 data = yaml.load(file_name)
 name = data['name']
 print(name)

if __name__ == "__main__":
    print_table('test.yaml')
    

Error:

ruamel.yaml.scanner.ScannerError: while scanning for the next token
found character '%' that cannot start any token
  in "test.yaml", line 17, column 26

Just use ${{each in }} .

policies:
  - name: test
    resource: ec2
    mode:
      type: periodic
      schedule: rate(1 day)
      role: arn:aws:iam::{account_id}:role/role_name
      tags:
        Name: ${tag_name}
        Project: ${tag_project}
    filters:
      - and:
        - type: value
          key: testname
          value: "${aws_account_id}"
        - type: value
          whitelist:
              ${{each acct_id in split (",", resource_list)}} - ${{acct_id}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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