Ansible笔记1--获取当前日期时间

获取当前日期

1
2
3
- name: Display current date and time
ansible.builtin.debug:
msg: "Current date and time is {{ ansible_date_time.date }}"

获取到的日期格式是YYYY-MM-DD, 如果想改成其他格式,可以使用正则获取年月日

1
2
3
- name: Format current date
ansible.builtin.debug:
msg: "Formatted date: {{ ansible_date_time.date | regex_replace('^(\\d{4})-(\\d{2})-(\\d{2})$', '\\3/\\2/\\1') }}"

\1 - 年

\2 - 月

\3 - 日

获取当前时间

1
2
3
- name: Display current date and time
ansible.builtin.debug:
msg: "Current date and time is {{ ansible_date_time.time }}"

问题

‘ansible_date_time’ is undefined

解决方案:

ansible_date_time是事实的一部分,所以需要设置

1
gather_facts: true