Ansible – Check if string exists in file

Question:

I’m very new to Ansible

Is it possible to check if a string exists in a file using Ansible.

I want to check is a user has access to a server.
this can be done on the server using cat /etc/passwd | grep username

but I want Ansible to stop if the user is not there.

I have tried to use the lineinfile but can’t seem to get it to return.

code

The code above adds user to the file if he is not there. All i want to do is check. I don’t want to modify the file in any way, is this possible

Thanks.

Answer:

I’d probably register and evaluate a variable.

The following simple playbook works for me:

Leave a Reply