I am using Ansible to automate some tasks when installing some applications.
In some point, I want to check if in a file, some lines are present. In the file are several lines always in the same order with the same content. And I want to check that no extra lines appear inserted between the previous lines.
I think that the correct command is blockinfile
, mixed with state: present
to assure that the lines are there. Also I use check_mode
to not change the file. Then the task is:
- name: Check content of file
delegate_to: localhost
blockinfile:
dest: "{{ file.path }}"
block:
line 1 text....
line 2 text....
line 3 text....
state: present
check_mode: yes
But, the task does not fail neither if the block is present or not. And Ansible continues without stopping. As suggested in this answer. I can add failed_when
as:
- name: Check content of file
delegate_to: localhost
blockinfile:
dest: "{{ file.path }}"
block:
line 1 text....
line 2 text....
line 3 text....
state: present
check_mode: yes
register: block_exists
failed_when: (block_exists | changed)
Always block_exists
is marked as changed
.
Is blockinfile
the correct command for this? How can I check if a set of lines exists in a file or not?