0

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?

U880D
  • 8,601
  • 6
  • 24
  • 40
Blitzden
  • 159
  • 7

1 Answers1

1

I want to get ... date and time from a Windows Remote Node ...

In order to do so you'll need to gather_facts from the specific Remote Node.

hosts.ini

[test]
test.example.com

A minimal example playbook

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Gather date and time only
    setup:
      gather_subset:
        - 'date_time'
        - '!min'

  - name: Show Gathered Facts
    debug:
      msg: "{{ hostvars['test.example.com'] }}"

  - name: Show 'date_time' from specific Remote Node only
    debug:
      msg: "{{ hostvars['test.example.com'].ansible_facts.date_time }}"

will result into an output of

TASK [Show 'date_time' from specific Remote Node only] ******
ok: [test.example.com] =>
  msg:
    date: '2023-07-17'
    day: '17'
    epoch: '1689595200'
    hour: '14'
    iso8601: '2023-07-17T12:00:00Z'
    iso8601_basic: 20230717T140000000000
    iso8601_basic_short: 20230717T140000
    iso8601_micro: '2023-07-17T12:00:00.000000Z'
    minute: '06'
    month: '07'
    second: '00'
    time: '14:00:00'
    tz: CEST
    tz_dst: CEST
    tz_offset: '+0200'
    weekday: Monday
    weekday_number: '1'
    weeknumber: '29'
    year: '2023'

since the debug module is executed on the Control Node (localhost) only already by design.

Or even possible

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Gather date and time only
    delegate_to: test.example.com
    setup:
      gather_subset:
        - 'date_time'
        - '!min'

  - name: Show Gathered Facts
    debug:
      msg: "{{ hostvars['test.example.com'] }}"

  - name: Show 'date_time' from specific Remote Node only
    debug:
      msg: "{{ hostvars['test.example.com'].ansible_facts.date_time }}"

Similar Q&A

Further Reading

U880D
  • 8,601
  • 6
  • 24
  • 40