반응형
    
    
    
  Ansible playbook에서 현재 날짜 및 시간을 얻는 방법(ansible-playbook)
- ansible_date_time fact
vim current_date_time1.yml---
- name: 사용자 정의 변수에 현재 날짜 및 시간 저장
  hosts: all
  gather_facts: true
  tasks:
    # ansible_date_time 표시
    - name: Debug ansible_date_time
      debug:
        var: ansible_date_time
    # 변수에 ansible_date_time 사용
    - name: 사용자 정의 변수에 현재 날짜 및 시간 저장
      set_fact:
        current_date_time: "{{ ansible_date_time.date }}_{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"
    # 현재 날짜 및 시간으로 변수 표시
    - name: Debug 현재 날짜 및 시간
      debug:
        var: current_date_time
###
# ansible-playbook -i ./inventory tmp/current_date_time1.yml --limit localhost이 코드는 ansible_date_time 팩트를 사용하여 현재 날짜 및 시간을 표시하고 해당 정보를 사용자 정의 변수 current_date_time에 저장하여 출력하는 Ansible playbook입니다.
ansible-playbook -i ./inventory tmp/current_date_time1.yml --limit localhost$ ansible-playbook -i ./inventory tmp/current_date_time1.yml --limit localhost
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [사용자 정의 변수에 현재 날짜 및 시간 저장] ***************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [localhost]
TASK [Debug ansible_date_time] *********************************************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2024-01-03",
        "day": "03",
        "epoch": "1704266954",
        "epoch_int": "1704266954",
        "hour": "16",
        "iso8601": "2024-01-03T07:29:14Z",
        "iso8601_basic": "20240103T162914413085",
        "iso8601_basic_short": "20240103T162914",
        "iso8601_micro": "2024-01-03T07:29:14.413085Z",
        "minute": "29",
        "month": "01",
        "second": "14",
        "time": "16:29:14",
        "tz": "KST",
        "tz_dst": "KST",
        "tz_offset": "+0900",
        "weekday": "수요일",
        "weekday_number": "3",
        "weeknumber": "01",
        "year": "2024"
    }
}
TASK [사용자 정의 변수에 현재 날짜 및 시간 저장] ***************************************************
ok: [localhost]
TASK [Debug 현재 날짜 및 시간] *********************************************************************
ok: [localhost] => {
    "current_date_time": "2024-01-03_162914"
}
PLAY RECAP *****************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0- ansible.builtin.pipe date
vim current_date_time2.yml---
- name: 사용자 정의 변수에 현재 날짜 및 시간 저장
  hosts: all
  gather_facts: false
  tasks:
    # lookup을 사용하여 현재 날짜 및 시간 가져오기
    - name: 현재 날짜 및 시간 조회
      set_fact:
        current_date_time: "{{ lookup('pipe', 'date +%Y%m%d_%H%M%S') }}"
    # 현재 날짜 및 시간으로 변수 표시
    - name: Debug 현재 날짜 및 시간
      debug:
        var: current_date_time
###
# ansible-playbook -i ./inventory tmp/current_date_time2.yml --limit localhost이 코드는 lookup 함수를 사용하여 현재 날짜 및 시간을 조회하고 해당 정보를 사용자 정의 변수 current_date_time에 저장하여 출력하는 Ansible playbook입니다.
ansible-playbook -i ./inventory tmp/current_date_time2.yml --limit localhost$ ansible-playbook -i ./inventory tmp/current_date_time2.yml --limit localhost
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [사용자 정의 변수에 현재 날짜 및 시간 저장] ***************************************************
TASK [현재 날짜 및 시간 조회] **********************************************************************
ok: [localhost]
TASK [Debug 현재 날짜 및 시간] *********************************************************************
ok: [localhost] => {
    "current_date_time": "20240103_163008"
}
PLAY RECAP *****************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0- ansible.builtin.shell module
vim current_date_time3.yml---
- name: Shell을 사용하여 현재 날짜 및 시간 가져오기
  hosts: all
  gather_facts: true
  tasks:
    # Shell을 사용하여 현재 날짜 가져오기
    - name: 현재 날짜 가져오기
      shell: date +%Y-%m-%dT%H:%M:%SZ
      register: current_date_time
    # 현재 날짜 및 시간으로 변수 표시
    - name: Debug 현재 날짜 및 시간
      debug:
        var: current_date_time
###
# ansible-playbook -i ./inventory tmp/current_date_time3.yml --limit localhost이 코드는 shell 모듈을 사용하여 현재 날짜와 시간을 가져오고 이 정보를 current_date_time 변수에 등록하여 출력하는 Ansible playbook입니다.
ansible-playbook -i ./inventory tmp/current_date_time3.yml --limit localhost$ ansible-playbook -i ./inventory tmp/current_date_time3.yml --limit localhost
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [Shell을 사용하여 현재 날짜 및 시간 가져오기] *************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [localhost]
TASK [현재 날짜 가져오기] **************************************************************************
changed: [localhost]
TASK [Debug 현재 날짜 및 시간] *********************************************************************
ok: [localhost] => {
    "current_date_time": {
        "changed": true,
        "cmd": "date +%Y-%m-%dT%H:%M:%SZ",
        "delta": "0:00:00.043700",
        "end": "2024-01-03 16:30:39.739427",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-01-03 16:30:39.695727",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "2024-01-03T16:30:39Z",
        "stdout_lines": [
            "2024-01-03T16:30:39Z"
        ]
    }
}
PLAY RECAP *****************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
참고URL
- Ansible Documentation : ansible_date_time facts
- Ansible Documentation : lookup commands
- Ansible Documentation : shell commands
728x90
    
    
  반응형
    
    
    
  '리눅스' 카테고리의 다른 글
| Ansible playbook에서 loop와 when을 사용하는 방법(ansible-playbook) (0) | 2024.01.03 | 
|---|---|
| Ansible playbook에서 register와 debug 모듈을 사용하는 방법(ansible-playbook) (0) | 2024.01.03 | 
| Nginx에서 로그 파일을 생성하는 방법 (0) | 2024.01.02 | 
| nload 사용법 (0) | 2023.12.28 | 
| bmon 사용법 (0) | 2023.12.28 | 
 
                  
                 
                  
                 
                  
                