大网数据广告活动
服务器运维问答

Recommended way to update config values in an automated way?

· 52CE 我爱测

To give some context, I have a build server task that requires updating a configuration file (in yaml format) field value and pushing to a repo to be used later.My immediate thought was to use some form of regex search/replace bu…

问题原文

原帖链接 · kaizenCoder · CC BY-SA 3.0

To give some context, I have a build server task that requires updating a configuration file (in yaml format) field value and pushing to a repo to be used later.My immediate thought was to use some form of regex search/replace but I'm wondering if there are other/better ways to accomplish this?

i.e:

# Before
my_field : 2

# After
my_field : 3.1

Ofcourse the file will contain other fields with values that need to be untouched.

EDIT

So as per rbtux's suggestion I found a ruby yaml modules which solves my problem. Here's how it works:

source.yaml:

---
some_value: 1
my_value: 2
new_value: 3

replace.ruby:

require 'yaml'

hiera = YAML.load_file('source.yaml');

hiera['my_value'] = "other data"

File.open('source.yaml','r+') do |h| 
   h.write hiera.to_yaml
end

puts hiera['my_value'] # outputs my_value as 'other data' with other variables intact

You can extend this to pass the value as an argument.

已采纳解答

原帖链接 · andrekeller · CC BY-SA 3.0

the best way would be to choose a script language perl, python, ruby, etc. that provides yaml manipulation utilities.

if the file is simple enough, you could get away with a sed hack though:

sed 's/^\(\s*my_field\s*:\s*\).*/\1new-value/' 

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-02-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。