Managed Node์ ํ์ผ์ ์์ฑํ๋ playbook ์ค์ต ์งํ ๊ณผ์ ์ ๊ธฐ๋กํ๋ค.
1. Playbook ์์ฑ
---
- name: ํ์ผ ์์ฑ ํ
์คํธ
hosts: all
tasks:
- name: touch file
file:
path: /home/test/test.txt
state: touch
file ๋ชจ๋์ ์ฌ์ฉํ์ฌ Managed Node์ธ ec2 ์๋ฒ์ ํ์ผ์ ๋ง๋๋ ค๊ณ ํ๋ค.
๊ทธ๋ฌ๋ permission ์๋ฌ๊ฐ ๋ฐ์ ..
ํด๋น ๋๋ ํ ๋ฆฌ๊ฐ root๋ง ์ ๊ทผ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ ์๋ฌ์์ผ๋ฉฐ, ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
---
- name: ํ์ผ ์์ฑ ํ
์คํธ
hosts: all
become: true # root ๊ถํ์ผ๋ก ์คํ
tasks:
- name: touch file
file:
path: /home/test/test.txt
state: touch
playbook์ ์์ ํ๊ณ ๋๋ ค๋ณด๋ฉด test.txt ํ์ผ์ด ๋ง๋ค์ด์ง ๊ฒ์ ํ์ธํ ์ ์๋ค.
file ๋ชจ๋์์ ์์ฃผ ์ฌ์ฉ๋๋ ์ต์
- path: ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ์ ๊ฒฝ๋ก ์ง์ (ํ์)
- state: ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ ์ํ ์ค์
- absent: ํ์ผ/๋๋ ํ ๋ฆฌ ์ญ์
- directory: ๋๋ ํ ๋ฆฌ ์์ฑ
- file: ํ์ผ ์์ฑ
- link: ์ฌ๋ณผ๋ฆญ ๋งํฌ ์์ฑ
- touch: ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ -> ๋น ํ์ผ ์์ฑ, ํ์ผ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ -> ํ์ผ ์์ ์๊ฐ ๊ฐฑ์
- owner: ํ์ผ/๋๋ ํ ๋ฆฌ ์์ ์ ์ง์
- group: ํ์ผ/๋๋ ํ ๋ฆฌ ๊ทธ๋ฃน ์ง์
- mode: ํ์ผ/๋๋ ํ ๋ฆฌ ๊ถํ ์ค์
๋ณ์ ์ค์
๋ณ์๋ฅผ ์ค์ ํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ์ค์ตํด๋ณด๋ฉฐ ์์ฑ๋๋ ํ์ผ์ ์ด๋ฆ์ ๋์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์๋๋ก ํด๋ณด๊ฒ ๋ค.
1. playbook ๋ด์์ ๋ณ์ ์ค์
vars ๋ฅผ ์ฌ์ฉํด์ ๋ณ์๋ฅผ ์ ์ํ๊ณ ์ด๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์ด๋ฆ์ ๋์ ์ผ๋ก ์ค์ ํ ์ ์๋ค.
---
- name: ํ์ผ ์์ฑ ํ
์คํธ
hosts: all
become: true
vars:
file_name: honggildong.txt
tasks:
- name: touch file
file:
path: /home/test/{{ file_name }}
state: touch
๋ณ์๋ฅผ {{ vars }} ํ์์ผ๋ก ์์ฑํ๋ ๋ฐฉ์์ Jinja2 ํ ํ๋ฆฟ ๋ฐฉ์์ด๋ค.
2. ์ธ๋ถ ๋ณ์ ํ์ผ ์ฌ์ฉ
vars.yml ํ์ผ์ ๋ณ์๋ฅผ ์ ์ํ๊ณ , playbook์์ ์ด๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐฉ์์ด๋ค.
- ๋ณ์ ์ ์
- ๋ณ์ ๋ถ๋ฌ์ค๊ธฐ
---
- name: ํ์ผ ์์ฑ ํ
์คํธ
hosts: all
become: true
vars_files:
- /home/ansible/vars/vars.yml
tasks:
- name: touch file
file:
path: /home/test/{{ file_name }}
state: touch
3. CLI์์ ๋ณ์ ์ค์
CLI์์ ๋ณ์๋ฅผ ์ค์ ํ๋ฉด playbook์ ์คํํ ๋ ๋์ ์ผ๋ก ๋ณ์๋ฅผ ์ ๋ฌํ์ฌ ์ฌ์ฉ์๊ฐ ์ํ๋ ๊ฐ์ ์ฆ์ ์ ์ฉํ ์ ์๋ค.
- ํ๋ ์ด๋ถ ์์ฑ
---
- name: ํ์ผ ์์ฑ ํ
์คํธ
hosts: all
become: true
tasks:
- name: touch file
file:
path: /home/test/{{ file_name }}
state: touch
- CLI ์์ฑ
--extra-vars ์ต์ ์ ์ฌ์ฉํด file_name์ ์ ๋ฌํ๋ค.
cli_test.txt ํ์ผ์ด ์๊ธด ๊ฒ์ ํ์ธํ ์ ์๋ค.
'Infra & DevOps > Ansible' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Ansible] ์๋ฒ ์ ๊ฒ ์คํฌ๋ฆฝํธ ์๋ํ ๊ตฌํ ์ค์ต - CPU ์ ์ ์จ (0) | 2024.04.01 |
---|---|
[Ansible] ansible.cfg ํ์ผ ์์ฑ ๋ฐ ์ค์ (0) | 2024.03.29 |
[Ansible] Shell, Command, Raw ๋ชจ๋์ ์ฐจ์ด์ (0) | 2024.03.28 |
[Ansible] ansible_facts์ gather_facts ์ฌ์ฉํด๋ณด๊ธฐ (0) | 2024.03.26 |
[Ansible] Playbook ์์ํ๊ธฐ & ํ ํ ์คํธ (1) | 2024.03.26 |