Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果节点不存在,则无法将xml节添加到 彩蓝 网络名称bnx1 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP_Xml_Puppet_Augeas - Fatal编程技术网

如果节点不存在,则无法将xml节添加到 彩蓝 网络名称bnx1 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP

如果节点不存在,则无法将xml节添加到 彩蓝 网络名称bnx1 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP 传输因特网 IPADR125.125.125.142 1234港 协议类型TCP,xml,puppet,augeas,Xml,Puppet,Augeas,我想在name=B的节点中创建一个带有密钥NetName和值bnx2的条目,如果它不存在的话。 我正在将augeas与puppet一起使用。以下是您可以使用AugeTool执行的操作: <root> <node name="Client"> <node name="Attributes"> <info> <hash> <entry><key

我想在name=B的节点中创建一个带有密钥NetName和值bnx2的条目,如果它不存在的话。
我正在将augeas与puppet一起使用。

以下是您可以使用AugeTool执行的操作:

<root>
<node name="Client">
    <node name="Attributes">
        <info>
            <hash>
                <entry><key>colour</key><value type="string">blue</value></entry>
            </hash>
        </info>
    </node>
</node>
<node name="Network">
    <node name="A">
        <info>
            <hash>
                <entry><key>NetName</key><value type="string">bnx1</value></entry>
                <entry><key>transport</key><value type="string">internet</value></entry>
                <entry><key>ipAddr</key><value type="string">125.125.125.142</value></entry>
                <entry><key>portNo</key><value type="string">1234</value></entry>
                <entry><key>protocolType</key><value type="string">tcp</value></entry>
            </hash>
        </info>
    </node>
    <node name="B">
        <info>
            <hash>
                <entry><key>transport</key><value type="string">internet</value></entry>
                <entry><key>ipAddr</key><value type="string">125.125.125.142</value></entry>
                <entry><key>portNo</key><value type="string">1234</value></entry>
                <entry><key>protocolType</key><value type="string">tcp</value></entry>
            </hash>
        </info>
    </node>
</node>
</root>
将其转换为木偶定义的类型:

augtool> defvar hash /files/39143450.xml/root/node[#attribute/name="Network"]/node[#attribute/name="B"]/info/hash
augtool> defnode entry $hash/entry[key/#text="NetName"]
augtool> set $entry/key/#text "NetName"
augtool> set $entry/value/#attribute/type "string"
augtool> set $entry/value/#text "bnx2"
augtool> save
奔跑木偶:

define entry (
  $value,
  $file,
  $ensure = 'present',
  $node = 'B',
) {
  case $ensure {
    'present': {
      $changes = [
        "defnode entry entry[key/#text='${name}'] ''",
        "set \$entry/key/#text '${name}'",
        "set \$entry/value/#attribute/type 'string'",
        "set \$entry/value/#text '${value}'",
      ]
    }

    'absent': {
      $changes = "rm entry[key/#text='${name}']"
    }

    default: {
      fail("Unknown value for ensure: ${ensure}")
    }
  }

  augeas { "Set ${title} in ${file}":
    incl    => $file,
    lens    => 'Xml.lns',
    context => "/files${file}/root/node[#attribute/name='Network']/node[#attribute/name='${node}']/info/hash",
    changes => $changes,
  }
}

entry { 'NetName':
  value => 'bnx2',
  file  => '/path/to/39143450.xml',
}

嵌套的“节点”使此任务变得困难。最好是使用标记“nodes”而不是“node”。 试试这个

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序11
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List rootNodes=doc.Element(“root”).Elements(“node”).ToList();
列出B节点=根节点。选择(x=>x.Elements(“节点”)。其中(y=>(string)y.Attribute(“name”)==“B”)。选择多个(z=>z)。ToList();
foreach(bNodes.substands中的XElement bHash(“哈希”))
{
Boolean netName=bHash.subjects(“key”)。其中(x=>(string)x==“netName”).Any();
如果(!netName)
{
添加(新元素(“条目”,新对象[]){
新XElement(“密钥”、“网络名称”),
新元素(“值”,新对象[]{
新XAttribute(“类型”、“字符串”),
“价值”
})
}));
}
}
}
}
}

末尾缺少
?已更正。我正在努力引用hiera中的entry变量。我试过%{::entry}\$entry$entry%{entry}运气不好,有什么建议吗?我正在努力让这个在hiera起作用。-“defnode entry/root/node[#attribute/name='Network']/node[#attribute/name='${node}']/info/hash/entry[key/#text='NetName']'”——“set\$entry/key/#text'NetName'”——“set\$entry/value/#attribute type'string'”——“set\$entry/value/#text'eth8'”失败,无法评估”把希拉放在等式中听起来像是另一个问题。
Notice: Compiled catalog for example.com in environment production in 0.15 seconds
Notice: /Stage[main]/Main/Entry[NetName]/Augeas[Set NetName in /path/to/39143450.xml]/returns: executed successfully
Notice: Applied catalog in 0.09 seconds
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication11
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> rootNodes = doc.Element("root").Elements("node").ToList();
            List<XElement> bNodes = rootNodes.Select(x => x.Elements("node").Where(y => (string)y.Attribute("name") == "B")).SelectMany(z => z).ToList();
            foreach (XElement bHash in bNodes.Descendants("hash"))
            {
                Boolean netName = bHash.Descendants("key").Where(x => (string)x == "NetName").Any();
                if (!netName)
                {
                    bHash.Add(new XElement("entry", new object[] {
                        new XElement("key","NetName"),
                        new XElement("value", new object[] {
                            new XAttribute("type","string"),
                            "value"
                        })
                    }));
                }
            }

        }
    }

}