티스토리 뷰

CloudNet@의 가시다님 Ansible 1기 스터디에 참여하게 되어 배운 내용과 책의 내용을 함께 정리합니다.

📚 앤서블로 시작하는 인프라 자동화

 

앤서블로 시작하는 인프라 자동화 | 장현정 - 교보문고

앤서블로 시작하는 인프라 자동화 | 효율적인 IT 자동화를 위한 도구, 앤서블 설계부터 응용까지 단계별로 배우는 인프라 관리클라우드 컴퓨팅을 논할 때 IaC(Infrastructure as Code)를 빼놓을 수 없는

product.kyobobook.co.kr

1. 앤서블 갤럭시

도커 허브와 비슷한 역할을 하는 롤을 공유하는 목적의 레파지토리라고 생각하시면 될 것 같습니다. 다만 롤은 검증되지 않은 것이 대부분이기 때문에 사용 시 주의해야 합니다.

 

Ansible Galaxy

 

galaxy.ansible.com

1.1. 명령어를 이용한 앤서블 갤럭시 활용

명령어를 통해 앤서블 갤럭시로부터 롤을 가져올 수 있습니다.

 

1. 롤 서브 명령어 확인

ansible-galaxy role -h
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the actual GitHub repository.
    list       Show the name and version of each role installed in the roles_path.
    search     Search the Galaxy database by tags, platforms, author and multiple keywords.
    import     Import a role into a galaxy server
    setup      Manage the integration between Galaxy and the given source.
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy

options:
  -h, --help   show this help message and exit

 

2. 롤 검색

ansible-galaxy role search postgresql --platforms Ubuntu
Found 270 roles matching your search:

 Name                                                 Description
 ----                                                 -----------
 aaronpederson.postgresql                             PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active>
 alainvanhoof.alpine_postgresql                       PostgreSQL for Alpine Linux
 alikins.postgresql                                   PostgreSQL server for Linux.
...

 

3. 롤 상세 정보 확인

ansible-galaxy role info geerlingguy.postgresql
Role: geerlingguy.postgresql
        description: PostgreSQL server for Linux.
        commit: a7723eb017c618611da37531e5961632d9ae03b0
        commit_message: Fedora support time for 37 and 38.
        created: 2023-05-08T20:49:59.794667Z
        download_count: 2454875
        github_branch: master
        github_repo: ansible-role-postgresql
        github_user: geerlingguy
        id: 10986
        imported: 2023-06-15T23:23:15.275804-04:00
        modified: 2023-10-29T18:44:43.644380Z
        path: ('/root/.ansible/roles', '/usr/share/ansible/roles', '/etc/ansible/roles')
        upstream_id: 12427
        username: geerlingguy

 

4. 롤 가져오기

my-ansible/roles/...

# -p 옵션으로 롤이 설치될 디렉터리 경로 지정
ansible-galaxy role install -p roles geerlingguy.postgresql

 

  • 실습에 사용된 롤이 예전 facts를 사용하므로 ansible.cfg 파일에서 inject_facts_as_vars = false 파라미터를 제거해줍니다.
# (옵션) 앤서블 환경 설정에 롤 디렉터리 설정
## my-ansible/ansible.cfg 파일 편집
[defaults]
inventory = ./inventory
remote_user = root
ask_pass = false
roles_path = ./roles
...

inject_facts_as_vars = false <- 실습 사용 롤이 예전 facts 를 사용하니 이 옵션은 제거하자
  • 가져온 롤의 리스트를 확인해보면 WARNING 메시지가 뜨는 것을 볼 수 있는데, 이는 ansible.cfg 파일에 roles_path를 설정해주지 않아서입니다.

  • roles_path = ./roles 라는 파라미터를 추가해주면 해당 경고메시지가 사라지는 것을 확인할 수 있습니다.

 

5. 가져온 롤을 이용하여 설치

my-ansible/role-galaxy.yml

---
- hosts: tnode1

  roles:
    - geerlingguy.postgresql

 

도전과제7

  • 앤서블 갤럭시에서 관심 있는 롤을 검색하여 해당 롤을 사용하는 플레이북을 만들어서 롤을 통한 애플리케이션을 배포해보자

