본문 바로가기

Devops/Ansible

[Ansible] docker를 이용하여 간단한 ansible 환경 구성하기!

docker란 참 편리한 툴이다.

원하는 환경을 그냥 뚝딱 만들어주니....

 

준비사항


docker

docker-compose

centos7(다른 os도 무관)

 


docker-compose.yml 파일 작성


docker run으로 하여 일일이 생성해도 괜찮지만 귀찮으니 간단한 compose파일을 만들어서 생성 시키도록 하겠다.

ansible-main은 엔서블 코어를 설치할것이고, 나머지 ansible-1,2는 node용으로 사용할것 이다.

 

그리고 docker image는 sshd를 사용해야 하니 centos7에 sshd가 설치된 이미지를 사용한다.

$ vi docker-compose.yml

version: "3" 

services:
  ansible-main:
    container_name: ansible-main
    image: "ncc0706/centos7-sshd"
    restart: always
    hostname: "ansible-main"

  ansible-1:
    container_name: ansible-1
    image: "ncc0706/centos7-sshd"
    restart: always
    hostname: "ansible-1"

  ansible-2:
    container_name: ansible-2
    image: "ncc0706/centos7-sshd"
    restart: always
    hostname: "ansible-2"

컨테이너 생성 및 확인


docker-compose 파일이 있는 디렉터리에서 아래 명령어를 입력하면 컨테이너 생성이 시작된다.

$ docker-compose up -d

 

설치가 완료되면 ps를 사용하여 확인해준다.

$ docker ps

 

이제 ansible을 설치하기전에 ssh client설치와 ssh로 패스워드 로그인을 해야하므로 패스워드를 설정해주자.

패스워드는 일단 동일하게 설정해준다.

# in ansible-main, ansible-1,2
$ yum -y install openssh-clients

$ passwd
Changing password for user root.
New password: 
Retype new password:

 


Ansible-core 설치


자 이제 ansible-main container로 들어가서 ansible을 설치해보자.

$ sudo docker exec -it $CONTAINER_ID /bin/bash

 

$ yum -y install epel-release
$ yum -y install ansible

 

설치가 잘 됐는지 확인해보자!

$ ansible --version
ansible 2.9.25
  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, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

 


Ansible로 ping 날려보기


이제 한번 ansible-1,2로 한번에 ping을 날려보자.

$ ansible all -m ping
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 

 

아무 설정도 안해주니 에러가 나는것을 확인할 수 있다.

위 에러는 /etc/ansible/hosts 파일에 Targer server들을 설정을 안해줘서 에러가 나는것이다.

이제 설정해보자.

 

본격적으로 설정하기전에 hosts파일에 아래와 같이 등록해주어 매번 ip를 입력하지않게 등록해준다.

$ vi /etc/hosts
...
172.20.0.4      ansible-1
172.20.0.2      ansible-2

 

hosts 파일 맨끝으로 이동하여 아래와 같이 설정해주었다.

$ vi /etc/ansible/hosts
...
...
# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

ansible-1
ansible-2

 

이제 다시 명령어를 입력해보자.

그러면 known_hosts 파일에 해당 시스템의 공개키(Public Key)의 등록이 이루어지니 yes를 2번(타겟 서버 갯수) 입력한다.

ansible all -m ping
The authenticity of host 'ansible-1 (172.20.0.4)' can't be established.
...
Are you sure you want to continue connecting (yes/no)? yes
ansible-1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'ansible-1,172.20.0.4' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
yes
ansible-2 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'ansible-2,172.20.0.2' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}

 

그리곤 이제 -k 옵션을 붙여서 패스워드를 묻게 한다.

그러면 아래와 같이 패스워드를 입력하면 성공적으로 ping을 보냈다.

$ ansible all -m ping -k
SSH password: 
ansible-1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
ansible-2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}