YAML - 用值替换变量?

发布于 2024-12-07 04:42:43 字数 607 浏览 0 评论 0原文

考虑以下 yaml

hadoop:  
   storage: '/x/y/z/a/b'
   streaming_jar_path: '/x/c/d/f/r/*.jar'
   commands:  
       mkdir: 'hadoop dfs -mkdir dir'
       copyFromLocal: 'hadoop dfs -copyFromLocal from_path to_path'  
       run: 'hadoop jar $streaming_jar_path -mapper mapper_path -reducer reducer_path -input hdfs_input -output hdfs_output'  

我想将 streaming_jar_path 的值替换为 $streaming_jar_path,我该怎么做?

我知道我们可以使用&(anchors)合并哈希,但在这里我只想更改一个值
如果这是一件微不足道的事情,我很抱歉,我对 YAML 很陌生,

谢谢

consider the following yaml

hadoop:  
   storage: '/x/y/z/a/b'
   streaming_jar_path: '/x/c/d/f/r/*.jar'
   commands:  
       mkdir: 'hadoop dfs -mkdir dir'
       copyFromLocal: 'hadoop dfs -copyFromLocal from_path to_path'  
       run: 'hadoop jar $streaming_jar_path -mapper mapper_path -reducer reducer_path -input hdfs_input -output hdfs_output'  

I want to substitute the value of streaming_jar_path to $streaming_jar_path, how can I do that?

I know we can merge the hashes using &(anchors) but here i just want to change one value
I am sorry if this is trivial thing, I am very new to YAML

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-12-14 04:42:43

您可以重构您的 YAML 文件并使用 Ansible 执行。

Commands.yml:

- hosts: localhost
  vars:
    streaming_jar_path: '/x/c/d/f/r/*.jar'
  tasks:
    - name: mkdir
      shell: "hadoop dfs -mkdir dir"
    - name: copyFromLocal
      shell: "hadoop dfs -copyFromLocal from_path to_path"
    - name: run
      shell: "hadoop jar {{ streaming_jar_path }} -mapper mapper_path -reducer reducer_path -input hdfs_input -output hdfs_output"

然后只需运行 ansible-playbook 即可执行 shell 命令:

ansible-playbook commands.yml

You can restructure your YAML file and execute with Ansible.

commands.yml:

- hosts: localhost
  vars:
    streaming_jar_path: '/x/c/d/f/r/*.jar'
  tasks:
    - name: mkdir
      shell: "hadoop dfs -mkdir dir"
    - name: copyFromLocal
      shell: "hadoop dfs -copyFromLocal from_path to_path"
    - name: run
      shell: "hadoop jar {{ streaming_jar_path }} -mapper mapper_path -reducer reducer_path -input hdfs_input -output hdfs_output"

Then simply run ansible-playbook to execute shell commands:

ansible-playbook commands.yml
明月松间行 2024-12-14 04:42:43

这应该是一个读取文件、编辑数据并写回文件的简单过程。

import yaml

infile = 'input.yaml'
outfile = 'output.yaml'

#read raw yaml data from file into dict
with open(infile, 'r') as f:
    data = yaml.load(f.read())

#make changes to dict
data['hadoop']['streaming_jar_path'] = '$streaming_jar_path'

#write dict back to yaml file
with open(outfile, 'w') as f:
    f.write(yaml.dump(data))

This should be a simple process of reading your file, editing the data and writing back to file.

import yaml

infile = 'input.yaml'
outfile = 'output.yaml'

#read raw yaml data from file into dict
with open(infile, 'r') as f:
    data = yaml.load(f.read())

#make changes to dict
data['hadoop']['streaming_jar_path'] = '$streaming_jar_path'

#write dict back to yaml file
with open(outfile, 'w') as f:
    f.write(yaml.dump(data))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文