Data Bag은 cookbook 단위가 아닌 리포지터리 전체를 커버하는 데이터이다. Data bag의 데이터는 모든 Cookbook에서 전역변수처럼 사용할 수 있다. 인프라스트럭쳐 전체에서 사용되는 데이터를 저장하기 위해서 사용하며, json 형태로 구성된다.
Data Bag을 사용하여 노드에 두 명의 사용자를 정의하는 테스트를 진행한다.
- Data Bag 생성
- hadong 사용자 데이터 객체 생성
- hdhwang 사용자 데이터 객체 생성
- hadong 사용자 데이터 객체 생성
[root@ChefWorkstation] $ knife data bag create users
Created data_bag[users]
[root@ChefWorkstation] $ knife data bag list
users
[root@ChefWorkstation] $ export EDITOR=vi
[root@ChefWorkstation] $ knife data bag create users hadong.json
{
"id" : "hadong",
"username" : "hadong",
"home” : "/home/hadong",
"shell" : "/bin/bash"
}
: wq
Created data_bag_item[hadong.json]
[root@ChefWorkstation] $ knife data bag create users hdhwang.json
{
"id" : "hdhwang",
"username" : "hdhwang",
"home" : "/home/hdhwang",
"shell" : "/bin/bash"
}
:wq
Created data_bag_item[hdhwang.json]
- login_users cookbook 생성
[root@ChefWorkstation] $ knife cookbook create login_users
** Creating cookbook login_users in /root/chef-repo/cookbooks
** Creating README for cookbook: login_users
** Creating CHANGELOG for cookbook: login_users
** Creating metadata for cookbook: login_users
- recipe 작성
[root@ChefWorkstation] $ vi ~/chef-repo/cookbooks/login_users/recipes/default.rb
data_ids = data_bag('users')
data_ids.each do |id|
u = data_bag_item('users', id)
user u['username'] do
home u['home']
shell u['shell']
end
end
:wq
- Cookbook 업로드
[root@ChefWorkstation] $ knife cookbook upload login_users
Uploading login_users [0.1.0]
Uploaded 1 cookbook.
- Node Object의 run_list에 recipe 등록
[root@ChefWorkstation] $ knife node run_list set 172.16.1.148 recipe[login_users]
172.16.1.148 :
run_list : recipe[login_users]
- Chef Client에 적용
[root@ChefWorkstation] $ knife ssh ‘name:172.16.1.148’ ‘sudo chef-client’
172.16.1.148 Starting Chef Client, version 12.4.1
172.16.1.148 resolving cookbooks for run list: ["login_users"]
172.16.1.148 Synchronizing Cookbooks:
172.16.1.148 - login_users
172.16.1.148 Compiling Cookbooks...
172.16.1.148 Converging 2 resources
172.16.1.148 Recipe: login_users::default
172.16.1.148 * user[hadong] action create
172.16.1.148 - create user hadong
172.16.1.148 * user[hdhwang] action create
172.16.1.148 - create user hdhwang
172.16.1.148 Running handlers:
172.16.1.148 Running handlers complete
172.16.1.148 Chef Client finished, 2/2 resources updated in 1.93856 seconds
- user 생성 확인
[root@ChefWorkstation] $ knife ssh 'name:172.16.1.148' 'cat /etc/passwd | grep hadong&& cat /etc/passwd | grep hdhwang'
172.16.1.148 hadong:x:500:500::/home/hadong:/bin/bash
172.16.1.148 hdhwang:x:501:501::/home/hdhwang:/bin/bash
참 고 문 헌
[1] 이토 나오야, 박상욱, “인프라스트럭처 자동화 프레임워크 Chef Solo 입문”, 제이펍, 2014.
'Information Technology > Automation' 카테고리의 다른 글
[Chef] Opscode Community Cookbook (0) | 2023.03.20 |
---|---|
[Chef] Role (0) | 2023.03.20 |
[Chef] Attribute (0) | 2023.03.20 |
[Chef] Definition (0) | 2023.03.20 |
[Chef] Chef Server Web page (0) | 2023.03.20 |