Ansible은 Agentless 방식으로 서버에 ssh로 접속하여 명령을 실행합니다.
서버는 Python과 Ansible, 호스트는 Python만 설치합니다.
Python
파이썬 버전 확인
python3 --version
Python 3.8.2
파이썬 버전 3.8 이상으로 설치해주세요. 만약 다른 메세지가 나온다면 링크를 참고하여 설치를 진행하세요.
[ 참고 ]
sudo apt install python3.8
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
sudo update-alternatives --config python3
출력에는 사용 가능한 선택 항목과 할당된 번호가 표시됩니다
사용하려는 버전 번호를 입력하고 Enter 키 를 누릅니다 .
ansible localhost -m ping
[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
자기 자신에게 Ping을 해봐요.
Key
서버에서 호스트로 접속 할 Key를 생성하여 호스트로 배포합니다.
생성 (서버)
Key 생성은 서버에서만 진행합니다.
Key 생성
ubuntu@ip-172-31-26-106:~$ cd ~/.ssh
ubuntu@ip-172-31-26-106:~/.ssh$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): ansible
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ansible
Your public key has been saved in ansible.pub
The key fingerprint is:
SHA256:Cj0xlHqj1ORiFOZl5od19eoZ7v/iadpa6BFv0dvoEAE ubuntu@ip-172-31-26-106
The key's randomart image is:
+---[RSA 3072]----+
| o.=.. E.. |
| o.*oo . . . |
| ..== . . . |
| =.=+ o . |
| o.+o.S = . .|
| .. o o B oo|
| . B =..|
| o Bo. |
| =**o.|
+----[SHA256]-----+
Ansible은 SSH 접속 시 기본 Key 파일 이름인id_rsa을 찾아 접속을 시도합니다. Key 파일 이름을 변경하는 경우, Key파일을 선택합니다.
[web]
10.10.10.10
10.10.10.11
[web:vars]
ansible_port=20022
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
# example
ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
Ansible - SSH
서버에서 호스트로 연결이 되는지 확인합니다.
ansible all -m ping -u ubuntu
모든 호스트의 ubuntu 사용자를 대상으로 ping 모듈을 수행합니다.
10.10.10.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 10.10.10.11 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.10/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.
10.10.10.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
DEPRECATION WARNING은 그룹 변수 설정 시 파이썬 인터프리터를 특정하는 것으로 해결할 수 있습니다.