[Ansible] 앤서블 학습 - 1. 설치 및 기본 명령어

 

Ansible 이란


서버를 효율적으로 관리할 수 있게 해 주는 오픈 소스 IT 자동화 도구

 

Ansible 특징


  • YAML 형식으로 구문을 해석하기 쉽다(특별한 코딩 기술이 필요하지 않음)
  • 별도 Agent가 필요 없으며 보안이 우수하다.
  • 멱등성 - 어떤 작업을 여러 번 실행해도 결과가 항상 같다.

Ansible 설치


CentOS7 공식 Repository에서는 ansible이 없네요 설치가 안됩니다.

[root@linux-1 yum.repos.d]# yum install -y ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.kakao.com
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
No package ansible available.
Error: Nothing to do

 

그럼 epel-repository를 받아서 ansbile을 설치해 보겠습니다.

[root@linux-1 yum.repos.d]# yum  install -y epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.kakao.com
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================
 Package                         Arch                      Version                   Repository                 Size
=====================================================================================================================
Installing:
 epel-release                    noarch                    7-11                      extras                     15 k

Transaction Summary
=====================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                                  |  15 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                                          1/1
  Verifying  : epel-release-7-11.noarch                                                                          1/1

Installed:
  epel-release.noarch 0:7-11

Complete!

 

ansible 설치 완료

[root@linux-1 yum.repos.d]# yum install -y ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.kakao.com
 * epel: ftp.jaist.ac.jp
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package ansible.noarch 0:2.9.7-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================
 Package                    Arch                      Version                          Repository               Size
=====================================================================================================================
Installing:
 ansible                    noarch                    2.9.7-1.el7                      epel                     17 M

Transaction Summary
=====================================================================================================================
Install  1 Package

