Python thay thế nhiều dòng trong tệp

Câu hỏi. Tôi có một tệp văn bản mà tôi muốn thay đổi nhiều dòng văn bản thành một nội dung khác nhưng không sử dụng trình soạn thảo văn bản. Có cách nào để tìm và thay thế mẫu chuỗi nhiều dòng từ dòng lệnh Linux không?

Giả sử bạn có một tệp văn bản giống như sau

Beginning of a text file.

This is a cat.
This is a dog.
This is a guinea pig.
This is a gecko.
This is a hamster.
This is a parrot.
This is a rabbit.
This is a ferret.
This is a turtle.

Bạn muốn thay thế nhiều dòng được đánh dấu bằng một dòng sau nhưng không sử dụng trình soạn thảo văn bản

Beginning of a text file.

This is a cat.
This is a dog.
These were removed.
This is a parrot.
This is a rabbit.
This is a ferret.
This is a turtle.
[email protected] 99:ServerName www.middlewareinventory.com:80 123:DocumentRoot "/var/www/html" 193:LogLevel warn

 

Với sự trợ giúp của lệnh ansible, ad hoc và grep, chúng tôi có thể nhận được các giá trị Cấu hình hiện tại trên máy chủ từ xa httpd. tập tin conf

Bây giờ, chúng ta hãy viết Playbook để Thay đổi tất cả các giá trị này cùng một lúc theo cách Nhiều Tìm kiếm và Thay thế

 

Ansible Lineinfile Playbook để thay thế nhiều dòng

Đây là playbook để thay thế nhiều dòng bằng mô-đun lineinfile. Chúng tôi đã sử dụng câu lệnh lặp/vòng lặp with_items để chuyển nhiều giá trị cho tham số regexline  cùng một lúc

Trong mô-đun lineinfile, tham số regex được sử dụng để giữ Chuỗi mà chúng tôi đang tìm kiếm và dòng là giữ dòng mà chúng tôi muốn thay thế bằng

Có nói rằng. Bây giờ hãy nhìn vào Playbook

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}

 

Trong with_items, mỗi Mục có hai giá trị  From

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
0

Nơi đây,

From sẽ được gọi là

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
2 trong tham số
---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
3 và

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
0 sẽ được gọi là 
---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
5 trong tham số dòng

 

Xác thực Cấu hình bằng lệnh AD HOC

$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug

 

Bạn có thể thấy Yêu cầu của chúng tôi đã hoàn thành. Chúng tôi đã di chuyển thành công Máy chủ ảo/Trang web từ miền này sang Miền khác. Cảm ơn Ansible lineinfile

 

Một cảnh báo nhanh mà tôi muốn đưa ra ở đây là Ansible lineinfile sẽ chỉ xem xét dòng phù hợp cuối cùng,

khi có nhiều hơn một mục bạn muốn thay thế của cùng một chuỗi/từ khóa. Bạn nên sử dụng mô-đun

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
6

Đây là ví dụ nhanh

 

Thay thế nhiều dòng và toàn bộ bằng mô-đun Thay thế

Đôi khi bạn muốn thay thế nhiều mục nhập của một số chuỗi hoặc từ. Ngoài ra, bạn sẽ có một danh sách các chuỗi được thay thế

ví dụ: giả sử bạn muốn thay thế nhiều chuỗi và nhiều mục nhập của chúng. Bạn không thể sử dụng mô-đun

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
7 như đã đề cập trước đó

Chúng ta hãy xem xét câu chuyện truyền thống

---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
8 được lưu vào một tệp có tên
---
  - name: Examples of lineinfile
    hosts: web
    tasks:
      - name: "Ansible Lineinfile Multiple Lines"
        become: yes
        become_user: root
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          # Line to Search/Match against
          regexp: '{{item.From}}'
          # Line to Replace with
          line: '{{item.To}}'
          state: present  
          # To validate the Changes Before Committing/Saving
          validate: "httpd -t -f %s"
        with_items:
         - { From: 'ServerName www.middlewareinventory.com:80', To: 'ServerName www.devopsjunction.com:80'}
         - { From: 'ServerAdmin [email protected]', To: 'ServerAdmin [email protected]'}
         - { From: '^LogLevel .*', To: 'LogLevel debug'}
         - { From: '^DocumentRoot .*', To: 'DocumentRoot "/var/www/devopsjunction"'}
9

A Fox one day spied a beautiful bunch of ripe grapes hanging from a vine trained along the branches of a tree. The grapes seemed ready to burst with juice, and the Fox's mouth watered as he gazed longingly at them.

The bunch hung from a high branch, and the Fox had to jump for it. The first time he jumped he missed it by a long way. So he walked off a short distance and took a running leap at it, only to fall short once more. Again and again he tried, but in vain.

Now he sat down and looked at the grapes in disgust.

"What a fool I am," he said. "Here I am wearing myself out to get a bunch of sour grapes that are not worth gaping for."

And off he walked very, very scornfully.

There are many who pretend to despise and belittle that which is beyond their reach.

 

