본문 바로가기
인프라/앤서블(Ansible)

[ansible] 앤서블 출력 관련 playbook yml

by IT맥구리나스 2022. 6. 1.

출력은 어떻게 하나?

먼저 변수를 선언하고  결과 값을 변수에 담아야한다.

 

#yml파일
---
- hosts: localhost
  tasks:
    - name: check hostname
      shell: hostname -f
      register: "check_hostname"

    - name: debug result
      debug:
        msg: "{{ check_hostname }}"
        
        
# 실행        
# ansible-playbook expect.yml

PLAY [localhost] **********************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************
ok: [localhost]

TASK [check hostname] *****************************************************************************************************************************************************************
changed: [localhost]

TASK [debug result] *******************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true, 
        "cmd": "hostname -f", 
        "delta": "0:00:00.009930", 
        "end": "2022-06-01 04:52:07.325563", 
        "failed": false, 
        "rc": 0, 
        "start": "2022-06-01 04:52:07.315633", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "seo-node00-centos.c.gcp-ie1.internal", 
        "stdout_lines": [
            "seo-node00-centos.c.gcp-ie1.internal"
        ]
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

그런데 위 결과 값은 쓸데없이 많은 정보를 담고 있다.

우리가 알고 싶은 결과 값만 알고 싶을 땐 아래와 같이" {{ 변수명.stdout }}" 와같이 .stdout을 붙여준다.

#yml파일
---
- hosts: localhost
  tasks:
    - name: check hostname
      shell: hostname -f
      register: "check_hostname"

    - name: debug result
      debug:
        msg: "{{ check_hostname.stdout}}"
        
        
## 실행
# ansible-playbook expect.yml

PLAY [localhost] **********************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************
ok: [localhost]

TASK [check hostname] *****************************************************************************************************************************************************************
changed: [localhost]

TASK [debug result] *******************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "seo-node00-centos.c.gcp-ie1.internal"
}

PLAY RECAP ****************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

반응형

댓글