我想使用 shell 脚本将网络和子网 ID 更新到模板文件中

问题描述 投票:0回答:0

我尝试使用 sed 命令替换网络和子网值,但它正在更新所有网络/子网行中的相同值。

以下是模板中需要更新的值

net1="network1"
sub1="subnet1"

net2="network2"
sub2="subnet2"

net3="network3"
sub3="subnet3"

net4="network4"
sub4="subnet4"

下面是模板文件,

       vm_group:
        - name: man1
          image: ubuntu18.04
          flavor: ram12
          volume: 500
          ext_net:
            - network: 
              subnet: 
            - network: 
              subnet: 
            - network: 
              subnet: 
            - network: 
              subnet:
                sriov:enabled
        zone: availablity-zone1

      vm_group:
        - name: man2
          image: ubuntu18.04
          flavor: ram12
          volume: 500
          ext_net:
            - network: 
              subnet: 
            - network: 
              subnet: 
            - network: 
              subnet: 
            - network: 
              subnet:
                sriov:enabled
        zone: availablity-zone2

下面的脚本我试过了,但它更新了所有网络和子网线路中的 net2 和 sub2

    file_path="path/to/your/file.yaml"

    # Replace network and subnet values for man1 group
    sed -i "s/\(network:\).*/\1 ${net1}\n      subnet: ${sub1}\n    - network: ${net2}\n      subnet: ${sub2}\n    - network: ${net3}\n      subnet: ${sub3}\n    - network: ${net4}\n      subnet: ${sub4}/" ${file_path}

    # Replace network and subnet values for man2 group
    sed -i "s/\(network:\).*/\1 ${net1}\n      subnet: ${sub1}\n    - network: ${net3}\n      subnet: ${sub3}\n    - network: ${net4}\n      subnet: ${sub4}\n    - network: ${net2}\n      subnet: ${sub2}/" ${file_path}

预期输出:

      vm_group:
        - name: man1
          image: ubuntu18.04
          flavor: ram12
          volume: 500
          ext_net:
            - network: net1
              subnet: sub1
            - network: net2
              subnet: sub2
            - network: net3
              subnet: sub3
            - network: net4
              subnet: sub4
                sriov:enabled
        zone: availablity-zone1

      vm_group:
        - name: man2
          image: ubuntu18.04
          flavor: ram12
          volume: 500
          ext_net:
            - network: net3
              subnet: sub3
            - network: net1
              subnet: sub1
            - network: net4
              subnet: sub4
            - network: net2
              subnet: sub2
                sriov:enabled
        zone: availablity-zone2

谢谢

linux bash shell command-line-interface
© www.soinside.com 2019 - 2024. All rights reserved.