考虑以下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
,我该怎么做?
我知道我们可以merge
hashes
使用&(anchors)
,但在这里我只想更改一个值
我很抱歉,如果这是微不足道的事情,我对YAML
谢谢
答案 0 :(得分:2)
您可以重新构建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
答案 1 :(得分:-1)
这应该是一个简单的过程,用于读取文件,编辑数据和写回文件。
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))
答案 2 :(得分:-1)
这是我的解决方法,使用python的字符串格式化程序......
yaml档案......
hadoop:
streaming_jar_path: '/x/c/d/f/r/*.jar'
commands:
run: 'hadoop jar {} -mapper mapper_path -reducer reducer_path -input hdfs_input -output hdfs_output'
python代码:
s_jar_p = params['hadoop']['streaming_jar_path']
run_cmd = (params['hadoop']['commands']['run']).format(s_j_p)
这对我有用: - )