Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ansible - lineinfile match string and insertafter specific place

I have a file like this

<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /box /home/shop/box
    SetEnv BOX_ROOT /home/shop/pen/www
    
</VirtualHost>


<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /pen /home/shop/pen
    Alias /book /home/shop/book
    SetEnv PEN_ROOT /home/shop/pen/www
    SetEnv BOOK_ROOT /home/shop/book/www
    
</VirtualHost>


<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /water /home/shop/water
    Alias /computer /home/shop/computer
    SetEnv WATER_ROOT /home/shop/water/www
    SetEnv COMPUTER_ROOT /home/shop/cpmputer/www
    
</VirtualHost>


<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /pen /home/shop/pen
    Alias /book /home/shop/book
    SetEnv PEN_ROOT /home/shop/pen/www
    SetEnv BOOK_ROOT /home/shop/book/www
    
</VirtualHost>

I need to add several lines knowing

  1. it need to match a VirtualHost containing the string pen
  2. if there is a match, add below line in its VirtualHost and insert after the line VirtualHost my.shop:80
  3. there might be one to many matches in one file

Here is the example of lines to insert

this
is
show
test

The expected result should be

<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /box /home/shop/box
    SetEnv BOX_ROOT /home/shop/pen/www

</VirtualHost>


<VirtualHost my.shop:80>
this
is
show
test
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /pen /home/shop/pen
    Alias /book /home/shop/book
    SetEnv PEN_ROOT /home/shop/pen/www
    SetEnv BOOK_ROOT /home/shop/book/www

</VirtualHost>


<VirtualHost my.shop:80>
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /water /home/shop/water
    Alias /computer /home/shop/computer
    SetEnv WATER_ROOT /home/shop/water/www
    SetEnv COMPUTER_ROOT /home/shop/cpmputer/www

</VirtualHost>


<VirtualHost my.shop:80>
this
is
show
test
    ServerAdmin root@localhost
    DocumentRoot /tpm
    ServerAlias *.*.*

    Alias /pen /home/shop/pen
    Alias /book /home/shop/book
    SetEnv PEN_ROOT /home/shop/pen/www
    SetEnv BOOK_ROOT /home/shop/book/www

</VirtualHost>

I don't know how I can use lineinfile when there are matches for pen and insertafter in specific.

question from:https://stackoverflow.com/questions/66060187/lineinfile-match-string-and-insertafter-specific-place

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...