如何在ansible中传递今天的日期和时间作为参数

meg*_*gha 1 ansible ansible-2.x

我正在使用 ansible 运行 python 脚本。

这是我的剧本 -

- 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}}"
Run Code Online (Sandbox Code Playgroud)

我希望此剧本在运行脚本时使用 EndDate 作为今天的日期。如何使用每次运行脚本时编写的相同格式的最新日期和时间,而不必每天手动更改?

Vla*_*tka 6

问:“我怎样才能使用最新的日期和时间......?”

答:给出下面的脚本

shell> cat test.sh
#!/bin/sh
echo $1
Run Code Online (Sandbox Code Playgroud)

如果以秒为单位的粒度足够,最有效的方法是使用函数strftime

    - name: Run script
      command: >
        {{ playbook_dir }}/test.sh '{{ "%Y-%m-%d %H:%M:%S"|strftime }}'
      register: result
    - debug:
        var: result.stdout
Run Code Online (Sandbox Code Playgroud)

给出

  result.stdout: '2023-05-09 00:09:29'
Run Code Online (Sandbox Code Playgroud)

如果您需要微秒,请使用 Ansible 事实变量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
Run Code Online (Sandbox Code Playgroud)

给出

  result.stdout: '2023-05-08T22:09:29.023295Z'
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 当剧本启动时,字典ansible_date_time也将由 ' gather_facts : true '创建。

  • 如果需要,字典中还有其他属性

  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'
Run Code Online (Sandbox Code Playgroud)
  • 当 playbook 运行时,变量ansible_date_time不会自动更新。例如,
    - debug:
        var: ansible_date_time.iso8601_micro
    - debug:
        var: ansible_date_time.iso8601_micro
    - debug:
        var: ansible_date_time.iso8601_micro
Run Code Online (Sandbox Code Playgroud)

给(删节)

  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'
Run Code Online (Sandbox Code Playgroud)
  • 运行模块安装程序以更新变量ansible_date_time。例如,
    - 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
Run Code Online (Sandbox Code Playgroud)

给(删节)

  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'
Run Code Online (Sandbox Code Playgroud)
  • 函数stftime自动更新日期和时间
    - 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 }}"
Run Code Online (Sandbox Code Playgroud)

给(删节)

  msg: '2023-05-09 00:26:58'
  msg: '2023-05-09 00:27:00'
  msg: '2023-05-09 00:27:01'
Run Code Online (Sandbox Code Playgroud)