[ANSIBLE] 플레이북 작성 및 실행해보기

Posted by Albert 289Day 17Hour 19Min 28Sec ago [2024-07-04]

플레이북은 실데 관리노드에서 실행할 내용을 담는YAML파일이다. 

(내용 작성시 space로 계층구조를 만드는데 Tab을 사용하면오류가 날수있으니 Tab은 사용하지말자)


1. 첫번째 테스트 플레이북 만들어보기

- hosts: all
tasks:
- name: Print message
debug:
msg: Hello albert

실행

[root@asimblecontroller asimble]' ansible-playbook first-playbook.yml

PLAY [all] *******************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [tnode1]
ok: [tnode2]

TASK [Print message] *********************************************************************************************************
ok: [tnode1] => {
"msg": "Hello albert"
}
ok: [tnode2] => {
"msg": "Hello albert"
}

PLAY RECAP *******************************************************************************************************************
tnode1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
tnode2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


2. 플레이북 실행시 문체크(--syntax-check)

[root@asimblecontroller asimble]' ansible-playbook --syntax-check first-playbook.yml
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded

Syntax Error while loading YAML.
mapping values are not allowed in this context

The error appears to be in '/root/asimble/first-playbook.yml': line 2, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- hosts: all
tasks:
^ here


3. 플레이북 실행점검 (--check)

플레이북 실제 실행전 제대로 실행될지 체크용도로 --check옵션을 추가하여 실행하여 미리 점검할수있음(실제로는 대상서버에서 실행되지않고 어떻게 변경될지만 보여줌)

restart-service.yml

- hosts: all
tasks:
- name: Restart sshd service
ansible.builtin.service:
name: sshd
state: restarted

--check option 추가하여 실행

[root@asimblecontroller asimble]' ansible-playbook --check restart-service.yml

PLAY [all] *******************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [tnode2]
ok: [tnode1]

TASK [Restart sshd service] **************************************************************************************************
changed: [tnode2]
changed: [tnode1]

PLAY RECAP *******************************************************************************************************************
tnode1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
tnode2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

다시 --check 옵션 제거하고 실제로 실행함

[root@asimblecontroller asimble]' ansible-playbook restart-service.yml

PLAY [all] *******************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [tnode2]
ok: [tnode1]

TASK [Restart sshd service] **************************************************************************************************
changed: [tnode2]
changed: [tnode1]

PLAY RECAP *******************************************************************************************************************
tnode1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
tnode2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

실제로 실행후 tnode1 에 접속하여 확인

[root@tnode1 .ssh]' vi /var/log/messages
Jul  4 15:19:48 tnode1 systemd: Stopping OpenSSH server daemon...
Jul 4 15:19:48 tnode1 systemd: Stopped OpenSSH server daemon.
Jul 4 15:19:48 tnode1 systemd: Starting OpenSSH server daemon...
Jul 4 15:19:48 tnode1 systemd: Started OpenSSH server daemon.

정상적으로 작동된부분 확인할수 있음





LIST

Copyright © 2014 visionboy.me All Right Reserved.