# when: a file contains this regexp

**URL:** <https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129>\
**Category:** Ansible Project\
**Created:** [May 1, 2014, 2:11pm UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129 "2014-05-01T14:11:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Leggett](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Peter\_Leggett](https://forum.ansible.com/u/Peter_Leggett)\
**Post date:** [May 1, 2014, 2:11pm UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129/1 "2014-05-01T14:11:19Z")

</div>

Hi,

how can i execute a ansible task only if a file contains a specfic regexp ?  
I was hoping to use the when: statement as follows :-

- name: do this task only if file contains fred  
command: some command  
when: file contains “fred”

Can something like this work ? Thanks for any help.

Pete

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [May 1, 2014, 8:27pm UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129/2 "2014-05-01T20:27:20Z")

</div>

I think what you’d want to do here is use the shell module to check for the regex with grep, emphasis on “I think”. You could also use the exit code, since grep exits with a 0 when it matches lines, 1 if no match is found. This is probably easier.

- name: Check if file has Fred  
shell: grep ‘fred’ /path/to/file  
register: grep

- name: Do this if we find Fred  
debug: msg=“We found Fred”  
when: grep.stdout

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 1, 2014, 9:33pm UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129/3 "2014-05-01T21:33:34Z")

</div>

You could also use the “slurp” module, and register the full file contents, and use

when: foo.contents | match(regexp)

I think Paul’s suggestion is clearer and easier though, and transfers less over the wire.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 1, 2014, 9:33pm UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129/4 "2014-05-01T21:33:50Z")

</div>

Could also just use the rc from grep vs stdout, I think.

---

<div class="post-metadata">

**Author:** ![kevin\_wessel](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kevin\_wessel](https://forum.ansible.com/u/kevin_wessel)\
**Post date:** [October 9, 2015, 12:38am UTC](https://forum.ansible.com/t/when-a-file-contains-this-regexp/15129/5 "2015-10-09T00:38:52Z")

</div>

Also don’t forget to add the “ignore\_errors: yes” parameter, otherwise your play will just exit in error if the string isn’t found.
