未分类
批量插入/替换整段代码到指定位置
需求是用ansible来修改nginx配置文件,添加代理gotty打开终端的逻辑,如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | --- - name: modify nginx configure   gather_facts: F   hosts: nginx   pre_tasks:     - name: define variables       set_fact:         nginx_conf: /data/middleware/nginx/conf.d/upstream-tomcat.conf   tasks:     - name: "modify nginx configure - part 1"       shell:         cmd: |           time=$(date +%F)           sed -i "1i\# modified at $time" upstream-tomcat.conf           Bstr='\# modified at'           Estr='upstream dashboard'           sed -n "             /$Bstr/ {               n               :start               /$Estr/! {                 N                 b start               }               p             }           " map.txt | sed "             /$Bstr/ {               n               :start               /$Estr/! {                 N                 b start               }               r /dev/stdin               d             }           " {{ nginx_conf }} > {{ nginx_conf }}.new           mv -f {{ nginx_conf }}.new {{ nginx_conf }}     - name: "modify nginx configure - part 2"       shell:         cmd: |           Bstr='# --- END FOR KUBOARD ---'           Estr='location.*.\/redis\/'           sed -n "             /$Bstr/ {               n               :start               /$Estr/! {                 N                 b start               }               p             }           " location.txt | sed "             /$Bstr/ {               n               :start               /$Estr/! {                 N                 b start               }               r /dev/stdin               d             }           " {{ nginx_conf }} > {{ nginx_conf }}.new           mv -f {{ nginx_conf }}.new {{ nginx_conf }} |