본문 바로가기

로컬 환경 구성/로컬 개발환경 구성

6. Ansible Module

# Ansible Module 사용하기

https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

 

All modules — Ansible Documentation

 

docs.ansible.com

- 실행 명령어 예시는 아래와 같다.

ansible all -m ping

이전에 ansible 서버에 hosts에 등록된 hosts를 기반으로 해당 모듈을 활용하여 작업을 수행한다 라고 볼 수 있다.

-m 은 모듈을 뜻하고, ping은 모듈의 종류를 말한다.

- ansible hosts에 등록된 서버들의 ping의 결과는 아래와 같다.

[root@ansible-server ~]# ansible all -m ping
172.17.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.17.0.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

- ansible hosts에 등록된 서버들의 디스크 용량 확인하기

에러발생 : "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"

해결방법 : wget https://cbs.centos.org/kojifiles/packages/sshpass/1.06/8.el8/x86_64/sshpass-1.06-8.el8.x86_64.rpm

dnf install -y ./sshpass-1.06-8.el8.x86_64.rpm

- 다시 용량 확인하기

[root@ansible-server ~]# ansible all -m shell -a "free -h" -k # -k를 입력하면 ssh 패스워드를 물어본다.
[root@ansible-server ~]# ansible all -m shell -a "free -h"
SSH password:
172.17.0.4 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:          7.4Gi       3.0Gi       444Mi        20Mi       4.1Gi       4.2Gi
Swap:         2.0Gi       5.0Mi       2.0Gi
172.17.0.3 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:          7.4Gi       3.0Gi       431Mi        20Mi       4.1Gi       4.2Gi
Swap:         2.0Gi       5.0Mi       2.0Gi

- 파일전송하기

vi test 로 파일을 생성하거 아무거나 입력 후 저장, test라는 파일을 hosts에 등록된 서버로 복사한다.

결과는 아래와 같다.

[root@ansible-server ~]# ansible all -m copy -a "src=./test dest=/tmp"
172.17.0.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "c4e863b4532bbb8b79beb0abe7dd02823ac30b66",
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "md5sum": "0875441907b78fb394deb1819abfa8c8",
    "mode": "0644",
    "owner": "root",
    "size": 16,
    "src": "/root/.ansible/tmp/ansible-tmp-1683611087.6703408-1649-154011920098635/source",
    "state": "file",
    "uid": 0
}
172.17.0.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "c4e863b4532bbb8b79beb0abe7dd02823ac30b66",
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "md5sum": "0875441907b78fb394deb1819abfa8c8",
    "mode": "0644",
    "owner": "root",
    "size": 16,
    "src": "/root/.ansible/tmp/ansible-tmp-1683611088.3805585-1651-72823120551872/source",
    "state": "file",
    "uid": 0
}

- ansible을 이용하여 hosts에 등록된 서버에 일괄적으로 프로그램 설치하기

[root@ansible-server tmp]# ansible devops -m yum -a "name=httpd state=present"
172.17.0.3 | 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"
    ]
}
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"
    ]
}

- 위의 내용처럼 정상적으로 설치된것을 확인 할 수 있다.

- Ansible을 활용하면 관리 서버에서 hosts에 등록된 서버들의 sw 관리나 환경설정 등을 효율적으로 관리 할 수 있다는 장점이 있다.

 

- 끝 -