jenkins를 앤서블 갤럭시에서 찾아 설치해보도록 하겠습니다. 다운로드가 많은 순으로 정렬해서 제일 상단에 있는 롤을 설치합니다.

 

찾은 롤을 roles라는 디렉토리에 설치하는 옵션을 주어 설치합니다.

 

가져온 롤을 사용하는 간단한 플레이북을 만들어 설치해봅니다.

---
- hosts: tnode2

  roles:
    - geerlingguy.jenkins

 

바로 플레이북을 실행했더니 다음과 같은 어마무시한 오류가 발생했습니다. 

 

문제를 해결하기 위해 앤서블 갤럭시 페이지로 돌아가 Github의 README를 천천히 살펴보다가 마지막 부분에서 Example Playbook을 발견했습니다. 예제를 통해 jenkins를 실행하기 위해서는 java 설치가 함께 진행되어야 한다는 것을 알 수 있었습니다. (become: true는 root 권한으로 해당 플레이북을 실행할 수 있는 옵션입니다. ansible 버전이 업데이트되면서 sudo 대신 사용됩니다.)

- hosts: jenkins
  become: true
  
  vars:
    jenkins_hostname: jenkins.example.com
    java_packages:
      - openjdk-8-jdk

  roles:
    - role: geerlingguy.java
    - role: geerlingguy.jenkins

 

예제의 플레이북대로 실행하기 위해 geerlingguy.java라는 롤도 설치해주어야 합니다.

 

플레이북을 위의 예제와 같이 수정하고, host만 그대로 tnode2로 사용하여 다시 플레이북을 실행하면 같은 dpkg 오류가 발생하였습니다.  구글링해서 다음과 같은 해결책을 찾았고 tnode2에 그대로 적용해주었습니다.

 

dpkg fails on Ubuntu Server 22.04 LTS

My Ubuntu 22.04 LTS seems to have problems with dpkg and I can't find anyone online that has the same issue as me. It makes me not able to install packages and I just don't know how to fix this. Se...

stackoverflow.com

 

롤의 태스크 메인 파일의 jenkins_hostname 변수를 사용하는 부분을 127.0.0.1로 수정하여 플레이북을 실행해보았습니다. (실제 사용할 수 있는 도메인이 생성되어 있지 않기 때문입니다.) 결과적으로 플레이북이 매끄럽게 실행되었고 tnode2에 접속하여 jenkins의 상태를 체크해보니 원활히 실행되고 있는 모습을 확인할 수 있었습니다.

 

실제 tnode2 주소의 8080 포트로 접속을 해보니 jenkins 화면이 뜨는 모습 또한 확인할 수 있었습니다.

 

사용을 마친 롤은 삭제해줍니다.

2. 콘텐츠 컬렉션

앤서블이 처음 개발되었을 때는 사용되는 모듈이 모두 핵심 소프트웨어 패키지의 일부로 포함되었습니다. 그런데 모듈 수가 늘어나면서 업스트림 프로젝트에서 모듈을 관리하기가 더 어려워졌습니다. 모든 모듈에는 고유한 이름이 필요하고, 모듈 업데이트를 핵심 앤서블 코드에 대한 업데이트와 동기화해야 했습니다.

앤서블 콘텐츠 컬렉션을 사용하면 핵심 앤서블 코드 업데이트와 모듈플러그인에 대한 업데이트가 분리됩니다. 그리고 플레이북에서 사용할 수 있는 일련의 관련 모듈, 역할, 기타 플러그인을 제공합니다. 그렇기 때문에 벤더개발자앤서블 릴리스독립적으로 컬렉션을 자신의 속도에 맞게 유지, 관리하고 배포할 수 있습니다.

앤서블 콘텐츠 컬렉션을 사용하면 유연성도 향상됩니다. 지원되는 모듈을 모두 설치하는 대신 필요한 콘텐츠설치할 수 있습니다. 특정 버전(이전 버전 또는 이후 버전)의 컬렉션을 선택하거나 레드햇 또는 벤더가 지원하는 컬렉션 버전 또는 커뮤니티에서 제공하는 버전 중에서 선택할 수도 있습니다.