Tại đây Nếu bạn muốn thực hiện một vài thay đổi như thế này

  • $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
    mwiweb02 | CHANGED | rc=0 >> 
    88:ServerAdmin [email protected] 
    99:ServerName www.devopsjunction.com:80 
    123:DocumentRoot "/var/www/devopsjunction" 
    193:LogLevel debug
    0 đến
    $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
    mwiweb02 | CHANGED | rc=0 >> 
    88:ServerAdmin [email protected] 
    99:ServerName www.devopsjunction.com:80 
    123:DocumentRoot "/var/www/devopsjunction" 
    193:LogLevel debug
    1
  • $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
    mwiweb02 | CHANGED | rc=0 >> 
    88:ServerAdmin [email protected] 
    99:ServerName www.devopsjunction.com:80 
    123:DocumentRoot "/var/www/devopsjunction" 
    193:LogLevel debug
    2 đến
    $ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
    mwiweb02 | CHANGED | rc=0 >> 
    88:ServerAdmin [email protected] 
    99:ServerName www.devopsjunction.com:80 
    123:DocumentRoot "/var/www/devopsjunction" 
    193:LogLevel debug
    3

 

Bạn có thể nhận thấy có nhiều mục từ

$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug
0 và
$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug
2 trong câu chuyện đó

Với playbook sau, bạn có thể thay đổi toàn bộ câu chuyện

---
- name: Change Fox to Bear and Grape to Apple
  hosts: localhost
  tasks:

  - name: Replace multiple lines and entries
    replace:
      path: story
      # Line to Search/Match against
      regexp: '{{item.From}}'
      # Line to Replace with
      replace: '{{item.To}}'
    with_items:
      - { From: '[fF]ox', To: 'Bear'}
      - { From: '[Gg]rapes', To: 'Apples'}

 

Đây là một ví dụ đơn giản tôi đã giữ để làm cho mọi thứ dễ hiểu. Các mô-đun thay thế và dòng trong tệp ansible đều hỗ trợ regex

$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug
7

Ngoài ra, mô-đun thay thế cũng hỗ trợ các yếu tố lựa chọn như

$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug
8 và
$ ansible web -m shell -a 'egrep -in "^ServerName|^ServerAdmin|^LogLevel|^DocumentRoot" /etc/httpd/conf/httpd.conf' -i ansible_hosts 
mwiweb02 | CHANGED | rc=0 >> 
88:ServerAdmin [email protected] 
99:ServerName www.devopsjunction.com:80 
123:DocumentRoot "/var/www/devopsjunction" 
193:LogLevel debug
9

Tham khảo các bài viết sau để biết thêm chi tiết và ví dụ về Thay thế và dòng trong tệp

  • Dòng Ansible trong tệp Ví dụ
  • Ví dụ thay thế Ansible

 

Đây là cách chúng tôi có thể thực hiện Nhiều Tìm kiếm và Thay thế bằng Ansible lineinfile và thay thế các mô-đun

Hi vọng điêu nay co ich

Chúc mừng
Sarav AK

Theo dõi tôi trên Linkedin Hồ sơ của tôi
Theo dõi DevopsJunction trên Facebook hoặc Twitter
Để biết thêm các video và hướng dẫn thực tế. Hãy đăng ký theo dõi kênh của chúng tôi
Đối với bất kỳ tư vấn hoặc thuê chúng tôi

Đăng ký nội dung độc quyền "chỉ dành cho người đăng ký"

Tên*

E-mail*

Thêm từ Khoảng không quảng cáo phần mềm trung gian

  • Các ví dụ về lineinfile ansible - Thêm, Sửa đổi, Xóa, Thay thế các dòng

    Mô-đun lineinfile ansible có thể là vị cứu tinh trong ngày của bạn khi bạn muốn làm việc với các tệp và đặc biệt là sửa đổi nội dung của chúng khi đang chạy, chẳng hạn như thêm một dòng mới trong tệp hoặc cập nhật một dòng trong tệp hoặc thay thế một dòng trong tệp khi nhất định

  • Dòng thay thế Ansible trong tệp - Ví dụ Thay thế Ansible. Giao lộ Devops

    Bài viết này nói về "cách thay thế một dòng trong tệp bằng ansible và xem nhiều ví dụ khác về mô-đun thay thế ansible". Ansible hỗ trợ chúng tôi bằng một mô-đun chuyên dụng có tên là thay thế Nguyên tắc hoạt động của mô-đun này giống như tìm và thay thế trong trình chỉnh sửa yêu thích của bạn và nó cũng hỗ trợ…

  • Ansible nhận địa chỉ IP của máy chủ hoặc mục tiêu hiện tại

    Cách lấy địa chỉ IP của máy chủ hiện tại hoặc máy chủ từ xa trong Ansible. Đó là câu hỏi, bài viết này sẽ giải quyết. Trong khi chạy Playbook, bạn có thể có một yêu cầu mà bạn cần lấy địa chỉ IP của máy chủ từ xa hiện tại và được kết nối. Có…

  • ansible tìm kiếm chuỗi trong tệp hoặc kiểm tra xem chuỗi có tồn tại trong tệp không

    Mục tiêu của bài đăng này là chỉ ra cách tìm kiếm một chuỗi trong một tệp với ansible. ansible cung cấp nhiều cách khác nhau để thực hiện cùng một. Chúng tôi sẽ đề cập đến ba cách chính để tìm kiếm một chuỗi trong một tệp. Mô-đun Lineinfile Sử dụng mô-đun Shell và lệnh grep…

  • Ansible Khởi động lại hệ thống và đợi khởi động lại để hoàn tất

    Mục tiêu Mục đích của bài đăng này là giải thích bằng một ví dụ về cách ansible bắt đầu khởi động lại và đợi quá trình khởi động lại hoàn tất. Có những trường hợp chúng tôi muốn các nút từ xa của mình được khởi động lại hoặc khởi động lại. Ví dụ: Lấy Bản vá làm ví dụ. Như là một phần…

Chủ Đề