포스트

[K8S Deploy Study by Gasida] - Ansible 기초 - 변수

[K8S Deploy Study by Gasida] - Ansible 기초 - 변수

변수

  • 변수를 사용하여 사용자, 설치하고자 하는 패키지, 재시작할 서비스, 생성 또는 삭제할 파일명 등 시스템 작업 시 사용되는 다양한 값을 저장할 수 있다. 앤서블에서 사용되는 변수는 그룹 변수, 호스트 변수, 플레이 변수, 추가 변수가 있으며 플레이 결과를 저장하기 위한 작업 변수도 있다.

    ansible.builtin.user

    1. 그룹 변수
    • 인벤토리에 정의된 호스트 그룹에 적용하는 변수

project/inventory

[all:vars] 섹션을 선언하고 해당 섹션 아래에 user=ansible이라는 변수와 값을 선언 후 all이라는 그룹에서 user라는 변수를 사용할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
[web]
tnode1 ansible_python_interpreter=/usr/bin/python3
tnode2 ansible_python_interpreter=/usr/bin/python3

[db]
tnode3 ansible_python_interpreter=/usr/bin/python3

[all:children]
web
db

[all:vars]
user=ansible

project/create-user.yaml

  • ``를 사용하여 변수 사용이 가능
1
2
3
4
5
6
7
8
---

- hosts: all
  tasks:
  - name: Create User 
    ansible.builtin.user:
      name: ""
      state: present

실행하기

1
ansible-playbook create-user.yml
  1. 호스트 변수
    • 호스트에서만 사용할 수 있음

project/inventory

  • db 그룹의 tnode-3 호스트 옆에 변수를 선언
1
2
3
4
5
6
7
8
9
10
11
12
13
[web]
tnode1 ansible_python_interpreter=/usr/bin/python3
tnode2 ansible_python_interpreter=/usr/bin/python3

[db]
tnode3 ansible_python_interpreter=/usr/bin/python3 user=ansible1

[all:children]
web
db

[all:vars]
user=ansible

project/create-user1.yml

  • hosts 를 all → db
1
2
3
4
5
6
7
8
---

- hosts: db
  tasks:
  - name: Create User 
    ansible.builtin.user:
      name: ""
      state: present

실행하기

1
ansible-playbook create-user1.yml
  1. 플레이 변수
    • 플레이북 내에서 선언되는 변수, 별도 파일 분리
    • hosts 아래에 vars: 를 추가하고 그 아래에 다시 user: ansible2 라는 변수와 값을 추가
1
2
3
4
5
6
7
8
9
10
11
---

- hosts: all
  vars:
    user: ansible2

  tasks:
  - name: Create User 
    ansible.builtin.user:
      name: ""
      state: present

실행하기

1
ansible-playbook create-user1.yml

플레이 변수를 별도 파일로 분리

1
mkdir vars echo "user: ansible3" > vars/users.yml

project/create-user3.yml

1
2
3
4
5
6
7
8
9
10
11
---

- hosts: all
  vars_files:
    - vars/users.yml

  tasks:
  - name: Create User 
    ansible.builtin.user:
      name: ""
      state: present

실행 하기

1
ansible-playbook create-user3.yml
  1. 추가 변수 ⭐️ 가장 우선순위 높음
    • 외부에서 ansible-playbook를 실행 할 때 함께 파라미터로 넘겨주는 변수
1
ansible-playbook -e user=ansible4 create-user3.yml

ansible.builtin.debug

  1. 작업 변수
    • 플레이북의 수행 결과를 저장. 특정 작업 수행 후 그 결과를 후속 작업에서 사용할 때 주로 사용

예상 시나리오 : 가상 자원을 조회하고 조회된 결과를 가지고 VM을 생성할 때

project/create-user4.yml

1
2
3
4
5
6
7
8
9
10
11
12
---

- hosts: db
  tasks:
  - name: Create User 
    ansible.builtin.user:
      name: ""
      state: present
    register: result
  
  - ansible.builtin.debug:
      var: result

실행하기

1
ansible-playbook -e user=ansible5 create-user4.yml

결과

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
# (터미널2) 모니터링
watch -d "ssh tnode3 tail -n 3 /etc/passwd"

ansible-playbook -e user=ansible5 create-user4.yml
PLAY [db] ***************************************************************************

TASK [Gathering Facts] **************************************************************
ok: [tnode3-ubuntu.local]

TASK [Create User ansible5] *********
changed: [tnode3-ubuntu.local]

TASK [ansible.builtin.debug] ********************************************************
ok: [tnode3-ubuntu.local] => {
    "result": {
        "changed": true,
        "comment": "",
        "create_home": true,
        "failed": false,
        "group": 1006,
        "home": "/home/ansible5",
        "name": "ansible5",
        "shell": "/bin/sh",
        "state": "present",
        "system": false,
        "uid": 1006
    }
}

PLAY RECAP **************************************************************************
tnode3-ubuntu.local        : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.