'File contents are copied again and again, ansible is not acting as idempotent
Im trying to add file contents into remote nodes, below script is working but problem is If I run it again the file contents are copied again and again to remote nodes, ansible is not acting as idempotent.Any suggestions will be appreciated
hosts: all
vars:
content: "{{ lookup('file','/etc/foo.txt')}}"
tasks:
- name: finding all files present in directory
find:
paths: /etc/something.d/
file_type: file
patterns: '*.d'
register: c1
become: true
- lineinfile:
path: "{{ item.path }}"
line: "{{ contents }}"
state: present
create: yes
backup: yes
register: c2
become: true
with_items: "{{ c1.files }}"
- debug:
var: c1
- debug:
var: c2
Solution 1:[1]
Your lineinfile task is behaving as documented since you did neither specify the parameter regexp nor search_string.
Since we do not know whether the file you read might contain a regular expression, better use search_string as it can just contain the same line again.
- lineinfile:
path: "{{ item.path }}"
line: "{{ contents }}"
search_string: "{{ contents }}"
state: present
create: yes
backup: yes
register: c2
become: true
with_items: "{{ c1.files }}"
See https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hiran Chaudhuri |
