Every once in a while I have a situation in roles where I would like to set the state of a module to ‘present’ or ‘absent’ depending if a variable is empty or not. For example if I want to set a cron with a cron module I usually have a time variable which if it is not empty, it should set the cron job, if it is empty it should remove it (not skip it). E.g. task:
`
- name: Set cron job
cron: name=“job1” hour=“{{ role_cron_hour }}” job=“/usr/locat/script.sh”
state={{ ‘present’ ifrole_cron_hour
!= “” else ‘absent’ }}
`
Lately I usually solve this by adding an additional variable and setting it to ‘present’/‘absent’, which is also not natural, since I would like to set it to True/False, but than again I have to do the ugly if like above.
Would it make more sense to add a new filter like
`
state={{ var1|empty_to_absent }}
`
Or maybe just call the filters present (returns ‘present’ if variable is not empty, else ‘absent’) and absent (works the other way around). This is just a few lines of code to be added and if anyone is interested I would gladly make a pull request.