# Modify line with lineinfile only if file exist

**URL:** <https://forum.ansible.com/t/modify-line-with-lineinfile-only-if-file-exist/13902>\
**Category:** Ansible Project\
**Created:** [November 13, 2013, 9:38pm UTC](https://forum.ansible.com/t/modify-line-with-lineinfile-only-if-file-exist/13902 "2013-11-13T21:38:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoann\_DAVID](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@Yoann\_DAVID](https://forum.ansible.com/u/Yoann_DAVID)\
**Post date:** [November 13, 2013, 9:38pm UTC](https://forum.ansible.com/t/modify-line-with-lineinfile-only-if-file-exist/13902/1 "2013-11-13T21:38:36Z")

</div>

Is there a way to make change in file but only if file exist ?  
For the moment the task stop with errors.

Example : the following task

> - name: “Paramétrages de suhosin pour php”  
> lineinfile:  
> dest=/etc/php5/apache2/conf.d/suhosin.ini  
> backup=yes  
> state=present  
> regexp=^{{ item.key }}  
> line=“{{ item.key }} {{ item.value }}”  
> insertafter=EOF  
> with\_items:
> - { key: ‘suhosin.post.max\_vars’, value: ‘5000’ }
> - { key: ‘suhosin.request.max\_vars’, value: ‘5000’ }
> - { key: ‘suhosin.perdir’, value: ‘pr’ }  
> notify:
> - restart apache

will fail if /etc/php5/apache2/conf.d/suhosin.ini don’t exist ie if php5-suhosin package is not installed

> failed: [127.0.0.1] =\> (item={‘value’: ‘5000’, ‘key’: ‘suhosin.post.max\_vars’}) =\> {“failed”: true, “item”: {“key”: “suhosin.post.max\_vars”, “value”: “5000”}, “rc”: 257}  
> msg: Destination /etc/php5/apache2/conf.d/suhosin.ini does not exist !  
> failed: [127.0.0.1] =\> (item={‘value’: ‘5000’, ‘key’: ‘suhosin.request.max\_vars’}) =\> {“failed”: true, “item”: {“key”: “suhosin.request.max\_vars”, “value”: “5000”}, “rc”: 257}  
> msg: Destination /etc/php5/apache2/conf.d/suhosin.ini does not exist !  
> failed: [127.0.0.1] =\> (item={‘value’: ‘pr’, ‘key’: ‘suhosin.perdir’}) =\> {“failed”: true, “item”: {“key”: “suhosin.perdir”, “value”: “pr”}, “rc”: 257}  
> msg: Destination /etc/php5/apache2/conf.d/suhosin.ini does not exist !

I’me about to use a pretask like this :

> - name: See if suhosin file exist
> 
> shell: “[-f ~/.oh-my-zsh] && echo ‘Found’ || echo ‘’”

> register: suhosin\_installed

Is there an another way ?
