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: 인공물
파일을 용도별로 구분을 해서 재사용하기 위함
플레이의 키워드
- hosts: x
vars_files:
- vars/a.yaml
tasks:
...
- 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 | 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
- 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:
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