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.
Python thay thế nhiều dòng trong tệp

Tìm kiếm và thay thế một mẫu chuỗi nhiều dòng

Khi nói đến việc tìm và thay thế một mẫu chuỗi nhiều dòng, khớp mẫu dựa trên biểu thức chính quy của Perl rất hữu ích

Một lớp lót sau đây có thể hoàn thành công việc

$ perl -i -0pe 's///' input.txt

Một vài lưu ý trong biểu thức trên

  • Tùy chọn -i yêu cầu Perl thực hiện chỉnh sửa tại chỗ, nghĩa là Perl đọc tệp đầu vào, thay thế và ghi kết quả trở lại cùng một tệp đầu vào. Nếu bạn muốn chạy khô các thay đổi, bạn có thể bỏ qua tùy chọn này, trong trường hợp đó, kết quả sẽ được hiển thị cho stdout mà không sửa đổi tệp đầu vào
  • Tùy chọ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.
    
    0 biến Perl thành chế độ "file slurp", trong đó Perl đọc toàn bộ tệp đầu vào trong một lần (thay vì từng dòng một). Điều này cho phép tìm kiếm và thay thế nhiều dòng
  • Tùy chọ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.
    
    1 cho phép bạn chạy mã Perl (khớp mẫu và thay thế trong trường hợp này) và hiển thị đầu ra từ dòng lệnh

Trong ví dụ cụ thể này, lệnh sau sẽ thay thế một chuỗi nhiều dòng theo yêu cầu và hiển thị kết quả (không áp dụng thay đổi đối với tệp đầu vào)

$ perl -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt

Sau khi xác nhận rằng kết quả là chính xác, bạn có thể áp dụng thay đổi cho tệp đầu vào bằng cách thêm tùy chọn -i

$ perl -i -0pe 's/This is a guinea pig.nThis is a gecko.nThis is a hamster.n/These were removed.n/' input.txt

Dưới đây là một số biến thể

Để tìm mẫu chuỗi nhiều dòng theo kiểu không phân biệt chữ hoa chữ thường

$ perl -i -0pe 's///i' input.txt

Để xóa mẫu chuỗi nhiều dòng

$ perl -i -0pe 's///' input.txt

Tìm kiếm và thay thế chuỗi nhiều dòng trong nhiều tệp

Nếu bạn muốn tìm và thay thế một chuỗi nhiều dòng trong nhiều tệp, bạn có thể sử dụng kết hợp

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.
3 và
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.
4 như sau

$ find ~/doc -name "*.txt" | xargs perl -i -0pe 's///'

Tìm kiếm một lớp lót này cho tất cả các tệp văn bản trong thư mục

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.
5 và áp dụng thay thế chuỗi nhiều dòng trong mỗi tệp

Trong bài đăng này, chúng ta sẽ xem cách sử dụng mô-đun Ansible lineinfile để thay thế nhiều Dòng cùng một lúc. Cách sử dụng nhiều biểu thức chính quy hoặc Regex cùng một lúc. Cách khớp nhiều dòng

Đối với ví dụ này, chúng tôi sẽ lấy tệp apache httpd.conf làm tệp cơ sở của chúng tôi và chúng tôi sẽ Thay thế tất cả cấu hình liên quan đến Trang web trong tệp để biến nó thành Trang web mới

Nói một cách đơn giản, chúng tôi sẽ di chuyển trang web/máy chủ ảo hiện tại của chúng tôi sang trang web/máy chủ ảo mới

Trước khi có bất kỳ sửa đổi nào, Lệnh Ad Hoc nhanh để kiểm tra giá trị cấu hình hiện tại là gì

 

Mục lục

Lệnh nhanh để kiểm tra cấu hình hiện tại là gì

$ 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.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

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

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

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

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

Tên*

E-mail*

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


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

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

    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

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

    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ợ…

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

    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ó…

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

    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…

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

    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…