# file src and state=absent together

**URL:** <https://forum.ansible.com/t/file-src-and-state-absent-together/32835>\
**Category:** Ansible Project\
**Created:** [May 2, 2020, 7:38pm UTC](https://forum.ansible.com/t/file-src-and-state-absent-together/32835 "2020-05-02T19:38:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [May 2, 2020, 7:38pm UTC](https://forum.ansible.com/t/file-src-and-state-absent-together/32835/1 "2020-05-02T19:38:03Z")

</div>

Hi,

I like constructs like the following:

- name: MPM prefork  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "../../apache2/mods-available/{{ configfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/etc/apache2{{ instance\_dashsuffix }}/mods-enabled/{{ configfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: "{{ (instance.instance\_data.mpm=='prefork') | ternary('link','absent') }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: "configfile"  
&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.load"  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.conf"

A warning tells me that having state=absent and a source at the same time will  
be an error in ansible 2.10

How would I write this in the future?

I do sincerely hope that I wouldn't be forced to write two tasks like:

- name: MPM prefork on  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "../../apache2/mods-available/{{ configfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/etc/apache2{{ instance\_dashsuffix }}/mods-enabled/{{ configfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: "link"  
&nbsp;&nbsp;when: "{{ (instance.instance\_data.mpm=='prefork') }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: "configfile"  
&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.load"  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.conf"

- name: MPM prefork on  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/etc/apache2{{ instance\_dashsuffix }}/mods-enabled/{{ configfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: "absent"  
&nbsp;&nbsp;when: "{{ (instance.instance\_data.mpm!='prefork') }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: "configfile"  
&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.load"  
&nbsp;&nbsp;&nbsp;&nbsp;- "mpm\_prefork.conf"

causing a lot of duplication.

Greetings  
Marc

---

<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:** [May 2, 2020, 9:00pm UTC](https://forum.ansible.com/t/file-src-and-state-absent-together/32835/2 "2020-05-02T21:00:58Z")

</div>

You need to add ternary on src also and use the variable omit when false.  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#omitting-parameters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#omitting-parameters)

I must say, I don't understand why they are doing this change. It make us write  
more code that IMHO makes the code more difficult to read.

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [May 3, 2020, 9:21am UTC](https://forum.ansible.com/t/file-src-and-state-absent-together/32835/3 "2020-05-03T09:21:37Z")

</div>

> You need to add ternary on src also and use the variable omit when false.  
> [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#omitting-parameters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#omitting-parameters)

Of course. Thanks.

> I must say, I don't understand why they are doing this change. It make us write  
> more code that IMHO makes the code more difficult to read.

I could not agree more.

Greetings  
Marc
