Linux & Development

Install Ansible on CentOS

DaehanCNI 2020. 8. 23. 20:32

 

1. Install Python3

Centos 8 이상 버전 부터는 Python3 이 기본적으로 설치 되어져 있지만 때때로 설치시 구성 및 설정에 따라 설치가 되어져 있지 않는 경우가 있다. 그러므로 아래 방법을 통해 설치 또는 설치 여부를 확인 하여야 한다.

 

1.1 최신 버전 업데이트 

#sudo dnf update

 

1.2 Python3 설치

#sudo dnf install python3

 

1.3 Python3 설치 확인

#python3 -V
Python 3.6.8

 

2. Install PIP – The Python Package Installer

PIP는 python 패키지 관리 매니저이다. 이는 Ansible 설치 이전에 선 설치하여야 한다. 

 

# dnf install python3-pip

 

※ 사용중인 OS가 RHEL8일 경우, Ansible 설치를 위해서는 Pip를 설치하면 안된다. 그 이유는 CentOS 8에서 Ansible 설치를 위해 EPEL 리파짓토리(repository)를 사용하며 정상적인 동작한다. 하지만 이는 공식적인 패키지(official package)는아니다. 그러므로 ansible 설치를 위해 PIP (the Python package manager)를 사용한다.

 

 그러므로 아래 설치버전의 ansible을 RHEL8 에서의 설치를 위해서는 Redhat에서 제공되는 리파짓토리(repository)를 활성화하여 설치하여야 한다.

 

 RHEL 8에서 Ansible 설치를 위한 리파짓토리 (repository) 활성화는 아래 명령어를 통하여 수행할 수 있다. 

# subscription-manager repos --enable ansible-2.8-for-rhel-8-x86_64-rpms

 

3. Install the Ansible Automation Tool

 Step 2을 통하여 Ansible을 설치할 수 있지만 추가적으로 ssh 접속을 위한 sshpass와 같은 패키지가 설치되기 위해서는 epel 리파지토리가 추가되어 있어야 한다. 

# sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

위에 모든 단계를 수행하였다면 아래 명령어를 통해 Ansible을 설치할수있다.

# pip3 install ansible --user

RHEL 8에서 Ansible 설치할 경우 아래 명령어를 통해 수행할 수 있다. 

# dnf -y install ansible

or

# yum -y install ansible

설치가 완료되었다면 아래 명령어를 수행하여  설치한 Ansible 버전을 확인할수있다.

# ansible --version
ansible 2.9.12
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

 

4. Testing the Ansible

Ansible이 동작하기 위해서는 ssh이 정상적으로 활성화 되어 동작하여야 한다. 동작 여부는 다음과 같은 명령어를 통해 수행할수있다.

# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-08-19 03:27:51 EDT; 1h 44min ago
 	Docs: man:sshd(8)
       	man:sshd_config(5)
 Main PID: 18252 (sshd)
	Tasks: 1 (limit: 5058)
   Memory: 6.9M
   CGroup: /system.slice/sshd.service
       	└─18252 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-12>
 
Aug 19 03:27:51 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...
Aug 19 03:27:51 localhost.localdomain sshd[18252]: Server listening on 0.0.0.0 port 22.
Aug 19 03:27:51 localhost.localdomain sshd[18252]: Server listening on :: port 22.
Aug 19 03:27:51 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
Aug 19 03:54:23 localhost.localdomain sshd[50885]: Accepted password for root from 192.168.122.1 port 55578 ssh2
Aug 19 03:54:24 localhost.localdomain sshd[50885]: pam_unix(sshd:session): session opened for user root by (uid=0)

 

ssh가 정상적으로 동작중이라면 /etc/ansible 디렉토리를 생성하고 host 장치 즉 Ansible의 명령어가 수행되는 노드들을 hosts 파일에 정의하여야 한다.

방법은 아래 과정을 참고하면 된다.

# mkdir /etc/ansible  
# cd /etc/ansible
# vi hosts
192.168.1.11
192.168.1.12
192.168.1.13

특정 항목을 정의하여 리모트 호스트를 정의하고자 할때에는 [항목명] 을 추가 하여 정의할수있다.

 

Example)

#cat hosts
[web]
192.168.1.11
192.168.1.12
192.168.1.13

Ansible의 명령어는 ssh 프로토콜을 통해 수행되며 이를 위해 Ansible을 수행하는 master노드에서 ssh 키를 생성하고  퍼블릭 키를 Ansible의 명령어가 수행되는 노드들에 복사한다.

$ ssh-keygen
$ ssh-copy-id 192.168.1.11
$ ssh-copy-id 192.168.1.12
$ ssh-copy-id 192.168.1.13

 

아래 Ansible 명령어를 이용해 Ansible의 명령어가 수행되는 리모트 노드에 ping 을 수행한다.

# ansible all -m ping
192.168.1.13 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/libexec/platform-python"
	},
	"changed": false,
	"ping": "pong"
}
192.168.1.11 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/libexec/platform-python"
	},
	"changed": false,
	"ping": "pong"
}
192.168.1.12 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/libexec/platform-python"
	},
	"changed": false,
	"ping": "pong"
}

 

-i 옵션을 이용하여 특정 호스트 파일(인벤토리 파일)을 지정할수하여 명령어를 수행할수있으며 호스트 파일(인벤토리 파일)에서트상위에서 설명한데로 특정 항목의 리스트만 수행할수있다.

 

Example)

#cat hosts
192.168.1.11
 
[web]
192.168.1.12
192.168.1.13

#ansible -i /etc/ansible/hosts web -m ping 
192.168.1.12 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/libexec/platform-python"
	},
	"changed": false,
	"ping": "pong"
}
192.168.1.13 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/libexec/platform-python"
	},
	"changed": false,
	"ping": "pong"
}