senthil Kumar B.K
senthil Kumar B.K

Reputation: 35

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

I have two files called source.txt and destination.txt as below, 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

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

When we compare source.txt and destination.txt, the below content is missing,

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

I want to append the missing content to destination.txt else we can skip it if no difference. I have used diif command in shell module, but i am not getting proper difference

Any idea how to do it in Ansible

Upvotes: 0

Views: 1740

Answers (1)

ikora
ikora

Reputation: 972

basically you always want to have the same content on the 2 files if there is a difference, usually, I do that using the copy module:

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

(You can delete check_mode see the link to know why: https://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html Basically check mode is a simulation and wont change any file)

Upvotes: 1

Related Questions