새소식

⇥ DevOps Tech 🙋🏻‍♀️/✏️ Ansible

Ansible 사용 방법

  • -
반응형

Ansible 설치가 완료 되었다면 해당 프로비저닝 툴을 통해 관리할 서버들 역시 등록을 해주어야 한다.
기본 경로는 /etc/ansible/hosts 로 관리가 되지만 사용자가 원한다면 다른 경로에 지정하여 사용할 수 있다.

# cat /etc/ansible/hosts
[devops]
172.17.0.3
172.17.0.4

devops 라는 그룹에 172.17.0.3 , 172.17.0.4 라는 호스트가 등록되어 있다.
ansible은 등록된 호스트를 대상으로 SSH를 통해 접속하기 때문에, 사전에 비밀번호 없이 로그인이 될 수 있도록 작업해줘야 한다.

# ssh root@172.17.0.3
The authenticity of host '172.17.0.3 (172.17.0.3)' can't be established.
ECDSA key fingerprint is SHA256:34V2G2OGT0mk2ZeSlrUe3IudWQRBR3gk2aRUBrzRmxY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
root@172.17.0.3's password:

최초에 로그인을 시도하면 위와 같이 비밀번호를 입력해야 하기 때문에 Ansible 이 정상 동작하지 않는다. 

따라서 아래의 과정을 수행해야 Ansible 을 통해 관리할 수 있게 된다.

 

1. Ansible 이 설치된 서버에서 ssh-keygen 으로 private key를 생성한다.

# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:6YNGbDlH+CfNUexwVGuRRHUh+vBBFMSx0MBE+LJSDas root@c5abd6974ad2
The key's randomart image is:
+---[RSA 3072]----+
|          =BO@B++|
|       . o.o*o+o.|
|      . . ** oo  |
|     . + B +=..  |
|      * S *  o   |
|     o E +       |
|      o +        |
|     .   .       |
|                 |
+----[SHA256]-----+

엔터를 반복해서 진행하면 위와 같이 로그가 발생하며 키가 정상적으로 생성된다.

2. 생성된 키를 ansible을 통해 관리하고자 하는 서버에 전달한다.

# ssh-copy-id root@172.17.0.3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.17.0.3 (172.17.0.3)' can't be established.
ECDSA key fingerprint is SHA256:34V2G2OGT0mk2ZeSlrUe3IudWQRBR3gk2aRUBrzRmxY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.17.0.3's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.17.0.3'"
and check to make sure that only the key(s) you wanted were added.

ssh-copy-id <<접속계정>>@<<접속IP>> 를 입력한 후 비밀번호를 입력하면 정상적으로 key를 복사하게 된다.

이후에 접속하면 비밀번호 없이 해당 계정에 접속할 수 있다.
마찬가지로 관리하고자 하는 모든 서버에 해당 과정을 수행하여 모든 서버에 비밀번호 없이 접속 할 수 있도록 해야 한다.

# ssh root@172.17.0.3
Last failed login: Mon Sep 26 06:13:52 UTC 2022 from 172.17.0.3 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 26 06:08:22 2022 from 172.17.0.3
[root@c5abd6974ad2 ~]#

 


ansible 모듈 테스트

관리하고자 하는 모든 서버에 위 과정이 완료되었다면 Ansible 명령어를 통해 등록된 서버를 관리할 수 있다.
Ansible 은 멱등성을 가지고 있다.

* 멱등성 ? ( 같은 설정을 여러번 적용하여도 결과가 달라지지 않는 성질 )

옵션 설명
-l 적용 될 호스트들에 대한 파일 정보
-m 모듈 선택
-k 관리자 암호 요청
-K 관리자 권한 상승
--list-hosts 적용되는 호스트 목록

 

1. ping

ansible all -m ping

# all -> 그룹을 따지지 않고 전체 대상 (/etc/ansible/hosts)
# -m -> 모듈 옵션
# ping -> ping 모듈을 사용

# ansible devops -m ping
172.17.0.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.17.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

등록된 모든 서버에 ping 모듈을 사용하는 과정이다.

SUCCESS 라는 메시지를 통해 등록된 모든 서버에 ping 명령어가 정상 수행되는 것을 확인할 수 있다.

 

2. free (메모리 확인)

ansible all -m shell -a "free -h"
172.17.0.3 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       1.0Gi       4.3Gi       359Mi       2.4Gi       6.0Gi
Swap:         1.0Gi          0B       1.0Gi
172.17.0.4 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       1.0Gi       4.3Gi       359Mi       2.4Gi       6.0Gi
Swap:         1.0Gi          0B       1.0Gi

등록된 모든 서버의 memory 확인을 위해 shell 모듈의 인자(-a) 로 free -h 명령어를 입력해준 결과이다.

 

3. COPY (파일 복사)

# touch test.txt
# echo "hi, test file" >> test.txt
# cat test.txt
hi, test file

복사하기 위한 테스트 파일을 만들어 준다.

# ansible all -m copy -a "src=./test.txt dest=/tmp"

이후 copy 모듈과 인자로 복사할 파일, 목적지를 지정해준다.

172.17.0.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2350b31b381ab7ed772dc67f312aaae977bf942c",
    "dest": "/tmp/test.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "c95222f62be947308a043caaeed2dd13",
    "mode": "0644",
    "owner": "root",
    "size": 14,
    "src": "/root/.ansible/tmp/ansible-tmp-1664172365.9331217-1443-224394283293539/source",
    "state": "file",
    "uid": 0
}
172.17.0.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2350b31b381ab7ed772dc67f312aaae977bf942c",
    "dest": "/tmp/test.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "c95222f62be947308a043caaeed2dd13",
    "mode": "0644",
    "owner": "root",
    "size": 14,
    "src": "/root/.ansible/tmp/ansible-tmp-1664172365.9178987-1441-216283168359694/source",
    "state": "file",
    "uid": 0
}

/tmp/test.txt 로 정상적으로 파일이 복사되는 것을 확인할 수 있다.

 

4. 패키지 설치 (yum)

ansible devops -m yum -a "name=httpd state=present"

devops 그룹에 yum 모듈을 사용하여 httpd 패키지를 설치하겠다는 명령어다.

172.17.0.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-1.6.3-12.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
        "Installed: brotli-1.0.6-3.el8.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: mailcap-2.1.48-3.el8.noarch",
        "Installed: centos-logos-httpd-85.8-2.el8.noarch",
        "Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64"
    ]
}
172.17.0.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: centos-logos-httpd-85.8-2.el8.noarch",
        "Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: apr-1.6.3-12.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
        "Installed: brotli-1.0.6-3.el8.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: mailcap-2.1.48-3.el8.noarch"
    ]
}

httpd 모듈이 정상적으로 설치되는 것을 확인할 수 있다.

반응형

'⇥ DevOps Tech 🙋🏻‍♀️ > ✏️ Ansible' 카테고리의 다른 글

Ansible Role 을 이용한 노드 설치 자동화  (59) 2023.10.20
Ansible Role 작성 방법  (0) 2023.10.18
Ansible Tower (AWX) 설치 방법  (0) 2023.10.17
Ansible Playbook  (0) 2022.10.06
Ansible 입문  (1) 2022.09.26
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.