How to get date and time from Remote Server and use it on a task running on Ansible Control Node?

huangapple go评论82阅读模式
英文:

How to get date and time from Remote Server and use it on a task running on Ansible Control Node?

问题

错误消息中显示需要为 10.14.2.130 定义 ansible_date_time 变量。您可以通过在您的 Ansible inventory 文件中或在 playbook 中指定主机变量来定义这个变量。以下是如何在 playbook 中定义它的示例:

- name: Rename file with local date and time
  hosts: localhost
  gather_facts: false

  tasks:

    - name: Define ansible_date_time variable
      set_fact:
        ansible_date_time: "{{ hostvars['10.14.2.130'].ansible_date_time }}"
      delegate_to: 10.14.2.130

    - 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) }}"

这个 playbook 首先使用 set_fact 任务在目标主机上定义 ansible_date_time 变量,然后使用它来为文件重命名。这应该解决您的问题。

英文:

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?

答案1

得分: 1

要从Windows远程节点获取日期和时间,您需要从特定的远程节点中收集事实信息。下面是一个示例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 }}"

这将产生以下输出:

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'

由于debug模块是在控制节点(localhost)上执行的,这是设计上的限制。如果需要在远程节点上执行,请使用delegate_to选项,如下所示:

---
- 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 }}"
英文:

> 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

huangapple
  • 本文由 发表于 2023年7月17日 18:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定