conditional variables depending on time

18 views
Skip to first unread message

James Gibson

unread,
Mar 28, 2019, 4:17:32 PM3/28/19
to Ansible Project
I am trying to output what shift an event has happened depending on the time of day, but keep running into errors or getting nothing back.


---
- hosts: localhost
  vars:
     shift: "{{ 'First Shift' if ansible_date_time.time > '09:00:00' && ansible_date_time.time < '17:00:01' else '' }}"
     shift: "{{ 'Second Shift' if ansible_date_time.time > '17:00:00' && ansible_date_time.time < '01:00:01' else '' }}"
     shift: "{{ 'Third Shift' if ansible_date_time.time > '01:00:00' && ansible_date_time.time < '09:00:01' else '' }}"
  
  tasks:
    - name: Validate shift
      debug:
          msg: "An event occured on {{ shift }}"

Kai Stian Olstad

unread,
Mar 29, 2019, 2:04:00 PM3/29/19
to ansible...@googlegroups.com
On 28.03.2019 21:17, James Gibson wrote:
> I am trying to output what shift an event has happened depending on the
> time of day, but keep running into errors or getting nothing back.
>
>
> ---
> - hosts: localhost
> vars:
> shift: "{{ 'First Shift' if ansible_date_time.time > '09:00:00' && ansible_date_time.time < '17:00:01' else '' }}"
> shift: "{{ 'Second Shift' if ansible_date_time.time > '17:00:00' && ansible_date_time.time < '01:00:01' else '' }}"
> shift: "{{ 'Third Shift' if ansible_date_time.time > '01:00:00' && ansible_date_time.time < '09:00:01' else '' }}"

There are two reason this will not work.
The second shift will overwrite the first shift and the third shift will overwrite the second shift.
ansible_date_time.time is a string an you are comparing string, since time compare and string compare is completely different thing, this will not work as you would like.



--
Kai Stian Olstad

oly...@gmail.com

unread,
Mar 29, 2019, 5:26:07 PM3/29/19
to ansible...@googlegroups.com
I figured it out by using set_fact and when for the conditionals. Thanks for the reply.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d8699f61-b874-510d-02b7-f31eedac5f1b%40olstad.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages