ansible 설치(Ubuntu)

작업 순서

  1. 설치 준비
  2. Ansible 설치
  3. ssh 키 생성
  4. inventory / 관리 서버 - host/group 설정

설치 준비

1. python3 / pip 버전 확인

# 버전 확인
python3 -m pip -V

# pip 설치 - 찾을 수 없는 경우
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user

2. python3 버전 업그레이드(ubuntu)

https://www.debugpoint.com/install-python-3-11-ubuntu/

// 설치
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install software-properties-common -y
sudo apt install python3.11

# 버전 선택
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
sudo update-alternatives --config python3

# 버전 확인
python3 -m pip -V                                      

Ansible 설치

# 설치
python3 -m pip install --user ansible

# 설치 버전 확인
ansible --version
python3 -m pip show ansible

설치 중 아래와 같은 메세지가 출력이되면 .bashrc 파일에 ansible 경로를 추가한다.

  WARNING: The script ansible-community is installed in '/home/jj/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

.bashrc 편집

vi .bashrc

# path에 경로 추가
LOCAL_BIN=/home/jj/.local/bin
export PATH=$JAVA_HOME/bin:$LOCAL_BIN:$PATH

# 적용 또는 재 로그인
source .bashrc 

ssh 키 생성

  • 관리 대상 서버와 ssh 통신에서 사용할 private/public 키 생성
# 키 생성
ssh-keygen -t rsa -b 4096 -f ~/.ssh/server_rsa

# public 키 복사
ssh-copy-id vagrant@192.168.0.20

inventory - host/group 설정

[all:vars]
ansible_connection=ssh
ansible_user=vagrant

[server]
192.168.0.20
192.168.0.21

또는

[server]
192.168.0.20    ansible_connection=ssh  ansible_user=vagrant

확인

// ping 테스트
ansible server -m ping

// 경로 지정
ansible server -i test-server-hosts  -m ping

// 개별 테스트
ansible 192.168.0.20 -m ping

+ Recent posts