Ansible 2.9 이상콘텐츠 컬렉션지원합니다. 업스트림 앤서블은 Ansible Base 2.10 및 Ansible Core 2.11의 코어 Ansible 코드에서 대부분의 모듈을 번들 해제하고 컬렉션에 배치했습니다. 레드햇 앤서블 오토메이션 플랫폼 2.2는 자동화 실행 기능을 상속하는 Ansible Core 2.13 기반의 자동화 실행 환경을 제공합니다.

 

Collection Index — Ansible Documentation

© Copyright Ansible project contributors. Last updated on Jan 25, 2024.

docs.ansible.com

 

2.1. 명령어를 이용한 앤서블 콘텐츠 컬렉션

1. 명령어 확인

ansible-galaxy collection -h
usage: ansible-galaxy collection [-h] COLLECTION_ACTION ...

positional arguments:
  COLLECTION_ACTION
    download         Download collections and their dependencies as a tarball for an offline install.
    init             Initialize new collection with the base structure of a collection.
    build            Build an Ansible collection artifact that can be published to Ansible Galaxy.
    publish          Publish a collection artifact to Ansible Galaxy.
    install          Install collection(s) from file(s), URL(s) or Ansible Galaxy
    list             Show the name and version of each collection installed in the collections_path.
    verify           Compare checksums with the collection(s) found on the server and the installed copy. This does not verify dependencies.

options:
  -h, --help         show this help message and exit

 

2. 앤서블이 설치된 프로젝트 환경에 설치된 컬렉션 확인

ansible-galaxy collection list

# /usr/lib/python3/dist-packages/ansible_collections
Collection                    Version
----------------------------- -------
amazon.aws                    6.5.0  
ansible.netcommon             5.3.0  
ansible.posix                 1.5.4  
ansible.utils                 2.12.0 
ansible.windows               1.14.0 
arista.eos                    6.2.2  
awx.awx                       22.7.0 
azure.azcollection            1.19.0 
...

tree /usr/lib/python3/dist-packages/ansible_collections -L 1
/usr/lib/python3/dist-packages/ansible_collections
├── amazon
├── ansible
├── ansible_community.py
├── ansible_release.py
├── arista
├── awx
├── azure
├── check_point
├── chocolatey
...

 

3. 컬렉션 삭제 및 설치

#
ansible-galaxy collection list openstack.cloud
# /usr/lib/python3/dist-packages/ansible_collections
Collection      Version
--------------- -------
openstack.cloud 2.2.0

tree /usr/lib/python3/dist-packages/ansible_collections/openstack -L 3
...

# 삭제
sudo rm -rf /usr/lib/python3/dist-packages/ansible_collections/openstack*

# 확인
ansible-galaxy collection list openstack.cloud
ansible-galaxy collection list


# 특정 버전으로 설치
ansible-galaxy collection install openstack.cloud:2.1.0

# 설치 확인
ansible-galaxy collection list
ansible-galaxy collection list openstack.cloud
# /root/.ansible/collections/ansible_collections
Collection      Version
--------------- -------
openstack.cloud 2.1.0

tree /home/ubuntu/.ansible/collections/ansible_collections -L 3
...

 

4. 오프라인 환경 지원을 위해서, 컬렉션을 tar 파일 형태로 다운로드 가능

# -p 디렉터리 지정
ansible-galaxy collection download -p ./collection openstack.cloud

# 확인
tree collection/
collection/
├── openstack-cloud-2.2.0.tar.gz
└── requirements.yml

tar tvf collection/openstack-cloud-2.2.0.tar.gz
...

cat collection/requirements.yml
collections:
- name: openstack-cloud-2.2.0.tar.gz
  version: 2.2.0

ansible-galaxy collection list
...
ansible-galaxy collection list openstack.cloud
...

 

5. tar 파일로 컬렉션 설치

# tar 파일로 컬렉션 설치
ansible-galaxy collection install ./collection/openstack-cloud-2.2.0.tar.gz

# 확인
ansible-galaxy collection list
# /root/.ansible/collections/ansible_collections
Collection                    Version
----------------------------- -------
openstack.cloud               2.2.0  

# /usr/lib/python3/dist-packages/ansible_collections
Collection                    Version
----------------------------- -------
amazon.aws                    6.5.0  
ansible.netcommon             5.3.0
...

ansible-galaxy collection list openstack.cloud
...

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함