file module, why useless src should be an error with state absent ?

950 views
Skip to first unread message

Daniel Caillibaud

unread,
Feb 9, 2021, 5:17:48 AM2/9/21
to Ansible Project
Hi,

I want a symlink to be present on some hosts and absent for others, so I write this task

- name: enable plugins for baremetal only
file:
path: "/etc/munin/plugins/{{ item }}"
state: "{{ is_baremetal | ternary('link', 'absent') }}"
src: "/usr/share/munin/plugins/{{ item }}"
loop:
- acpi
- cpu
- cpuspeed
- diskstats
- entropy
- forks
- fw_packets
- interrupts
- irqstats
- load
- proc_pri
- swap
- threads
- vmstat
notify: restart munin-node
tags: [ 'config', 'munin-node' ]

It works, but it show this warning

[WARNING]: The src option requires state to be 'link' or 'hard'. This will become an error in
Ansible 2.10

Why turn this to an error ?

It will force me to write almost the same task twice (one with `state: link` and a second
with `state: absent`)


--
Daniel

Les avions sont des jouets sans aucune valeur militaire.
Maréchal Foch (1911)

Vladimir Botka

unread,
Feb 9, 2021, 6:29:49 AM2/9/21
to Daniel Caillibaud, ansible...@googlegroups.com
On Tue, 9 Feb 2021 11:17:26 +0100
Daniel Caillibaud <m...@lairdutemps.org> wrote:

> - name: enable plugins for baremetal only
> file:
> path: "/etc/munin/plugins/{{ item }}"
> state: "{{ is_baremetal | ternary('link', 'absent') }}"
> src: "/usr/share/munin/plugins/{{ item }}"
> loop:
> - acpi
> - cpu

Wouldn't help to link conditionally? e.g.

- name: enable plugins for baremetal only
file:
when: is_baremetal|bool
state: link
path: "/etc/munin/plugins/{{ item }}"
src: "/usr/share/munin/plugins/{{ item }}"
loop:
- acpi
- cpu
...

--
Vladimir Botka

Daniel Caillibaud

unread,
Feb 9, 2021, 6:48:01 AM2/9/21
to ansible...@googlegroups.com
Le 09/02/21 à 12:29, Vladimir Botka <vbo...@gmail.com> a écrit :
Yes, but instead of my uniq task I need to write the same thing twice

- name: enable plugins for baremetal only
file:
state: link
path: "/etc/munin/plugins/{{ item }}"
src: "/usr/share/munin/plugins/{{ item }}"
loop:
- acpi
- cpu
when: is_baremetal|bool

- name: disable same plugins for others
file:
state: absent
path: "/etc/munin/plugins/{{ item }}"
loop:
- acpi
- cpu
when: not is_baremetal

And to avoid inconsistency between these two task, a var or set_fact task need to be added (to
guarantee the same list in both task).

Much more complicated than a ternary in state prop of a uniq task…

It's just a remark, why forbid such a way to switch state link|absent ?

--
Daniel

L'homme est imparfait, mais ce n'est pas étonnant si l'on songe à
l'époque où il fut créé.
Alphonse Allais

Vladimir Botka

unread,
Feb 9, 2021, 7:22:06 AM2/9/21
to Daniel Caillibaud, ansible...@googlegroups.com
On Tue, 9 Feb 2021 12:47:39 +0100
It's possible to omit src conditionally e.g.

- name: enable plugins for baremetal only
file:
path: "/etc/munin/plugins/{{ item }}"
state: "{{ is_baremetal | ternary('link', 'absent') }}"
src: "{{ is_baremetal | ternary('/usr/share/munin/plugins/' ~ item, omit) }}"
loop:
- acpi
- cpu
...

(As a workaround, not very nice, ...). Your remark is valid. +1

--
Vladimir Botka

Daniel Caillibaud

unread,
Feb 9, 2021, 2:32:42 PM2/9/21
to ansible...@googlegroups.com
Le 09/02/21 à 13:21, Vladimir Botka <vbo...@gmail.com> a écrit :
> It's possible to omit src conditionally e.g.
>
> - name: enable plugins for baremetal only
> file:
> path: "/etc/munin/plugins/{{ item }}"
> state: "{{ is_baremetal | ternary('link', 'absent') }}"
> src: "{{ is_baremetal | ternary('/usr/share/munin/plugins/' ~ item, omit) }}"
> loop:
> - acpi
> - cpu
> ...
>
> (As a workaround, not very nice, ...). Your remark is valid. +1

Still nicer than duplicate all the task, thanks a lot !

(my "pb" is solved and I learnt something)

--
Daniel

Ce n'est pas en continuant de faire ce que l'on connait
que l'on pourra faire ce que l'on ne connait pas.
Reply all
Reply to author
Forward
0 new messages