使用 Augeas 创建 conf 文件

Create conf file with Augeas

我正在尝试编写一个 bash 脚本,它将利用 Augeas 在 /etc/httpd/conf.d/ 中创建一个 conf 文件,并为 vhost 设置必要的节点。由于某种原因,我无法设置 DocumentRoot 节点。当我尝试通过交互式 shell 时,我可以很好地创建它:

[root@panel conf.d]# augtool 
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
augtool> set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive='DocumentRoot']/arg "/var/www/vhosts/hey.com"
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive/arg = "/var/www/vhosts/hey.com"
augtool> save
Saved 1 file(s)
augtool> quit
[root@panel conf.d]# cat hey.com.conf 
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>

但是,我的脚本无法创建它:

(我把脚本放在我的路径中以便于执行) 使用示例:# aug hey.com


#!/bin/bash

# Set up conf file
name=
path="/files/etc/httpd/conf.d/"
conf="$name.conf"
echo ${path}${conf}
docroot="/var/www/vhosts/$name"

# Create Vhost
echo "DocRoot = $docroot"
mkdir $docroot
echo $path$conf
augtool -s set ${path}${conf}/VirtualHost/arg "*:80"
augtool -s set ${path}${conf}/VirtualHost/directive "DocumentRoot"

echo $docroot
echo "Nodes Present"

augtool print ${path}${conf}
#augtool -s set ${path}${conf}/VirtualHost/*[self::directive='DocumentRoot']/arg "$docroot"
augtool -s set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive=DocumentRoot]/arg /var/www/vhosts/hey.com
augtool print ${path}${conf}
augtool match /augeas//error

据我所知,它正在尝试执行相同的命令,但无法将参数写入 DocumentRoot。如果能帮助我指明正确的方向或修改我的总体思路,我们将不胜感激。

这是在 CentOS 6.6 机器上。

更新:

脚本会即时创建文件,因此它事先不存在。我在脚本运行之间将其删除。示例输出如下:

[root@panel ~]# aug hey.com
/files/etc/httpd/conf.d/hey.com.conf
DocRoot = /var/www/vhosts/hey.com
/files/etc/httpd/conf.d/hey.com.conf
Saved 1 file(s)
Saved 1 file(s)
/var/www/vhosts/hey.com
Nodes Present
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/augeas/files/etc/sysconfig/iptables.save/error = parse_failed

明白了,我没有正确引用。在 augtool 提示符下有效的相同命令在我的 bash 脚本中无效。在脚本中,我能够设置指令本身,参数的唯一不同之处在于使用 * 和 [] 结果是 bash 正在处理它们而不是 augtool。我引用了它:

augtool -s set '/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive="DocumentRoot"]/arg "/var/www/vhosts/hey.com"'

现在我可以很好地构建节点了:

[root@panel ~]# cat /etc/httpd/conf.d/hey.com.conf 
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>