Ansible 네트워크 자동화 – 1

앤시블(Ansible) 네트워크 자동화 – 1

앤시블 기초

Ansible은 IT 자동화 도구이며, 기본적으로 ssh 프로토콜을 이용한다. 또한 데몬이 실행되지도 않는다.

참고:  SF 소설에서의 Ansible –  앤시블은 원래 SF 소설에서 초광속 실시간 통신을 위한 개념으로 사용했다. 1966년  Ursula K. Le Guin의 소설에서 Ansible이란 단어를 처음 사용했다고 한다. Orson Scott Card의  소설 엔더스 게임이나 듀나의 소설 대리전에서도 앤시블 통신을 사용하는 것을 볼 수 있다.

앤시블 구성

Control Node
ansible이 설치되어 있는 어떤 기계들도 control node가 될 수 있다. control node에서 anaible이나 ansible-playbook 명령을 실행한다. 파이썬이 설치된 어떤 컴퓨터든 control node로 사용할 수 있지만, 윈도우가 설치된 기계는 control node로 사용할 수 없다. 또한 control node를 여러개 가질 수도 있다.

Managed Node
Ansible이 관리하는 네트워크 장치들이나 서버들. Managed node는 때대로 host 라 부르기도 하며, managed node에는 ansible을 설치하지 않는다.

Inventory
Managed node의 목록. 인벤토리 파일은 때때로 hostfile 이라 부른다. inventory 파일은 각 managed node의 정보(ip 주소 등)가 명시된다. managed node를 그룹으로 만들고 중첩시킬 수도 있다.

Collection
collection은 playbook, role, module, plugin을 포함하는 ansible 콘텐츠의 배포 포맷이다. ansible galaxy를 통해 collection을 설치하고 사용할 수 있다.

Modules
ansible 실행 코드의 단위. 각 모듈은 특정 용도를 가진다.

Tasks
ansible의 작업 단위. ad-hoc 명령으로 한번의 task를 실행할 수 있다.

Playbook
순서가 지정된 task 리스트가 저장되어 해당 task를 반복 실행할 수 있다. playbook은 변수와 task를 포함할수도 있다. playbook은 YAML로 작성되어 읽고 쓰고 공유하고 이해하기 쉽다.

앤시블 설치 – Ubuntu 18.04

패키지 설치

# sudo apt install software-properties-common
# apt-add-repository --yes --update ppa:ansible/ansible
# apt install ansible

만약, 아래와 같은 오류가 발생한다면, snap 데몬을 정지 시키고 다시 설치한다.

E: The repository 'http://ppa.launchpad.net/ansible/ansible/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

# systemctl disable snapd
# systemctl stop snapd

실행해 보기

1. Inventory 설정(/etc/ansible/hosts)
inventory에 관리할 서버 또는 장치의 호스트네임이나 IP 주소를 설정한다. 여기에서는 모두 리눅스 박스를 사용한다.

# vi /etc/ansible/hosts

[test]
192.168.100.122
192.168.100.251

known hosts에 등록하기 위해 ansible all -m ping 확인한다.
인벤토리에 등록된 서버에 한번도 ssh 접속을 하지 않았다면, 아래와 같은 메시지가 보인다. 이때, yes를 입력하면 된다.

$ ansible all -m ping
The authenticity of host '192.168.100.251 (192.168.100.251)' can't be established.
ECDSA key fingerprint is SHA256:MwdPQks4J3zZrM8icdPCFrwXH9Xf1QYCcWGLkBFhEZw.
The authenticity of host '192.168.100.122 (192.168.100.122)' can't be established.
ECDSA key fingerprint is SHA256:MwdPQks4J3zZrM8icdPCFrwXH9Xf1QYCcWGLkBFhEZw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
192.168.100.251 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.100.251' (ECDSA) to the list of known hosts.\r\nsnowfox@192.168.100.251: Permission denied (publickey,password).",
    "unreachable": true
}

192.168.100.122 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Host key verification failed.",
    "unreachable": true
}

이제, ansible 명령이 실행되는지 확인한다.

$ ansible all -m ping -k
SSH password:
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 192.168.100.251 should use /usr/bin/python3, but is using
/usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using
 the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This
feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
 ansible.cfg.
192.168.100.251 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.100.122 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

호스트들을 그룹화하려면, [그룹이름] 형식으로 그룹을 만든다.
이제 test2 그룹을 하나 더 만들고 ansible 명령이 작동하는지 확인한다.

# vi /etc/ansible/hosts

[test]
192.168.100.122
192.168.100.251

[test2]
192.168.0.42
192.168.100.122

all 대신 그룹 이름을 명시하면 해당 그룹에만 명령이 실행된다. 한 호스트가 서로 다른 그룹에 존재할 수도 있다.

$ ansible test2 -m ping -k
SSH password:
192.168.0.42 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.100.122 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

2. 앤시블 설정 파일
앤시블 설정 파일은 /etc/ansible/ansible.cfg 에 있으며, 인벤토리 파일이나 연결 포트 등을 설정할 수 있다. 그러나 파일을 확인해보면 모든 내용이 주석처리 되어 있는 것을 볼 수 있다. 설정파일의 우선순위는 환경변수 ANSIBLE_CONFIG, 현재 작업 디렉토리 안의 ansible.cfg 홈디렉토리의 .ansible.cfg /etc/ansible/ansible.cfg 중에서 먼저 찾은 항목이다. 또한 대부분의 파라메타는 ansible-playbook 이나 커맨드라인 플래그로 다시 정의 할 수 있다.

3. 새 인벤토리 파일 만들기
홈 디렉토리에 디렉토리를 새로 만들고 인벤토리 파일을 만든다.(/home/snowfox/test_ansible/hosts)

$ mkdir test_ansible;cd test_ansible
$ vi hosts
[real]
192.168.100.122
192.168.100.251

[virtual]
192.168.0.42

새로 만든 인벤토리 파일을 실행해 본다.

$ ansible -i hosts all -m ping -k
SSH password:
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 192.168.100.251 should use /usr/bin/python3, but is using
/usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using
 the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This
feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
 ansible.cfg.
192.168.100.251 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.100.122 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.42 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
snowfox@seafox1:~/test_ansible$ ansible -i hosts virtual -m ping -k
SSH password:
192.168.0.42 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

$ ansible -i hosts 192.168.0.42 -m ping -k
SSH password:
192.168.0.42 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

4. 인벤토리 기초 : 포맷, 호스트, 그룹
인벤토리 파일은 다양한 포맷을 가질 수 있으며, 보통 INI 나 YAML 포맷이다. 기본 인벤토리 파일은 /etc/ansible/hosts 포맷이다.

$ vi hosts
[real]
192.168.100.122
192.168.100.251

[virtual]
192.168.0.42

위에서 만든 인벤토리와 동일한 YAML 포맷 인벤토리는 다음과 같다.

$ vi hosts.yaml

---
real:
  hosts:
    192.168.100.122:
    192.168.100.251:

virtual:
  hosts:
    192.168.0.42:

실행

$ ansible -i hosts.yaml all -m ping -k
SSH password:
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 192.168.100.251 should use /usr/bin/python3, but is using
/usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using
 the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This
feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
 ansible.cfg.
192.168.100.251 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.100.122 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.42 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

참고문서: 참고문서: https://docs.ansible.com/ansible/latest/index.html

답글 남기기

Your email address will not be published.