file src and state=absent together

11 views
Skip to first unread message

Marc Haber

unread,
May 2, 2020, 3:38:19 PM5/2/20
to ansible...@googlegroups.com
Hi,

I like constructs like the following:

- name: MPM prefork
file:
src: "../../apache2/mods-available/{{ configfile }}"
dest: "/etc/apache2{{ instance_dashsuffix }}/mods-enabled/{{ configfile }}"
state: "{{ (instance.instance_data.mpm=='prefork') | ternary('link','absent') }}"
loop_control:
loop_var: "configfile"
loop:
- "mpm_prefork.load"
- "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
file:
src: "../../apache2/mods-available/{{ configfile }}"
dest: "/etc/apache2{{ instance_dashsuffix }}/mods-enabled/{{ configfile }}"
state: "link"
when: "{{ (instance.instance_data.mpm=='prefork') }}"
loop_control:
loop_var: "configfile"
loop:
- "mpm_prefork.load"
- "mpm_prefork.conf"

- name: MPM prefork on
file:
dest: "/etc/apache2{{ instance_dashsuffix }}/mods-enabled/{{ configfile }}"
state: "absent"
when: "{{ (instance.instance_data.mpm!='prefork') }}"
loop_control:
loop_var: "configfile"
loop:
- "mpm_prefork.load"
- "mpm_prefork.conf"

causing a lot of duplication.

Greetings
Marc

--
-----------------------------------------------------------------------------
Marc Haber | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany | lose things." Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature | How to make an American Quilt | Fax: *49 6224 1600421

Kai Stian Olstad

unread,
May 2, 2020, 5:01:21 PM5/2/20
to ansible...@googlegroups.com
On Sat, May 02, 2020 at 09:38:03PM +0200, Marc Haber wrote:
> I like constructs like the following:
>
> - name: MPM prefork
> file:
> src: "../../apache2/mods-available/{{ configfile }}"
> dest: "/etc/apache2{{ instance_dashsuffix }}/mods-enabled/{{ configfile }}"
> state: "{{ (instance.instance_data.mpm=='prefork') | ternary('link','absent') }}"
> loop_control:
> loop_var: "configfile"
> loop:
> - "mpm_prefork.load"
> - "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?

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


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.

--
Kai Stian Olstad

Marc Haber

unread,
May 3, 2020, 5:21:54 AM5/3/20
to ansible...@googlegroups.com
On Sat, May 02, 2020 at 11:00:58PM +0200, Kai Stian Olstad wrote:
> 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

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.
Reply all
Reply to author
Forward
0 new messages