如何使用Augeas在Puppet中使用“set”创建分离的XML节点?

时间:2013-09-04 00:55:20

标签: xml set puppet augeas

我正在使用Puppet 3.2的Augeas工具,我正在尝试创建一个XML文件。我希望能够在我的XML文档中添加多个具有相同名称的字段。例如,我想通过将node2 / location2放在它自己的“node”字段中来将node2 / location2与node1 / location1分开。这是我的代码:

    augeas { "update template":
        lens    => "Xml.lns",
        require => File["${buildpath}/tempfile.xml"],
        incl => "${buildpath}/tempfile.xml",
        changes => [
            "set member/acceptors[#attribute]/node[#attribute]/nodeIdentity[#attribute]/#text node2",
            "set member/acceptors/node/nodeLocation[#attribute]/#text location2",
            "set member/acceptors/node/nodeIdentity[#attribute]/#text node1",
            "set member/acceptors/node/nodeLocation[#attribute]/#text location1"
        ],
   }

这是我得到的XML输出:

    <member>
        <acceptors>
            <node>
                <nodeIdentity>node2</nodeIdentity>
                <nodeLocation>location2</nodeLocation>
                <nodeIdentity>node1</nodeIdentity>
                <nodeLocation>location1</nodeLocation>
            </node>
        </acceptors>
    </member>

这是我想要的输出:

    <member>
        <acceptors>
            <node>
                <nodeIdentity>node2</nodeIdentity>
                <nodeLocation>location2</nodeLocation>
            </node>
            <node>
                <nodeIdentity>node1</nodeIdentity>
                <nodeLocation>location1</nodeLocation>
            </node>
        </acceptors>
    </member>

我尝试将[#attribute]添加到node1行,如下所示:

     "set member/acceptors/node[#attribute]/nodeIdentity[#attribute]/#text node1",

但是“node1”没有输出。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您需要使用XPath表达式指定要影响的node。在您的情况下,您可以通过执行以下操作来编写幂等更改:

augeas { "update template":
     lens    => "Xml.lns",
     require => File["${buildpath}/tempfile.xml"],
     incl    => "${buildpath}/tempfile.xml",
     changes => [
         "set member/acceptors/node[nodeIdentity/#text='node2']/nodeIdentity/#text node2",
         "set member/acceptors/node[nodeIdentity/#text='node2']/nodeLocation/#text location2",
         "set member/acceptors/node[nodeIdentity/#text='node1']/nodeIdentity/#text node1",
         "set member/acceptors/node[nodeIdentity/#text='node1']/nodeLocation/#text location1"
     ],
}

没有必要(我看到)过滤#attribute个子节点的存在,而不是你没有创建它们,所以你的改变不会是幂等的。 / p>