使用 Augeas 创建 conf 文件



我正在尝试编写一个bash脚本,该脚本将利用Augeas在/etc/httpd/conf.d/中创建conf文件,并为vhost设置必要的节点。由于某种原因,我无法设置文档根节点。当我通过交互式 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=$1
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>

相关内容

  • 没有找到相关文章

最新更新