Reputation: 833
I am running python script using ansible.
Here is my playbook -
- name: Run python script
command: python Test.py -StartDate 2020-10-01T00:00:00 -EndDate 2020-11-05T00:00:00
register: result
- debug: msg="{{result.stdout}}"
I want this playbook to use EndDate as todays date when I run script. How can I use latest date and time in same format I have written every time I run script without having to change manually every day?
Upvotes: 1
Views: 9944
Reputation: 105
You can call the now() function with a fmt
argument which accepts a strftime string.
---
- hosts: localhost
vars:
exec_date: "{{ now(fmt='%Y-%m-%d') }}T00:00:00"
Upvotes: 0
Reputation: 68104
Q: "How can I use the latest date and time ... ?"
A: Given the script below
shell> cat test.sh
#!/bin/sh
echo $1
If the granularity by seconds is sufficient the most efficient way is using the function strftime
- name: Run script
command: >
{{ playbook_dir }}/test.sh '{{ "%Y-%m-%d %H:%M:%S"|strftime }}'
register: result
- debug:
var: result.stdout
gives
result.stdout: '2023-05-09 00:09:29'
If you need microseconds use Ansible facts variable ansible_date_time
- setup:
gather_subset: date_time
- name: Run script
command: "{{ playbook_dir }}/test.sh {{ ansible_date_time.iso8601_micro }}"
register: result
- debug:
var: result.stdout
gives
result.stdout: '2023-05-08T22:09:29.023295Z'
Notes:
The dictionary ansible_date_time will be also created by 'gather_facts: true' when a playbook starts.
If needed there are other attributes in the dictionary
ansible_date_time:
date: '2023-05-09'
day: 09
epoch: '1683583769'
epoch_int: '1683583769'
hour: '00'
iso8601: '2023-05-08T22:09:29Z'
iso8601_basic: 20230509T000929023295
iso8601_basic_short: 20230509T000929
iso8601_micro: '2023-05-08T22:09:29.023295Z'
minute: 09
month: '05'
second: '29'
time: 00:09:29
tz: CEST
tz_dst: CEST
tz_offset: '+0200'
weekday: Tuesday
weekday_number: '2'
weeknumber: '19'
year: '2023'
- debug:
var: ansible_date_time.iso8601_micro
- debug:
var: ansible_date_time.iso8601_micro
- debug:
var: ansible_date_time.iso8601_micro
give (abridged)
ansible_date_time.iso8601_micro: '2023-05-08T22:09:29.023295Z'
ansible_date_time.iso8601_micro: '2023-05-08T22:09:29.023295Z'
ansible_date_time.iso8601_micro: '2023-05-08T22:09:29.023295Z'
- setup:
gather_subset: date_time
- debug:
var: ansible_date_time.iso8601_micro
- setup:
gather_subset: date_time
- debug:
var: ansible_date_time.iso8601_micro
- setup:
gather_subset: date_time
- debug:
var: ansible_date_time.iso8601_micro
give (abridged)
ansible_date_time.iso8601_micro: '2023-05-08T22:09:29.845384Z'
ansible_date_time.iso8601_micro: '2023-05-08T22:09:30.390135Z'
ansible_date_time.iso8601_micro: '2023-05-08T22:09:31.059892Z'
- debug:
msg: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
- wait_for:
timeout: 1
- debug:
msg: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
- wait_for:
timeout: 1
- debug:
msg: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
give (abridged)
msg: '2023-05-09 00:26:58'
msg: '2023-05-09 00:27:00'
msg: '2023-05-09 00:27:01'
Upvotes: 8
Reputation: 6685
assuming the T00:00:00
is always fixed, you could declare a variable using the lookup plugin, see an example below the exec_date
variable and the modified command
task:
---
- hosts: localhost
gather_facts: false
vars:
exec_date: "{{ lookup('pipe', 'date +%Y-%m-%d') }}T00:00:00"
tasks:
- name: print
debug: var=exec_date
- name: Run python script
command: "python Test.py -StartDate 2020-10-01T00:00:00 -EndDate {{ exec_date }}"
register: result
- debug: msg="{{result.stdout}}"
If you want to pass the current time too instead of a fixed T00:00:00
, you could use the below:
vars:
exec_date: "{{ lookup('pipe', 'date +%Y-%m-%dT%H:%M:%S') }}"
cheers
Upvotes: 1