«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags more
Archives
Today
Total
05-20 13:42
관리 메뉴

+1-1+1-1+1-1+1-1...

[서버관리 자동화] Ansible 기초 명령 본문

Linux/Sever Mangement

[서버관리 자동화] Ansible 기초 명령

투명인간 2021. 2. 27. 16:24
728x90

Ansible을 이용하면 Ansible Master 서버에서 ad-hoc 명령을 통해 원격으로 기초적인 작업을 수행할 수 있음

 

기본적인 명령 옵션

-m 모듈 지정

-a 모듈에 제공하는 command를 argument 형태로 지정함

-i 인벤토리 지정 (미지정시 Ansible.cfg에 설정된 Default 경로에서 찾아 씀)

-k SSH 암호입력 옵션 (미리 SSH-KEY 값을 공유하는 서버간에는 쓸 필요 없음)

-b sudo 명령으로 실행해야 할 경우 지정

 

관련 ansible 지원 ad-hoc 명령은 아래 사이트에서 찾아 쓰면 된다.

Introduction to ad-hoc commands — Ansible Documentation

 

Introduction to ad-hoc commands — Ansible Documentation

An Ansible ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. Ad-hoc commands are quick and easy, but they are not reusable. So why learn about ad-hoc commands first? Ad-hoc commands demonstra

docs.ansible.com

1. Ansible에서 관리하는 호스트 리스트 조회하기

[root@test-mgmt01 ~]# ansible all --list-hosts
  hosts (4):
    TEST-WEB01
    TEST-WEB02
    TEST-SQL01
    TEST-SQL02

2. Ping 테스트

[root@test-mgmt01 ~]# ansible all -m ping
TEST-SQL01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
TEST-WEB01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
TEST-SQL02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
TEST-WEB02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

3. shell 명령 실행 > 디렉토리 용량 조회

[root@test-mgmt01 ~]# ansible all -m shell -a "df -h"
TEST-SQL01 | CHANGED | rc=0 >>
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   27G  1.5G   26G   6% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   19M  478M   4% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1             1014M  139M  876M  14% /boot
tmpfs                  100M     0  100M   0% /run/user/0
TEST-WEB01 | CHANGED | rc=0 >>
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   27G  1.3G   26G   5% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   20M  478M   4% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1             1014M  139M  876M  14% /boot
tmpfs                  100M     0  100M   0% /run/user/0
TEST-WEB02 | CHANGED | rc=0 >>
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   27G  1.3G   26G   5% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   20M  478M   4% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1             1014M  139M  876M  14% /boot
tmpfs                  100M     0  100M   0% /run/user/0
TEST-SQL02 | CHANGED | rc=0 >>
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   27G  1.5G   26G   6% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   20M  478M   4% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1             1014M  139M  876M  14% /boot
tmpfs                  100M     0  100M   0% /run/user/0

4. shell 명령 실행 > 프로세스 리스트 조회

[root@test-mgmt01 ~]# ansible all -m shell -a "ps -ef" | egrep "httpd|>>"
TEST-SQL01 | CHANGED | rc=0 >>
TEST-WEB02 | CHANGED | rc=0 >>
root     14668     1  0  2월06 ?      00:01:10 /usr/sbin/httpd -DFOREGROUND
apache   25160 14668  0  2월21 ?      00:00:33 /usr/sbin/httpd -DFOREGROUND
apache   25161 14668  0  2월21 ?      00:00:32 /usr/sbin/httpd -DFOREGROUND
apache   25162 14668  0  2월21 ?      00:00:32 /usr/sbin/httpd -DFOREGROUND
apache   25163 14668  0  2월21 ?      00:00:33 /usr/sbin/httpd -DFOREGROUND
apache   25164 14668  0  2월21 ?      00:00:32 /usr/sbin/httpd -DFOREGROUND
apache   25192 14668  0  2월21 ?      00:00:33 /usr/sbin/httpd -DFOREGROUND
apache   25193 14668  0  2월21 ?      00:00:33 /usr/sbin/httpd -DFOREGROUND
TEST-WEB01 | CHANGED | rc=0 >>
root     15006     1  0  2월06 ?      00:01:21 /usr/sbin/httpd -DFOREGROUND
apache   26112 15006  0  2월21 ?      00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   26113 15006  0  2월21 ?      00:00:28 /usr/sbin/httpd -DFOREGROUND
apache   26114 15006  0  2월21 ?      00:00:28 /usr/sbin/httpd -DFOREGROUND
apache   26115 15006  0  2월21 ?      00:00:28 /usr/sbin/httpd -DFOREGROUND
apache   26212 15006  0  2월21 ?      00:00:28 /usr/sbin/httpd -DFOREGROUND
TEST-SQL02 | CHANGED | rc=0 >>

