Ansible을 활용한 자동화 구성 관리(22.04.19)

박민선·2022년 4월 21일
0

템플릿 주석

ansible.cfg

[defaults]
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}

templates/port.cnf.j2

{{ ansible_managed | comment }}
[mysqld]
port={{ database["svc_port"] }}

경고의 목적


Artefact, Artifact: 인공물

  • 애플리케이션이 작동해서 생성한 데이터
  • 사람이 직접 작성한 코드

파일을 용도별로 구분을 해서 재사용하기 위함

  • 변수 파일
  • 작업 파일
  • 플레이/플레이북 파일
  • 역할(Role)

변수 파일

vars_files

플레이의 키워드

- hosts: x
  vars_files:
    - vars/a.yaml

  tasks:
  ...

include_vars 모듈

- hosts: x

  tasks:
    - include_vars:
        dir: vars/

인벤토리 변수

인벤토리 내부 파일에 변수 설정

호스트 변수

node1 message="hello world"

그룹 변수

[wordpress]
node1
node2

[wordpress:vars]
message="hello world"

인벤토리 외부 파일에 변수 설정

인벤토리 파일 또는 플레이북 파일이 있는 디렉토리에...

  • group_vars/<GROUP NAME>
  • host_vars/<HOST NAME>

<GROUP NAME>, <HOST NAME> 디렉토리 또는 파일을 생성

.
├── ansible.cfg
├── group_vars
│   └── nodes
├── host_vars
│   ├── 192.168.100.11
│   └── 192.168.100.12
│       └── var.yaml
└── inven.ini

ansible.cfg

[defaults]
inventory = inven.ini

inven.ini

[nodes]
192.168.100.11
192.168.100.12

host_vars\192.168.100.11

---
message: hello node1

host_vars\192.168.100.12\var.yaml

---
message: hello node2

group_vars\nodes

service_port: 8080
message: hello world
ansible-inventory --list
ansible-inventory --host <HOST>

test.yaml

---
- hosts: nodes
  tasks:
    - debug:
        msg: "{{ message }} - {{ service_port }}"

작업 재사용

include_vars: 변수 가져오기
include_role: 역할 가져오기
include_tasks: 작업 가져오기

import_playbook: 플레이북 가져오기
import_role: 역할 가져오기
import_tasks: 작업 가져오기

include vs. import

includeimport
적용 시점동적정적
루프 사용 가능가능불가능
핸들러 호출 가능불가능가능

import에서 루프 불가능

- hosts: 192.168.100.11
  tasks:
    - debug:
        msg: in play
    - include_tasks:
        file: task.yaml
      with_sequence: start=1 end=3 # 안됨
    - debug:
        msg: in play

해결방법

- hosts: 192.168.100.11
  tasks:
    - debug:
        msg: in play
    - include_tasks:
        file: task.yaml
    - debug:
        msg: in play

task.yaml

- debug:
  with_sequence: start=1 end=3

include에서 핸들러 호출 불가능

- hosts: 192.168.100.11
  tasks:
    - command: hostname
      notify:
        - hello notify
  handlers:
    - include_tasks: task.yaml

task.yaml

- name: hello notify
  debug:
    msg: hello notify

해결방법

- hosts: 192.168.100.11
  tasks:
    - command: hostname
      notify:
        - hello notify
  handlers:
    - name: hello notify
	  include_tasks: task.yaml

역할

https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#playbooks-reuse-roles

역할 생성 --> 통합 --> 플레이북
통일화된 구조

mkdir roles
ansible-galaxy init common --init-path roles
.
└── roles
    └── common
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

roles/common: 역할의 이름

tasks/main.yml: 작업이 위치
handlers/main.yml: 핸들러 작업이 위치

tests/inventory: 역할을 테스트 하기 위한 인벤토리
tests/test.yml: 역할을 테스트 하기 위한 플레이북

defaults/main.yml: 기본 역할 변수(우선 순위가 매우 낮음)
vars/main.yml: 역할 변수(우선 순위가 매우 높음)

files: 파일 관련 모듈의 src: 파라미터에서 참조하는 파일의 위치
files/a.txt: 경로 지정할 필요 없음

- copy:
	src: a.txt

templates: 템플릿 모듈의 src: 파라미터에서 참조하는 파일의 위치
templates/a.j2

- templates:
    src: a.j2

meta/main.yml: 역할을 설명하고 있는 파일

  • 역할 버전
  • 역할 이름
  • 역할 만든 사람
  • 역할 적용되는 플렛폼(리눅스 배포판)
  • 역할의 의존성

플레이에서 작업 실행 순서

# Play
- hosts:
  
  pre_tasks:
  
  roles:
  
  tasks:
  
  post_tasks:

  handlers:
  
  1. pre_tasks
  2. pre_tasks의 handlers
  3. roles
  4. roles의 handlers
  5. tasks
  6. tasks의 handlers
  7. post_tasks
  8. post_tasks의 handlers

ansible-galaxy

https://galaxy.ansible.com/

  • 역할(Role)
  • 컬랙션(Collection): 역할 + 3rd Party 모듈
  • 번들(Bundle): RedHat OpenShift <-- 역할

역할 목록 확인

ansible-galaxy list

(0. 현재 디렉토리의 roles)
1. /home/vagrant/.ansible/roles
2. /usr/share/ansible/roles
3. /etc/ansible/roles

ansible-galaxy list --roles-path roles
ansible-galaxy search elasticsearch
ansible-galaxy info geerlingguy.elasticsearch
ansible-galaxy install geerlingguy.elasticsearch
ansible-galaxy remove geerlingguy.elasticsearch
profile
클라우드신생아

0개의 댓글