Ansible 설치 및 사용법

쌈지몬·2022년 7월 4일
0

Ansible이란?
Ansible 설치 및 사용법
Ansible Tower 소개 및 주요 기능

저번 포스팅에서는 Ansible의 주요 개념 및 기능, 구조에 대해서 언급한 바 있다. 이번 포스팅에서는 해당 Ansible을 사용하기 위한 설치 과정 및 실제 사용법에 대해서 간단한 예시를 들어 설명하도록 하겠다.

1. Ansible 설치

OS: Ubuntu 18.04

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

OS: CentOS 8

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum install ansible

ansible이 제대로 설치되었는지는 ansible 명령을 통해 확인 가능

ansible -h

2. Inventory 설정

ansible은 Inventory라고 하는 작업을 수행할 서버들에 대한 정보를 저장하고 있는 hosts 파일을 설정하도록 되어 있다. 기본 경로는 "/etc/ansible/hosts"

Inventory 예시

[test_servers]
192.168.0.1
192.168.0.2
192.168.0.3

3. Playbook 설정

ansible 실행에 필요한 Script를 정의한다. 기본적으로 하나의 playbook은 여러개의 task가 들어갈 수 있으며 해당 task가 ansible 명령의 실행 단위이다.

Playbook 예시

---

- name: Network Getting Started First Playbook
  connection: ansible.netcommon.network_cli
  gather_facts: false
  hosts: all
  tasks:

    - name: Get config for VyOS devices
      vyos.vyos.vyos_facts:
        gather_subset: all

    - name: Display the config
      debug:
        msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

추가적인 playbook 예시는 해당 링크 를 참고할 것

4. Ansible 실행

Inventory, Playbook 구성이 완료되었으며 이제 Ansible을 실행할 차례이다. 해당 Playbook이 있는 위치로 이동하여 Ansible 명령을 실행한다.

ansible-playbook playbook.yaml

ansible이 정상적으로 실행되는 것을 확인할 수 있다.

에러 발생 시 대응

(1) To use the ‘ssh’ connection type with passwords, you must install the sshpass program


sshpass가 설치되어 있지 않아서 발생하는 에러로 sshpass 패키지를 설치해주면 해결된다.

Ubuntu

apt install sshpass

CentOS

yum install sshpass

참고

https://docs.ansible.com/ansible/latest/network/getting_started/first_playbook.html
https://github.com/ansible/ansible-examples

profile
What goes around comes around.

0개의 댓글