# How can I execute task only when file not contain a string?

**URL:** <https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811>\
**Category:** Ansible Project\
**Created:** [February 24, 2017, 7:40pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811 "2017-02-24T19:40:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephane\_Klein1](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Stephane\_Klein1](https://forum.ansible.com/u/Stephane_Klein1)\
**Post date:** [February 24, 2017, 7:40pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/1 "2017-02-24T19:40:43Z")

</div>

Hi,

is there a solution more user friendly that [http://stackoverflow.com/questions/30786263/only-check-whether-a-line-present-in-a-file-ansible](http://stackoverflow.com/questions/30786263/only-check-whether-a-line-present-in-a-file-ansible) to test if file contain a string?

I don’t like that:

- name: Check if bash-completion in enable for root user  
command: grep -q “/etc/bash\_completion” /root/.bashrc  
register: check\_bashcompletion\_enabled  
ignore\_errors: True

- name: Enable bach-completion for root user  
command: cp /etc/skel/.bashrc /root/.bashrc  
when: check\_bashcompletion\_enabled.rc == 1

Do you know a better solution?

Best regards,  
Stéphane

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 24, 2017, 8:26pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/2 "2017-02-24T20:26:16Z")

</div>

What I would do is put the correct .bashrc in Ansible and just use the copy module to copy it to the host(s).

---

<div class="post-metadata">

**Author:** ![Stephane\_Klein1](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Stephane\_Klein1](https://forum.ansible.com/u/Stephane_Klein1)\
**Post date:** [February 24, 2017, 8:56pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/3 "2017-02-24T20:56:31Z")

</div>

I would like copy only if bash\_completion isn’t already present in .bashrc

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 24, 2017, 9:39pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/4 "2017-02-24T21:39:48Z")

</div>

Then you don't have much choice but go for the solution you posted.

But to make the output nicer I would replace "ignore\_errors: True"  
with

&nbsp;&nbsp;&nbsp;failed\_when: False

or

&nbsp;&nbsp;&nbsp;failed\_when: check\_bashcompletion\_enabled.rc \> 1  
&nbsp;&nbsp;&nbsp;changed\_when: check\_bashcompletion\_enabled.rc == 1

What is it about your solution you don't like?

---

<div class="post-metadata">

**Author:** ![Stephane\_Klein1](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Stephane\_Klein1](https://forum.ansible.com/u/Stephane_Klein1)\
**Post date:** [February 26, 2017, 1:36pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/5 "2017-02-26T13:36:33Z")

</div>

Yes, thank it’s better.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 26, 2017, 3:49pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/6 "2017-02-26T15:49:39Z")

</div>

My preference in how to achieve this in a more ansible way, is using slurp, and then checking the returned contents:

- name: Get /root/.bashrc  
slurp:  
src: /root/.bashrc  
register: root\_bashrc

- name: Enable bash-completion for root user  
copy:  
src: /etc/skel/.bashrc  
dest: /root/.bashrc

remote\_src: true  
when: “‘/etc/bash\_completion’ not in root\_bashrc.content|b64decode”

I also replaced your command to copy the file, with the copy module, using remote\_src=true

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 27, 2017, 10:18pm UTC](https://forum.ansible.com/t/how-can-i-execute-task-only-when-file-not-contain-a-string/23811/7 "2017-02-27T22:18:34Z")

</div>

it might be easier with the find module

- find: paths=... contains='whatever'  
register: found

- command: run if found  
&nbsp;&nbsp;when: found.matched \> 0
