본문 바로가기

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

5. Ansible 구성

# Ansible 서버 구성

- docker 이미지 실행하기

docker run --privileged -itd --name ansible-server -p 20022:22 -p 8081:8080 -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup [docker-hub-id]/ansible:latest /usr/sbin/init

- 로컬 환경에서 ansible을 조회하면 아래와 같은 결과를 확인할 수 있다.

seungkim@DESKTOP-BUANL37:~$ sudo docker ps -a | grep ansible
566ba96afd13  edowon0623/ansible:latest  "/sbin/init systemct…"  11 minutes ago  Up 9 minutes  0.0.0.0:20022->22/tcp, :::20022->22/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp  ansible-server

그리고 해당 컨테이너에 ssh를 통해 접근하는 방법은 아래와 같다.

ssh root@localhost -p 20022

password : P@ssw0rd

그리고 해당 컨테이너의 정보를 확인하면 아래와 같다.

[root@566ba96afd13 ~]# hostname
566ba96afd13
[root@566ba96afd13 ~]# ansible --version
ansible [core 2.13.4]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.8 (default, Aug 25 2021, 16:13:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.2
  libyaml = True

1. Ansible이 관리하는 대상 설정하기

ls -al /etc/ansible/hosts

- 위의 파일이 없다고 나오면 아래의 작업과 동일하게 진행한다.

해당 폴더를 생성한다.

mkdir /etc/ansible/

- 파일을 생성한다.

생성한 /etc/ansible/ 폴더로 이동 후 아래의 명렁으롤 활용하여 파일을 생성한다.

vi hosts

- hosts 파일에 아래의 내용을 추가한다.

[devops]
172.17.0.3
172.17.0.4

- 현재 까지의 ansible 구성을 보면 아래와 같다.

- 만약 docker-server가 포트 충돌등 으로 실행이 안되어 있다면 아래의 명령어를 활용하여 실행한다.

sudo docker run --privileged --name docker-server -itd -p 10022:22 -p 8082:8080 -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup [docker-hub-id]/docker:latest /usr/sbin/init

local docker env.drawio
0.00MB

2. ssh-keygen을 활용한 서버 접근

- Ansible 서버에서 실행 해야한다. (172.17.0.3)

- ssh-keygen을 활용하여 접근 구성을 하면 패스워드 없이 바로 접근이 가능하다.

- key 생성 방법은 아래와 같다.

ssh-keygen

ssh-copy-id root@[접속할 서버 IP]

ssh-copy-id root@172.17.0.4

172.17.0.4 과 172.17.0.3 번으로 ssh 키를 복사하고 접근을 해보자.

[root@566ba96afd13 ansible]# ssh-copy-id root@172.17.0.4
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.4's password:

Number of key(s) added: 1

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

[root@566ba96afd13 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.
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.

 

위의 작업이 끝난뒤에 패스워드 없이 ansible 서버에서 접근을 해보자.

아래와 같이 정상적으로 접근이 가능한것을 확인 할 수 있다.

[root@566ba96afd13 ansible]# ssh root@172.17.0.3
Last login: Tue May  9 00:54:18 2023 from 172.17.0.1

[root@ansible-server ~]# ssh root@172.17.0.4
Last login: Tue May  9 05:02:30 2023 from 172.17.0.3

ssh-keygen을 활용하여 키를 생성하고 해당 키를 접속 하려는 서버에 복사하여 패스워드 없이 접근이 가능하도록 설정 하였다.

- ansible 특징 : 멱등성 : 같은 설정을 여러번 적용 하여도 한번만 실행되어 결과가 달라지지 않는다.

 

- 끝 -