본문 바로가기
IaC/Ansible

[Ansible] Ansible에서 현재 날짜, 시간 값 얻기

by chan10 2024. 9. 3.

Ansible 이용 중 파일 저장 등 여러 이유로 현재 날짜, 시간 정보 포함이 필요할 때가 있습니다.

그럴 경우 사용할 수 있는 현재 날짜, 시간을 얻는 방법입니다.

 

[현재 날짜, 시간]

  - name: Get timestamp from the system
    shell: "date +%Y-%m-%d%H-%M-%S"
    register: tstamp

  - name: Get timestamp from the system, include the first 5 nanoseconds digits
    shell: "date +%Y-%m-%d%H-%M-%S.%5N"
    register: tstamp_ns

  - name: Set variables
    set_fact:
     cur_date: "{{ tstamp.stdout[0:10]}}"
     cur_time: "{{ tstamp.stdout[10:]}}"
     cur_time_ns: "{{ tstamp_ns.stdout[10:]}}"

  # Debug로 변수 출력
  - name: System timestamp - date
    debug:
     msg:  "Current date: {{ cur_date }}"

  - name: System timestamp - time
    debug:
     msg:  "Current date: {{ cur_time }}"

  - name: System timestamp - time, including the first 5 nanoseconds digits
    debug:
     msg:  "Current date: {{ cur_time_ns }}"

 

[출력 결과]

[출력 결과]

TASK [System timestamp - date] **********************************************************************************************************************
ok: [###] => {
    "msg": "Current date: 2024-08-29"
}

TASK [System timestamp - time] **********************************************************************************************************************
ok: [###] => {
    "msg": "Current date: 15-56-34"
}

TASK [System timestamp - time, including the first 5 nanoseconds digits] ****************************************************************************
ok: [###] => {
    "msg": "Current date: 15-56-35.24804"
}

 

 

[참고블로그]

https://ttl255.com/ansible-getting-date-and-timestamp/