본문 바로가기

Python/Default

yaml 파일 요소 뽑아내기

import yaml
import pprint

def print_instance(filename):

    with open(filename) as f:
        conf = yaml.load(f)
    temp = conf['instance_groups']
    instance = []
    for i in temp:
        deployments = []
        deployments.extend([i['name'], i['instances'], i['vm_type']])
        instance.append(deployments)
    return instance

filename = 'D:\deployment\paasta-deployment\paasta-deployment.yml'
pprint.pprint(print_instance(filename))

paas-ta vm instance 뽑아내야하는데 더럽게 많고 deployments 오지게 복잡하게 되있어서 걍 코드 짰다.

- cp949 에러나면 encoding UTF-8로 해준다

아래처럼 생긴 yml 파일을 넣으면

instance_groups:
- name: smoke-tests
  lifecycle: errand
  azs:
  - z1
  instances: 1
  vm_type: minimal
  stemcell: default
  update:
    serial: true
  networks:
  - name: default
  jobs:
  - name: smoke_tests
    release: cf-smoke-tests
    properties:
      bpm:
        enabled: true
      smoke_tests:
        api: "https://api.((system_domain))"
        apps_domain: "((system_domain))"
        client: cf_smoke_tests
        client_secret: "((uaa_clients_cf_smoke_tests_secret))"
        org: cf_smoke_tests_org
        space: cf_smoke_tests_space
        cf_dial_timeout_in_seconds: 300
        skip_ssl_validation: true
  - name: cf-cli-6-linux
    release: cf-cli

이렇게 나온다.

[['smoke-tests', 1, 'minimal'],
 ['nats', 2, 'minimal'],
 ['adapter', 2, 'minimal'],
 ['database', 1, 'small'],
 ['diego-api', 2, 'small'],
 ['uaa', 2, 'small'],
 ['singleton-blobstore', 1, 'small'],
 ['api', 2, 'small'],
 ['cc-worker', 2, 'minimal'],
 ['scheduler', 2, 'minimal'],
 ['router', 2, 'minimal'],
 ['tcp-router', 2, 'minimal'],
 ['doppler', 4, 'small'],
 ['diego-cell', 2, 'small-highmem-16GB'],
 ['log-api', 2, 'minimal'],
 ['credhub', 2, 'small'],
 ['rotate-cc-database-key', 1, 'minimal']]

이제 정리하면 됨 ^^