[Ansible] How to use ansible

HyunDong Lee·2021년 5월 10일
0

Ansible

목록 보기
1/1
post-thumbnail

Ansible Architecture Details

Ansible automation engine is installed on local(server) machine
수백 - 수천 수만의 노드 기계와 네트워크 장비들이 configuration의 대상물
Module : 각 노드가 정해진 configuration이 되도록 하는 명령 프로그램
Playbook : module들을 포함한 각 노드들의 configuration을 지휘하는 매뉴얼로서 각 노드들이 유지해야 하는 policy나 IT process step들을 기재함
Ansible 엔진이 playbook에 따라 각 노드들에게 ssh 연결을 통해 해당 module을 push
Inventory : Ansible이 관리하는 노드들의 이름을 모아둔 text file

Ansible property

  • 무료, open source (기업용은 유료)
  • Agentless
  • SSH connection for operations
  • System requirement is low
  • Python 언어로 구현됨
  • Lightweight. Quick deployment
  • Configuration 파일은 YAML 구문 사용(선언적 구문)
  • 넓은 사용자 그룹

Ansible master와 node들 사이의 ssh 연결

Root가 아닌 일반 사용자로서 ansible 운영 필요하다. Ansible 운영자(master/nodes)들 사이에서 ssh연결을 한다. Ansible 운영자에 sudo 권한 필요하다. master와 node 기계 사이에서 ssh 연결에 필요한 key 공유.
ssh-keygen
cat ~/.ssh/id_rsa.pub
ssh-copy-id private_ip_address
노드에 ssh 연결을 위해 node의 password 설정이 필요하다. (root에서 passwd root)
매 번 SSH 연결 때마다 passwrod 묻지 않고 자동으로 작업할 수 있도록 노드의 ssh demon configuration 수정한다.


Ansible 설치

//root에서 설치한다!
$ apt-get update
$ apt-get install -y ansible
$ ansible --version

Inventory

$ vi /etc/ansible/hosts
$ ansible --list-host all
$ ansible all -m ping


연결할 노드의 private ip주소를 다음과 같이 아래에 넣는다.

처음에는 연결이 안되있기 때문에 다음과 같은 코드를 실행시킨다.

$ ssh-keygen
$ ls -la
$ ls .ssh
$ ssh-copy-id <private ip address of nodes>

근데 이렇게 해도 passwd를 입력하라고 명령어가 나올 수 있다.

//nodes에서 실행
$ passwd root //비밀번호 설정
$ ls -la /etc/ssh
$ vi /etc/ssh/sshd_config
/*PasswordAuthentication yes
*PermitEmptyPasswords yes
*PermitRootLogin yes
*/
$ systemctl restart sshd

//master에서
$ ssh-copy-id <private ip address of nodes>
$ ssh <private ip address>

다음 코드를 실행한 후 아래 명령어로 연결이 됐는지 확인한다.

연결됨!

Excercise

간단한 text파일을 node로 전송해본다.

//master에서
$ vi trivial.txt
/*
hello hongik
bye hong
*/
$ vi test.yml
/*
---
- name : deploy a file
  hosts : webservers
  gather_facts : true
  tasks :
          - name : copy trivial file
            copy : src=/root/trivial.txt dest=/root/yap.txt
*/
$ ansible-playbook test.yml --syntax-check
$ ansible-playbook test.yml

//node에서 확인
$ cat yap.txt

잘 도착했다.!


Playbook

Playbook Example

#demo1.yaml
---
- name : install apache2 & php
  hosts : webservers

  gather_facts : true
  tasks :
          - name : "install apache2"
            package : name=apache2 state=present
          - name : "install apache2-php7"
            package : name=libapache2-mod-php state=present
          - name : "install php-cli"
            package : name=php-cli state=present
          - name : "install php-gd"
            package : name=php-gd state=latest
          - name : start apache2
            service : name=apache2 state=restarted

터미널

$ ansible-playbook demo1.yml --syntax-check
$ ansible-playbook demo1.yml

결과 화면

node의 public ip 주소로 접속

apache 서버 접속 성공!!

0개의 댓글