๐Ÿ‘ฉโ€๐Ÿ’ป0706[ansible,vagrant]

๋ง์ง€ยท2022๋…„ 7์›” 6์ผ
1
post-custom-banner

๐Ÿ“Œ ansible- vbox ์„œ๋ฒ„ ์ดˆ๊ธฐ ์„ค์ •

  1. ์–ด์ œ ๋งŒ๋“  ubuntu-vbox ova๋กœ ๋งŒ๋“ค์–ด์„œ ๋‚ด๋ณด๋‚ด๊ธฐ
  2. centos ova ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  3. cpu1๊ฐœ ๋ฉ”๋ชจ๋ฆฌ 1024
  4. dvd์ฒดํฌํ•ด์ œ, usb์ฒดํฌํ•ด์ œ
  5. ์ด vm ์ด๋ฆ„์„ ansible-server๋กœ ํ•ด์ฃผ๊ธฐ.
  6. ansible-server๋ฅผ ๋ณต์ œํ•ด์„œ ์ด๋ฆ„ centos-node01๋กœ ํ•ด์ฃผ๊ธฐ.
  7. ์—ฐ๊ฒฐ๋œ ๋ณต์ œ.
  8. ansible-server ๋˜ ๋ณต์ œ. ์ด๋ฆ„:centos-node02
  9. ์—ฐ๊ฒฐ๋œ ๋ณต์ œ.
  10. 1.์—์„œ ๋งŒ๋“  ์šฐ๋ถ„ํˆฌ ๊ฐ€์ ธ์˜ค๊ธฐ.
  11. dvd,usbํ•ด์ œ
  12. ์„ค์ •์—์„œ ์ด๋ฆ„ ubuntu-node01๋กœ ํ•ด์ฃผ๊ธฐ.
  13. CPU1๊ฐœ, ram1024 ํ™•์ธ
  14. ๋ฐฉ๊ธˆ ๊ฐ€์ ธ์˜จ ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„ ๋ณต์ œ
  15. ์ด๋ฆ„ : ubuntu node 02
  16. ์—ฐ๊ฒฐ๋œ ๋ณต์ œ
  17. ๊ทธ๋ฃน๋งŒ๋“ค๊ธฐ - ansible-server์—๋‹ค๊ฐ€ ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ
  18. ๊ทธ๋ฃน์ด๋ฆ„ : ansible
  19. ๋‹ค๋ฅธ ์„œ๋ฒ„๋“ค ์•ˆ์— ๋„ฃ์–ด์ฃผ๊ธฐ
  20. ๊ทธ๋ฃน์œผ๋กœ ์—ฐ๊ฒฐ์‹œ์ž‘.

์—”์„œ๋ธ” ์„œ๋ฒ„ ; 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

โœ”๏ธ ์ธํ„ฐ๋„ท์—์„œ 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\

์ด ๊ฒฝ๋กœ ์ถ”๊ฐ€.

๐Ÿ“Œ ansible

๐Ÿ“™ ์ด๋ก 

โœ”๏ธ ํ•œ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฌ๋Œ€์˜ ์„œ๋ฒ„ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ.
โœ”๏ธ IaC.
โœ”๏ธ ์—์ด์ „ํŠธ ์„ค์น˜๊ฐ€ ํ•„์š” ์—†์Œ.

โœ”๏ธ ์—”์„œ๋ธ”์˜ 3๊ฐ€์ง€ ์š”์†Œ
1. ์ธ๋ฒคํ† ๋ฆฌ ( ์–ด๋””์„œ;์„œ๋ฒ„ )
2. playbook ( ๋ฌด์—‡์„;httpd?mariadb?adduser? )
3. ๋ชจ๋“ˆ ( ์–ด๋–ป๊ฒŒ ) ex)yum

๐Ÿ“™ ansible ์„ค์น˜

โœ”๏ธ 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

๐Ÿ“™ AD-HOC

โœ”๏ธ 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 key ํ™œ์šฉ

# 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(์•ค์„œ๋ธ” ๋ชจ๋“ˆ)์„ ์‹คํ–‰ํ•œ๋‹ค

  • name: Playbook Tutorial # - ๋กœ ์‹œ์ž‘ํ•˜๋Š” 3์ค„์ด ํ•œ ๋ธ”๋ก์ด๋‹ค.
    hosts: all # ":" ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ํ•ญ๋ชฉ๋ช…๊ณผ ๊ฐ’์„ ์„ค์ •ํ•œ ํ‚ค-๊ฐ’ ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค.
    tasks: # ํ•ญ๋ชฉ๋ช…์˜ ์œ„์น˜๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค. yaml์€ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊นŠ์ด๊ฐ€ ๋œ๋‹ค.

๐Ÿ“™ ์„ผํ† ์Šค, ์šฐ๋ถ„ํˆฌ ์•„ํŒŒ์น˜ ์„ค์น˜ ํ”Œ๋ ˆ์ด๋ถ

# 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

๐Ÿ“™โœ”๏ธโœ๏ธ๐Ÿ“ขโญ๏ธ๐Ÿ“Œ

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 8์›” 18์ผ

๊ทผ๋ฐ ์™œ ์ด ์นœ๊ตฌ๋งŒ kosa ํƒœ๊ทธ๊ฐ€ ๋‹ฌ๋ ค์žˆ์ฃ ?-?ใ…‹ใ…‹

1๊ฐœ์˜ ๋‹ต๊ธ€