Ansible 을 이용하여 서버 종료하기

Ansible 을 이용하여 서버를 종료해 보겠습니다. 테스트를 위해 아래와 같이 2대의 VM 을 생성 하였습니다.
PROXMOX | 2 Core [host] | 2G Ram | 50G | Ubuntu 22.04.3 | vmbr0:eth0 192.168.10.100/24 | Host
PROXMOX | 2 Core [host] | 2G Ram | 50G | Ubuntu 22.04.3 | vmbr0:eth0 192.168.10.101/24 | Target
Install Ansible
Ansible 을 설치하는 방법은 여러 방법이 있지만 아래는 venv 를 이용하여 최신버전 (2024년 2월 16일 기준) 을 설치합니다.
ubuntu@ubuntu2204:~$ sudo apt install -y python3.10-venv
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
python3-pip-whl python3-setuptools-whl
The following NEW packages will be installed:
python3-pip-whl python3-setuptools-whl python3.10-venv
0 upgraded, 3 newly installed, 0 to remove and 6 not upgraded.
Need to get 2473 kB of archives.
After this operation, 2884 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-pip-whl all 22.0.2+dfsg-1ubuntu0.4 [1680 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-setuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 [788 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.10-venv amd64 3.10.12-1~22.04.3 [5716 B]
Fetched 2473 kB in 2s (1032 kB/s)
Selecting previously unselected package python3-pip-whl.
(Reading database ... 93920 files and directories currently installed.)
Preparing to unpack .../python3-pip-whl_22.0.2+dfsg-1ubuntu0.4_all.deb ...
Unpacking python3-pip-whl (22.0.2+dfsg-1ubuntu0.4) ...
Selecting previously unselected package python3-setuptools-whl.
Preparing to unpack .../python3-setuptools-whl_59.6.0-1.2ubuntu0.22.04.1_all.deb ...
Unpacking python3-setuptools-whl (59.6.0-1.2ubuntu0.22.04.1) ...
Selecting previously unselected package python3.10-venv.
Preparing to unpack .../python3.10-venv_3.10.12-1~22.04.3_amd64.deb ...
Unpacking python3.10-venv (3.10.12-1~22.04.3) ...
Setting up python3-setuptools-whl (59.6.0-1.2ubuntu0.22.04.1) ...
Setting up python3-pip-whl (22.0.2+dfsg-1ubuntu0.4) ...
Setting up python3.10-venv (3.10.12-1~22.04.3) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
ubuntu@ubuntu2204:~$ python3 -m venv ~/.ansible
ubuntu@ubuntu2204:~$ source ~/.ansible/bin/activate
(.ansible) ubuntu@ubuntu2204:~$ pip install ansible-core
Collecting ansible-core
Downloading ansible_core-2.16.3-py3-none-any.whl (2.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.3/2.3 MB 7.4 MB/s eta 0:00:00
Collecting resolvelib<1.1.0,>=0.5.3
Downloading resolvelib-1.0.1-py2.py3-none-any.whl (17 kB)
Collecting jinja2>=3.0.0
Downloading Jinja2-3.1.3-py3-none-any.whl (133 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.2/133.2 KB 9.3 MB/s eta 0:00:00
Collecting cryptography
Downloading cryptography-42.0.3-cp39-abi3-manylinux_2_28_x86_64.whl (4.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.6/4.6 MB 11.2 MB/s eta 0:00:00
Collecting PyYAML>=5.1
Downloading PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (705 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 705.5/705.5 KB 10.9 MB/s eta 0:00:00
Collecting packaging
Downloading packaging-23.2-py3-none-any.whl (53 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.0/53.0 KB 6.9 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting cffi>=1.12
Downloading cffi-1.16.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (443 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 443.9/443.9 KB 10.5 MB/s eta 0:00:00
Collecting pycparser
Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 KB 9.2 MB/s eta 0:00:00
Installing collected packages: resolvelib, PyYAML, pycparser, packaging, MarkupSafe, jinja2, cffi, cryptography, ansible-core
Successfully installed MarkupSafe-2.1.5 PyYAML-6.0.1 ansible-core-2.16.3 cffi-1.16.0 cryptography-42.0.3 jinja2-3.1.3 packaging-23.2 pycparser-2.21 resolvelib-1.0.1
(.ansible) ubuntu@ubuntu2204:~$ ansible --version
ansible [core 2.16.3]
config file = None
configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ubuntu/.ansible/lib/python3.10/site-packages/ansible
ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
executable location = /home/ubuntu/.ansible/bin/ansible
python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/home/ubuntu/.ansible/bin/python3)
jinja version = 3.1.3
libyaml = True
Ping
Ansible 로 타겟 서버에 통신이 가능한지 테스트 합니다.
(.ansible) ubuntu@ubuntu2204:~$ tee inventory <<EOF
host ansible_host="192.168.10.101"
[all:vars]
ansible_ssh_user="ubuntu"
ansible_ssh_port=22
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
EOF
(.ansible) ubuntu@ubuntu2204:~$ ansible -i inventory host -m ping
host | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Shut down a machine
community.general 모듈을 이용하여 Ansible 로 타겟 서버를 종료해 보겠습니다. 관련 정보는 https://docs.ansible.com/ansible/latest/collections/community/general/shutdown_module.html 를 참조 하세요.
(.ansible) ubuntu@ubuntu2204:~$ ansible-galaxy collection install community.general
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/community-general-8.3.0.tar.gz to /home/ubuntu/.ansible/tmp/ansible-local-1544sy95jxq8/tmpjxoyjba9/community-general-8.3.0-dlz57d7g
Installing 'community.general:8.3.0' to '/home/ubuntu/.ansible/collections/ansible_collections/community/general'
community.general:8.3.0 was installed successfully
(.ansible) ubuntu@ubuntu2204:~$ tee shutdown.yml <<EOF
---
- hosts: host
become: true
tasks:
- name: Unconditionally shut down the machine with all defaults
community.general.shutdown:
EOF
(.ansible) ubuntu@ubuntu2204:~$ ansible-playbook -i inventory shutdown.yml
PLAY [host] ***********************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [host]
TASK [Unconditionally shut down the machine with all defaults] ********************************************************************************
changed: [host]
PLAY RECAP ************************************************************************************************************************************
host : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
타겟 서버가 정상적으로 종료되었습니다.
감사합니다.
[베이넥스] DX총괄사업본부 솔루션사업부 - 김규현