As my Ansible Control Node time is in UTC, I want to get the date and time from a Windows Remote Server, 10.14.2.130
, and use that date and time for a file name.
10.14.2.130
is already in my inventory file.
My playbook:
- name: Rename file with local date and time
hosts: localhost
gather_facts: false
tasks:
- name: Rename file
delegate_to: 10.14.2.130
delegate_facts: true
become: yes
copy:
src: /var/lib/awx/projects/Windows/{{ createfilename }}
dest: /var/lib/awx/projects/Windows/{{ date_time }}{{ createfilename }}
vars:
date_time: "{{ date }}_{{ hms }}"
date: "{{ '%d-%m-%Y' | strftime(hostvars['10.14.2.130'].ansible_date_time.epoch) }}"
hms: "{{ '%H.%M.%S' | strftime(hostvars['10.14.2.130'].ansible_date_time.epoch) }}"
Error msg:
"msg": "The task includes an option with an undefined variable. The error was: {{ date }}_{{ hms }}: {{ '%d-%m-%Y' | strftime(hostvars['10.14.2.130'].ansible_date_time.epoch) }}: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_date_time'\n"
}
From the error message, it looks like I need to define the ansible_date_time
variable for 10.14.2.130
?
How and where do I do it?