Total size: 17 M
Installed size: 105 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ansible-2.9.7-1.el7.noarch [##################################################################  ] 1/1t  Installing : ansible-2.9.7-1.el7.noarch                                                                        1/1
  Verifying  : ansible-2.9.7-1.el7.noarch                                                                        1/1
Installed:
  ansible.noarch 0:2.9.7-1.el7

Complete!

 

ansible 버전은 2.9.7 입니다.

[root@linux-1 yum.repos.d]# ansible --version
ansible 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

 

ansible Root Directory 위치입니다.

해당 위치에는 ansible 설정 파일, roles 디렉터리, hosts 파일 (inventory)이 있습니다.

[root@linux-1 ansible]# cd /
[root@linux-1 /]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
└── roles

 

Ansible 실습


앞으로 실행할 명령어는 아래 환경에서 진행 예정입니다.

ansible 서버가 있고 3대의 관리 서버가 있습니다, 관리 서버는 편의상 웹서버로 지칭하겠습니다.

 

ansible 명령어를 쳐보기 전에 먼저 진행할 작업이 두 가지 있습니다.

첫 번째는 hosts 파일에 서버를 등록하는 작업이고

두 번째는 ansible 서버에서 웹서버로 패스워드 없이 접속할 수 있도록 key등록을 해주는 작업입니다.

 

 

hosts 파일에 서버 등록 - 완료

(서버에 전서버 등록 -> 이후 부터는 호스트네임으로 서버 접속 가능)

[root@linux-1 /]# cat /etc/hosts
192.168.56.101 linux-1
192.168.56.102 linux-2
192.168.56.103 linux-3
192.168.56.104 linux-4

 

key 등록 작업  - 완료

(서버에서 key 생성 후 웹서버에 copy)

[root@linux-1 /]# 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:ew/Rl8QjtKl8uU7ad+RLLBcbCLd1Eq8pnIw9vpkak68 root@linux-1
The key's randomart image is:
+---[RSA 2048]----+
|            . .  |
|           . + o |
|           .+.=.o|
|         . Bo=+*.|
|        S = @o=o |
|         . = =. =|
|        . * +. B |
|         . @ += o|
|          EoO. o.|
+----[SHA256]-----+

 

[root@linux-1 /]# ssh-copy-id root@linux-2
[root@linux-1 /]# ssh-copy-id root@linux-3
[root@linux-1 /]# ssh-copy-id root@linux-4

 

사전 준비는 다 됐고요.

이제 관리할 웹서버들을 ansible 이벤토리에 추가해주겠습니다.

[root@linux-1 ansible]# cat /etc/ansible/hosts
linux-2
linux-3

 

전체 웹서버에 ping을 날려보겠습니다.

ping 모듈을 사용하여 전 서버(all)에 ping을 날립니다. 결과는 성공입니다.

(-m : MODULE_NAME)

[root@linux-1 ansible]# ansible -m ping all
linux-2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
linux-3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

 

이번엔 웹서버의 "/" 경로의 파일 리스트를 "ls -l"로 확인해 보겠습니다.

"-a"는 module에 대한 Argument 값이고요,

module은 "COMMAND"입니다, 디폴트 값이라 명령어에는 생략이 되어있습니다.

풀 명령은 아래와 같습니다.

ansibe -m command -a 'ls -l /' all

[root@linux-1 ansible]# ansible -a 'ls -l /'  all
linux-3 | CHANGED | rc=0 >>
합계 16
lrwxrwxrwx.   1 root root    7  5월  8 23:27 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096  5월  8 23:41 boot
drwxr-xr-x.  19 root root 3100  5월  9 00:38 dev
drwxr-xr-x.  74 root root 8192  5월  8 23:41 etc
drwxr-xr-x.   2 root root    6  4월 11  2018 home
lrwxrwxrwx.   1 root root    7  5월  8 23:27 lib -> usr/lib
lrwxrwxrwx.   1 root root    9  5월  8 23:27 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6  4월 11  2018 media
drwxr-xr-x.   2 root root    6  4월 11  2018 mnt
drwxr-xr-x.   2 root root    6  4월 11  2018 opt
dr-xr-xr-x. 104 root root    0  5월  8 23:42 proc
dr-xr-x---.   4 root root  163  5월  9 00:09 root
drwxr-xr-x.  23 root root  640  5월  9 00:38 run
lrwxrwxrwx.   1 root root    8  5월  8 23:27 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6  4월 11  2018 srv
dr-xr-xr-x.  13 root root    0  5월  8 23:42 sys
drwxrwxrwt.   9 root root  249  5월  9 01:09 tmp
drwxr-xr-x.  13 root root  155  5월  8 23:27 usr
drwxr-xr-x.  19 root root  267  5월  8 23:40 var
linux-2 | CHANGED | rc=0 >>
합계 20
lrwxrwxrwx.   1 root root    7  5월  8 22:25 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096  5월  8 22:35 boot
drwxr-xr-x.  19 root root 3080  5월  9 12:30 dev
drwxr-xr-x.  74 root root 8192  5월  9 12:34 etc
drwxr-xr-x.   2 root root    6  4월 11  2018 home
lrwxrwxrwx.   1 root root    7  5월  8 22:25 lib -> usr/lib
lrwxrwxrwx.   1 root root    9  5월  8 22:25 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6  4월 11  2018 media
drwxr-xr-x.   2 root root    6  4월 11  2018 mnt
drwxr-xr-x.   2 root root    6  4월 11  2018 opt
dr-xr-xr-x. 105 root root    0  5월  9 12:30 proc
dr-xr-x---.   4 root root  163  5월  8 23:41 root
drwxr-xr-x.  23 root root  640  5월  9 12:34 run
lrwxrwxrwx.   1 root root    8  5월  8 22:25 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6  4월 11  2018 srv
dr-xr-xr-x.  13 root root    0  5월  9 12:30 sys
drwxrwxrwt.  10 root root 4096  5월  9 13:51 tmp
drwxr-xr-x.  13 root root  155  5월  8 22:25 usr
drwxr-xr-x.  19 root root  267  5월  8 22:34 var

 

이번엔 서버 /tmp 경로에 있는 data.txt 파일을 웹서버에 copy 해 보았습니다.

이젠 명령어만 봐도 어떤 의미인지 아시죠?

[root@linux-1 ansible]# ansible -m copy -a 'src=/tmp/data.txt dest=/root/'  all
linux-3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/data.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1589000596.66-6124-72370894460726/source",
    "state": "file",
    "uid": 0
}
linux-2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/data.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:admin_home_t:s0",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1589000596.65-6123-38438812925663/source",
    "state": "file",
    "uid": 0
}

 

data.txt 파일 copy 확인

[root@linux-3 ~]# cd /root/
[root@linux-3 ~]# ls
anaconda-ks.cfg  data.txt

 

네. 오늘은 설치 및 예제 몇 가지 확인해 보았고 

다음 시간에는 invertory 설정 내용에 대해 학습 할 예정입니다.

댓글

Designed by JB FACTORY

loading