์์๋ธ ์๋ฒ ; 192.168.0.196
[centos]
192.168.1.44
192.168.1.45
[ubuntu]
192.168.1.46
192.168.1.47
โ๏ธ hostname ๊ฐ ์๋ฒ๋ง๋ค ๋ฐ๊ฟ์ฃผ๊ธฐ
hostnamectl set-hostname ํธ์คํธ์ด๋ฆ
โ๏ธ centos selinuxํ์ธ
# sestatus
# vi /etc/selinux/config
=> ๊ฐ ์๋ฒ ์ค๋ ์ท ์ฐ์ด๋๊ธฐ
โ๏ธ ์ธํฐ๋ท์์ vagrant๊ฒ์ ํ ์ค์น - ์ปดํจํฐ ์ฌ์์
โ๏ธ cmd ์ฐฝ์์ ์๋์ ๊ฐ์ด ์งํ
C:\Users\johnlee>cd c:\HashiCorp
c:\HashiCorp>vagrant init
https://app.vagrantup.com/boxes/search?provider=virtualbox
c:\HashiCorp>notepad Vagrantfile
config.vm.box = "centos/7"
config.vm.network "public_network"
c:\HashiCorp>vagrant up
c:\HashiCorp>vagrant ssh
!-- ํ์ธ์์
--!
[vagrant@localhost ~]$ sestatus
[vagrant@localhost ~]$ sudo systemctl status firewalld
[vagrant@localhost ~]$ sudo yum install httpd -y
[vagrant@localhost ~]$ sudo systemctl enable --now httpd
[vagrant@localhost ~]$ ip a
[vagrant@localhost ~]$ curl 192.168.1.66
!------------!
c:\HashiCorp>vagrant destroy
๐ข c:\HashiCorp ์ง์
ํ dir ํ์ ๋ vagrantfile ์๋ค๋ฉด
ํ๊ฒฝ๋ณ์ ์ค์ ๋ค์ด๊ฐ์ ์ฌ์ฉ์ ๋ณ์ path์
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
์ด ๊ฒฝ๋ก ์ถ๊ฐ.
โ๏ธ ํ์ฌ๋์ด ์ฌ๋ฌ๋์ ์๋ฒ ๊ด๋ฆฌ ๊ฐ๋ฅ.
โ๏ธ IaC.
โ๏ธ ์์ด์ ํธ ์ค์น๊ฐ ํ์ ์์.
โ๏ธ ์์๋ธ์ 3๊ฐ์ง ์์
1. ์ธ๋ฒคํ ๋ฆฌ ( ์ด๋์;์๋ฒ )
2. playbook ( ๋ฌด์์;httpd?mariadb?adduser? )
3. ๋ชจ๋ ( ์ด๋ป๊ฒ ) ex)yum
โ๏ธ ansible-server ๋ชจ๋ฐ์์คํ ์ผ๋ก ์ง์
# yum install epel-release -y
# yum --enablerepo=epel -y install ansible
# ansible --version
์ค์๋ธ ๋ฌธ์ ์ฌ์ดํธ https://docs.ansible.com/ansible/latest/index.html
์ค์๋ธ ๋ชจ๋ ์ฌ์ดํธ https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
โ๏ธ shell ๋ชจ๋ ; ๋ฉฑ๋ฑ์ฑ์ด ์์. ์์ ์ํํ ๋ช
๋ น์ด๋ฅผ ๋ ์ํํจ.
-> ํธ๋ฆฌํ์ง๋ง ๋ถํ๋ฅผ ์ค ๊ฐ๋ฅ์ฑ์ด ์ปค์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค.
โ๏ธ service ๋ชจ๋ ; ๋ฉฑ๋ฑ์ฑ์ด ์์.
!--๋งจ ์๋์ ์ธ๋ฒคํ ๋ฆฌ ์ง์ด๋ฃ๊ธฐ --!
# vi /etc/ansible/hosts
[centos]
192.168.1.44
192.168.1.45
[ubuntu]
192.168.1.46
192.168.1.47
!-------------------------!
# ansible all -m ping //์ฐ๋ฆฌ๊ฐ ์๋ ping ์๋. ๋ชจ๋๋ก์์ ping
# ansible all -m ping -k //ask password
# ansible centos -m ping -k
# ansible ubuntu -m ping -k
# echo "192.168.1.44" >> inventory.list
# echo "192.168.1.46" >> inventory.list
# ansible all -i inventory.list -m ping -k
# ansible 192.168.1.46 -i inventory.list -m ping -k
# ansible all --list-hosts
# ansible all -i inventory.list --list
# ansible all -m shell -a "uptime" -k
# ansible all -m shell -a "df -h" -k
# ansible all -m shell -a "free -h" -k
# ansible all -m user -a "name=kosa" -k
# ansible all -m shell -a "tail -n 1 /etc/passwd" -k
# ansible all -m user -a "name=kosa state=absent" -k
# ansible all -m shell -a "tail -n 1 /etc/passwd" -k
--- ์ผํ ์ค ์ํ์น ์ ๋ํน ๊ด๋ฆฌ
# ansible centos -m yum -a "name=httpd state=present" -k //centos๋ถ๋ถ์ ํน์ IP ๋ฃ์ด์ ์ ์ฉ ๊ฐ๋ฅ.
# curl https://www.nginx.com/ -o index.html
# ansible centos -m copy -a "src=index.html dest=/var/www/html/index.html" -k
# ansible centos -m service -a "name=httpd state=started" -k
# ansible centos -m shell -a "systemctl status firewalld" -k
# ansible centos -m shell -a "systemctl start firewalld" -k
# ansible centos -m shell -a "firewall-cmd --permanent --zone=public --add-service=http" -k
# ansible centos -m shell -a "firewall-cmd --reload" -k
# ansible centos -m service -a "name=httpd state=stopped" -k
# ansible centos -m shell -a "systemctl stop firewalld" -k
# ansible centos -m shell -a "systemctl disable firewalld" -k
# ansible centos -m yum -a "name=httpd state=absent" -k
--- ์ฐ๋ถํฌ ์ํ์น ์ ๋ํน ๊ด๋ฆฌ
# ansible 192.168.0.143 -m apt -a "name=apache2 state=present" -k
# curl https://www.nginx.com/ -o index.html
# ansible ubuntu -m copy -a "src=index.html dest=/var/www/html/index.html" -k
# ansible ubuntu -m service -a "name=apache2 state=stopped" -k
# ansible ubuntu -m service -a "name=apache2 state=started" -k
# ansible ubuntu -m apt -a "name=apache2 state=absent" -k
์ค์๋ธ์ ๋ฉฑ๋ฑ์ฑ(Idempotency)์ด๋ ํน์ง์ ๊ฐ์ง๋๋ค. ์ด๋ ์ฌ๋ฌ ๋ฒ ์ ์ฉํด๋ ๊ฒฐ๊ณผ๊ฐ ๋ฐ๋์ง ์์ผ๋ฉฐ, ์์ ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด ๊ทธ ๋ถ๋ถ๋ง ์๋กญ๊ฒ ๋ฐ์๋๋ ํน์ง์ด ์์ต๋๋ค.
โ๏ธ๋ฉฑ๋ฑ์ฑ ์๋ ๊ฒฝ์ฐ
# echo "172.16.0.100" >> inventory.list
# cat inventory.list
# echo "172.16.0.100" >> inventory.list
# cat inventory.list
โ๏ธ๋ฉฑ๋ฑ์ฑ ์๋ ๊ฒฝ์ฐ
# ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200"
# cat inventory.list
# ansible localhost -c local -m lineinfile -a "path=inventory.list line=172.16.0.200"
# cat inventory.list
-c ; connection
-c local ; remote ์๋ local ์ฐ๊ฒฐ ssh ์ฌ์ฉ ์ํ๊ฒ ๋ค.
# ssh-keygen -t rsa
# ansible all --list
# ssh-copy-id root@192.168.1.44
# ssh-copy-id root@192.168.1.45
# ssh-copy-id root@192.168.1.46
# ssh-copy-id root@192.168.1.47
# ansible all -m ping
YAML ํ์์ผ๋ก ์์ฑ๋ ๊ฐ๊ฐ์ Playbook๋ค์ ํ๋ ์ด์์ Play๋ฅผ ๊ฐ์ง๋ฉฐ, ๊ฐ๊ฐ์ Play๋ ํ๋ ์ด์์ task(์ค์๋ธ ๋ชจ๋)์ ์คํํ๋ค
# vi apache_install.yml
- name: Install apache on centos
hosts: centos
gather_facts: no
tasks:
- name: install apache web server
yum: name=httpd state=present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
- name: start apache web server
service: name=httpd state=started enabled=yes
- name: Install apache on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: install apache web server
apt: name=apache2 state=present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
- name: start apache web server
service: name=apache2 state=started
# ansible-playbook apache_install.yml -k
# vi apache_remove.yml
- name: Remove apache on centos
hosts: centos
gather_facts: no
tasks:
- name: remove apache web server
yum: name=httpd state=absent
- name: Remove apache on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: remove apache web server
apt: name=apache2 state=absent
# ansible-playbook apache_remove.yml -k
๐โ๏ธโ๏ธ๐ขโญ๏ธ๐
๊ทผ๋ฐ ์ ์ด ์น๊ตฌ๋ง kosa ํ๊ทธ๊ฐ ๋ฌ๋ ค์์ฃ ?-?ใ ใ