5. 사용자 계정 생성/삭제 - ansible에서는 state 값에 present(add), absent(del)를 지정하여 생성/삭제 할 수 있음

[root@test-mgmt01 ~]# ansible all -m user -a "name=user01"
TEST-SQL02 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/user01",
    "name": "user01",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: 경고: 홈디렉터리가 이미 있습니다.\nskel 디렉터리에서 파일을 복사하지 않습니다.\n메일함 파일을 만드는 중: 파일이 있습니다\n",
    "stderr_lines": [
        "useradd: 경고: 홈디렉터리가 이미 있습니다.",
        "skel 디렉터리에서 파일을 복사하지 않습니다.",
        "메일함 파일을 만드는 중: 파일이 있습니다"
    ],
    "system": false,
    "uid": 1000
}
TEST-WEB02 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/user01",
    "name": "user01",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: 경고: 홈디렉터리가 이미 있습니다.\nskel 디렉터리에서 파일을 복사하지 않습니다.\n메일함 파일을 만드는 중: 파일이 있습니다\n",
    "stderr_lines": [
        "useradd: 경고: 홈디렉터리가 이미 있습니다.",
        "skel 디렉터리에서 파일을 복사하지 않습니다.",
        "메일함 파일을 만드는 중: 파일이 있습니다"
    ],
    "system": false,
    "uid": 1000
}
TEST-WEB01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/user01",
    "name": "user01",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: 경고: 홈디렉터리가 이미 있습니다.\nskel 디렉터리에서 파일을 복사하지 않습니다.\n메일함 파일을 만드는 중: 파일이 있습니다\n",
    "stderr_lines": [
        "useradd: 경고: 홈디렉터리가 이미 있습니다.",
        "skel 디렉터리에서 파일을 복사하지 않습니다.",
        "메일함 파일을 만드는 중: 파일이 있습니다"
    ],
    "system": false,
    "uid": 1000
}
TEST-SQL01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/user01",
    "name": "user01",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: 경고: 홈디렉터리가 이미 있습니다.\nskel 디렉터리에서 파일을 복사하지 않습니다.\n메일함 파일을 만드는 중: 파일이 있습니다\n",
    "stderr_lines": [
        "useradd: 경고: 홈디렉터리가 이미 있습니다.",
        "skel 디렉터리에서 파일을 복사하지 않습니다.",
        "메일함 파일을 만드는 중: 파일이 있습니다"
    ],
    "system": false,
    "uid": 1000
}
#사용자 삭제
[root@test-mgmt01 ~]#  ansible all -m user -a "name=user01 state=absent"
TEST-SQL02 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "user01",
    "remove": false,
    "state": "absent"
}
TEST-WEB02 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "user01",
    "remove": false,
    "state": "absent"
}
TEST-WEB01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "user01",
    "remove": false,
    "state": "absent"
}
TEST-SQL01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "user01",
    "remove": false,
    "state": "absent"
}

6. 파일 복사 - 중복 실행인 경우, 푸른색으로 표시만 준다 (이를 멱등성을 보장한다고 함)

[root@test-mgmt01 ~]# ansible all -m copy -a "src=/root/copytest.txt dest=/root/copytest.txt"
TEST-SQL02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/copytest.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/root/copytest.txt",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
TEST-SQL01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/copytest.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/root/copytest.txt",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
TEST-WEB02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/copytest.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/root/copytest.txt",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
TEST-WEB01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/copytest.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/root/copytest.txt",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

7. 서비스 시작 (systemctl)

# 웹서버 리스트 조회
[root@test-mgmt01 ~]# ansible web --list-hosts
  hosts (2):
    TEST-WEB01
    TEST-WEB02

[root@test-mgmt01 ~]# ansible web -m service -a "name=httpd state=started" | egrep "=>|name|state"
TEST-WEB01 | SUCCESS => {
    "name": "httpd",
    "state": "started",
TEST-WEB02 | SUCCESS => {
    "name": "httpd",
    "state": "started",
반응형