比较密钥后,将内容从一个 YAML 复制到另一个 YAML

Copy content from one YAML to another YAML after comparison of keys

我有一个用例,我需要从新的 YAML 文件中选择 key:value 对。检查该密钥是否存在于旧 YAML 文件中,以及它是否复制该值并将其设置到新 YAML 文件中。
此外,如果旧文件中不存在该密钥,则询问用户。

代码:

copyfile('all.isv', '/home/ubuntu/tmp/deploy/all')
with open("/home/ubuntu/ansible-environments/aws/lp/all", 'r') as f1:
    try:
        oldvars = yaml.load(f1)
        with open("/home/ubuntu/tmp/deploy/all", 'rw') as f2:
                newvars = yaml.load(f2)
                for key,value in newvars.items():
                        print key, ":", value
                        if key in f1:
                                value =  oldvars.items(value)
                                print key,value
                                f2.write(value)
                        else:
                                value = raw_input("Enter the value ")

没用。无法理解如何检查旧文件中的键并在新文件中写入该键的值。

新文件:

# Enter the release and build you wish to deploy
release: "4.0"
build: "4_0_178"
ems_release: "4.0"
ems_build: "4_0_982"
build_type: "gold_master"

# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-test

# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: test
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: "{{ deployment_type }}"
some_new_var: hello

旧文件:

# Enter the release and build you wish to deploy
release: "4.0"
build: "4_0_178"
ems_release: "4.0"
ems_build: "4_0_999"
build_type: test_build

# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-deepali

# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: trial
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: "{{ deployment_type }}"

预期:使用两个文件(旧的和新的)生成的文件

# Enter the release and build you wish to deploy
    release: "4.0"
    build: "4_0_178"
    ems_release: "4.0"
    ems_build: "4_0_999"
    build_type: test_build

    # The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
    name_prefix: syd01-devops-deepali

    # The deployment type is one of: [ test | trial | dev | prod ]
    deployment_type: trial
    # deployment_url is typically the same value as deployment type unless it is a premium deployment.
    # In that case deployment_type is set to prod and deployment_url is either dev, trial or test
    deployment_url: "{{ deployment_type }}"
    some_new_var: Value provided by user as input

无效 Python,您的 try 没有匹配的 except。除此之外,没有必要在用于读取 "old" 文件的 with 语句的上下文中打开第二个文件。因此开始于:

import ruamel.yaml

copyfile('all.isv', '/home/ubuntu/tmp/deploy/all')
with open("/home/ubuntu/ansible-environments/aws/lp/all", 'r') as f1:
    oldvars = ruamel.yaml.round_trip_load(f1)

您无法打开 YAML 文件进行读写,因此只能读取它(打开文件进行读写是使用 'r+' 而不是 'rw' 完成的):

with open("/home/ubuntu/tmp/deploy/all", 'r') as f2:
    newvars = ruamel.yaml.round_trip_load(f2)

之后继续取消缩进并根据需要从旧变量更新新变量:

for key in newvars:
    if key in oldvars:
        # this copies the value from the old file if the key exists in there
        value =  oldvars[key] 
    else:
        # ask the user for a new value
        value = raw_input("Enter the value ")
    # update the value in newvars
    newvars[key] = value
# and write the update mapping back
with open("/home/ubuntu/tmp/deploy/all", 'w') as f2:
     ruamel.yaml.round_trip_dump(newvars, f2)

合并并命名文件 old.yamlnew.yaml 并用“abcd”回答提示:

import sys
import ruamel.yaml

with open('new.yaml') as fp:
    newvars = ruamel.yaml.round_trip_load(fp)
with open('old.yaml') as fp:
    oldvars = ruamel.yaml.round_trip_load(fp)
for key in newvars:
    if key in oldvars:
        # this copies the value from the old file if the key exists in there
        value =  oldvars[key]
    else:
        # ask the user for a new value
        value = raw_input("Enter the value ")
    # update the value in newvars
    newvars[key] = value
ruamel.yaml.round_trip_dump(newvars, sys.stdout)

给你:

Enter the value abcd
# Enter the release and build you wish to deploy
release: '4.0'
build: '4_0_178'
ems_release: '4.0'
ems_build: '4_0_999'
build_type: test_build
# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-deepali

# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: trial
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: '{{ deployment_type }}'
some_new_var: abcd

请注意:

  • 这只在 Python 2.7
  • 上运行
  • 对于不需要它们的标量,引号会被删除
  • 我没有尝试将输出的四个 space 缩进包含在第一行之后。