簡體   English   中英

Ansible - 如何比較源文件和目標文件之間的兩個文件並將內容從源文件附加到目標文件

[英]Ansible -How to Compare two files between source and destination file and append the content from source to destination file

我有兩個名為 source.txt 和 destination.txt 的文件,如下所示, Source.txt

[profile appdev]
Arn: aaa.dev:333:iam
Region: ap.southeast2
[profile applead]
Arn: aaa.techlead:333:iam
Region: ap.southeast2
[profile appprod]
Arn: aaa.techlead:333:iam
Region: ap.southeast2
[profile apppadmin]
Arn: aaa.techlead:333:iam
Region: ap.southeast2
[profile appsupport]
Arn: aaa.dev:333:iam
Region: ap.southeast2

目的地.txt

[profile appdev]
Arn: aaa.dev:333:iam
Region: ap.southeast2
[profile applead]
Arn: aaa.techlead:333:iam
Region: ap.southeast2
[profile appprod]
Arn: aaa.techlead:333:iam
Region: ap.southeast2
[profile apppadmin]
Arn: aaa.techlead:333:iam
Region: ap.southeast2

當我們比較 source.txt 和 destination.txt 時,缺少以下內容,

[profile appsupport]
Arn: aaa.dev:333:iam
Region: ap.southeast2

我想將缺少的內容附加到 destination.txt 否則我們可以跳過它,如果沒有區別。 我在 shell 模塊中使用了 diif 命令,但我沒有得到正確的區別

任何想法如何在 Ansible 中做到這一點

基本上,如果存在差異,您總是希望在兩個文件上具有相同的內容,通常,我使用復制模塊來做到這一點:

---
- hosts: localhost

  tasks:
  - name: Copy content of file src to destination
    copy:
      src: /home/myfolder/txt2.txt
      dest: /home/myfolder/txt1.txt
    check_mode: yes
    diff: yes

(您可以刪除 check_mode 查看鏈接以了解原因: https ://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html 基本上檢查模式是一種模擬,不會更改任何文件)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM