有没有写ini文件的GNU/Linux命令?
Is there a GNU/Linux command to write ini files?
我目前正在编写一些文档(特别是关于如何使用本地 Vagrant 机器设置 Ansible)并且我喜欢以要发出的命令的形式列出评论。例如:
Set up the SSH agent:
ssh-agent bash
ssh-add .vagrant/machines/default/virtualbox/private_key
然而,有时我发现我想记录配置文件中的更改,这并不容易描述。我目前这样做:
Configure Ansible to use port 2222 for Vagrant
Modify /etc/ansible/ansible.cfg to use remote_port = 2222
我宁愿做这样的事情(理论上的命令),因为它可以以更自动化的方式快速发出,而不是使用文本编辑器:
Configure Ansible to use port 2222 for Vagrant
ansible-config remote_port 2222
这似乎是一个 ini 文件,所以如果 ansible
二进制文件不支持这个(而且我看不到它支持),是否有通用的 Linux/Unix 方法来做这个?做不到以上,怎么样:
iniwrite /etc/ansible/ansible.cfg defaults.remote_port 2222
当然可以写个脚本之类的,但最好不要重新发明轮子!
我正在使用 Ubuntu 14.04 LTS。
我不知道有这样的工具,它绝对不是您可以期望安装在用户系统上的默认工具链的一部分。
您可以使用 sed
:
sed -i 's/^remote_port=.*/remote_port=2222/' ansible.cfg
所有 GNU/Linux 用户都会明白这一点,尤其是当您描述它在做什么时。
ansible-config
脚本可以像这样简单:
#!/bin/sh
CFG=/etc/ansible/ansible.cfg
awk '{BEGIN {found=0} END {if(!found){print keyword" = "value}} if([=10=]~"^ *"keyword" *="){print keyword" = "value; found=1}else{print [=10=]}}' \
keyword="" value="" $CFG > $CFG.new \
&& mv $CFG.new $CFG \
|| rm -f $CFG.new
需要进行一些错误检查等...,但它给了你想法。
虽然我的问题的重点是 "how to automate the writing of ini files in general",但我已经找到了如何在 Ansible 中避免它。可以将 SSH 端口添加到 /etc/ansible/hosts
文件中,因此:
127.0.0.1:2222
这会覆盖 /etc/ansible/ansible.cfg
配置 ini 文件中的 remote_port
。
这正是 Ansible 应该解决的问题之一 - 因此您可以使用它来编写自己的配置文件。只需创建一个 bootstrap 剧本,在本地主机上完成所有必要的事情(当然除了安装 Ansible)。
这些工具的前身是 Augeas. It comes with a default IniFile "lens",您可以使用它轻松编写各种 ini 条目。
我目前正在编写一些文档(特别是关于如何使用本地 Vagrant 机器设置 Ansible)并且我喜欢以要发出的命令的形式列出评论。例如:
Set up the SSH agent:
ssh-agent bash ssh-add .vagrant/machines/default/virtualbox/private_key
然而,有时我发现我想记录配置文件中的更改,这并不容易描述。我目前这样做:
Configure Ansible to use port 2222 for Vagrant
Modify /etc/ansible/ansible.cfg to use remote_port = 2222
我宁愿做这样的事情(理论上的命令),因为它可以以更自动化的方式快速发出,而不是使用文本编辑器:
Configure Ansible to use port 2222 for Vagrant
ansible-config remote_port 2222
这似乎是一个 ini 文件,所以如果 ansible
二进制文件不支持这个(而且我看不到它支持),是否有通用的 Linux/Unix 方法来做这个?做不到以上,怎么样:
iniwrite /etc/ansible/ansible.cfg defaults.remote_port 2222
当然可以写个脚本之类的,但最好不要重新发明轮子!
我正在使用 Ubuntu 14.04 LTS。
我不知道有这样的工具,它绝对不是您可以期望安装在用户系统上的默认工具链的一部分。
您可以使用 sed
:
sed -i 's/^remote_port=.*/remote_port=2222/' ansible.cfg
所有 GNU/Linux 用户都会明白这一点,尤其是当您描述它在做什么时。
ansible-config
脚本可以像这样简单:
#!/bin/sh
CFG=/etc/ansible/ansible.cfg
awk '{BEGIN {found=0} END {if(!found){print keyword" = "value}} if([=10=]~"^ *"keyword" *="){print keyword" = "value; found=1}else{print [=10=]}}' \
keyword="" value="" $CFG > $CFG.new \
&& mv $CFG.new $CFG \
|| rm -f $CFG.new
需要进行一些错误检查等...,但它给了你想法。
虽然我的问题的重点是 "how to automate the writing of ini files in general",但我已经找到了如何在 Ansible 中避免它。可以将 SSH 端口添加到 /etc/ansible/hosts
文件中,因此:
127.0.0.1:2222
这会覆盖 /etc/ansible/ansible.cfg
配置 ini 文件中的 remote_port
。
这正是 Ansible 应该解决的问题之一 - 因此您可以使用它来编写自己的配置文件。只需创建一个 bootstrap 剧本,在本地主机上完成所有必要的事情(当然除了安装 Ansible)。
这些工具的前身是 Augeas. It comes with a default IniFile "lens",您可以使用它轻松编写各种 ini 条目。