# yum install epel-release -y
# yum --enablerepo=epel -y install ansible
# ansible --version
์ ์ knownํธ์คํธ ๋ฑ๋ก ํด๊ฒฐํ๊ธฐ. (์ผ์ผ์ด yesํ์ง ์๋๋ก)
[root@ansible-server ~]# mkdir env && cd $_
# vi keyscan.yml
- name: Setup for the Ansible's Environment
hosts: localhost
gather_facts: no
tasks:
- name: keyscan
shell: "{{ item }}"
with_items:
- "ssh-keyscan 192.168.1.44 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.1.45 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.1.46 >> ~/.ssh/known_hosts"
- "ssh-keyscan 192.168.1.47 >> ~/.ssh/known_hosts"
# ansible-playbook keyscan.yml -k
[root@ansible-server env]# vi ansible_env.yml
- name: Setup for the Ansible's Environment
hosts: localhost //์๊ธฐ ์์ ์๋ฒ ์๋ํ
gather_facts: no
tasks:
- name: Add "/etc/ansible/hosts"
blockinfile:
path: /etc/ansible/hosts
block: | //๊ฐํ์ ์ฃผ๊ธฐ ์ํ ํ์ดํ
[centos]
192.168.1.44
192.168.1.45
[ubuntu]
192.168.1.46 ansible_python_interpreter=/usr/local/python3
192.168.1.47 ansible_python_interpreter=/usr/local/python3
- name: Configure Bashrc
lineinfile:
path: /root/.bashrc
line: "{{ item }}" // ๋ฐ๋ณต๋ถ ์์ item๋ฃ๊ณ
with_items: // lineinfile๊ณผ ๊ฐ์ ์ค์ withitems ๋ฃ๊ณ ์๋ ๋ช
๋ น์ด ๋ฃ์ผ๋ฉด item์์ ์๋๋ฌธ๊ตฌ๋ค์ด ๋ค์ด๊ฐ.
- "alias ans='ansible'" // ๋ณ์นญ
- "alias anp='ansible-playbook'"
โ๏ธ ์ฐ๋ถํฌ18๋ฒ์ ๋ถํฐ๋ ํ์ด์ฌ3๋ฅผ ์ฌ์ฉํด์ผํจ.
# ansible-playbook ansible_env.yml
โ๏ธ ํ์ธ
[root@ansible-server ~]# cat /etc/ansible/hosts
[root@ansible-server ~]# ans all -m ping -k
// yes ์๋ฌผ์ด๋ด.
# vi keypair_new.yml
- name: Create known_hosts between server and nodes
hosts: all
connection: local
serial: 1
gather_facts: no
tasks:
- name: ssh-keyscan for known_hosts file
command: /usr/bin/ssh-keyscan -t ecdsa {{ ansible_host }} # ๋งค์ง ๋ณ์ ansible_host ํ์ฉํ์ฌ hosts ip ํธ์ถ
register: keyscan //์ด๋ผ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์์ฑํด์ ์ ๋ด์ฉ์ ์ฌ๊ธฐ์ ์ ์ฅํ๊ฒ ๋ค.
- name: input key
lineinfile:
path: ~/.ssh/known_hosts
line: "{{ item }}"
create: yes
with_items:
- "{{ keyscan.stdout_lines }}"
- name: Create authorized_keys between server and nodes
hosts: all
connection: local
gather_facts: no
vars:
ansible_password: kosa0401
tasks:
- name: ssh-keygen for authorized_keys file
openssh_keypair:
path: ~/.ssh/id_rsa
size: 2048
type: rsa
force: False # overwriteํ์ง ์๋๋ค๋ False๋ผ๊ณ ๊ฐ์ ๋ฃ๊ฑฐ๋ ์๋๋ฉด ์ญ์ ํ๊ฑฐ๋ ํ๋ฉด ๋๊ฒ ์ต๋๋ค.
- name: input key for each node
connection: ssh
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
# anp keypair.yml
# vi nginx_install.yml
- name: Install nginx on centos
hosts: centos
gather_facts: no
tasks:
- name: install epel-release
yum:
name: epel-release
state: latest
- name: install nginx web server
yum: name=nginx state=present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644
- name: start nginx web server
service: name=nginx state=started
- name: Install nginx on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: install nginx web server
apt: pkg=nginx state=present update_cache=yes
- name: Upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/
mode=0644 validate_certs=no
# ansible-playbook nginx_install.yml
# vi nginx_remove.yml
- name: Remove nginx on centos
hosts: centos
gather_facts: no
tasks:
- name: remove nginx web server
yum: name=nginx state=absent
- name: Remove nginx on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: remove nginx web server
apt: pkg=nginx* state=absent
# ansible-playbook nginx_remove.yml
[root@ansible-server ~]# mkdir nfs && cd $_
# vi nfs.yml
- name: Setup for nfs server
hosts: localhost
gather_facts: no
tasks:
- name: make nfs_shared directory
file:
path: /root/nfs_shared
state: directory
mode: 0777
- name: configure /etc/exports
lineinfile:
path: /etc/exports
line: /root/nfs_shared 192.168.0.0/20(rw,sync)
- name: Install NFS
yum:
name: nfs-utils
state: present
- name: nfs service start
service:
name: nfs-server
state: restarted
enabled: yes
- name: Setup for nfs clients
hosts: centos
gather_facts: no
tasks:
- name: make nfs_client directory
file:
path: /root/nfs
state: directory
- name: Install NFS
yum:
name: nfs-utils
state: present
- name: mount point directory as client
mount:
path: /root/nfs
src: 192.168.0.196:/root/nfs_shared // ansible server IP
fstype: nfs
state: mounted
- name: Setup for nfs clients U
hosts: ubuntu
gather_facts: no
tasks:
- name: make nfs_client directory
file:
path: /root/nfs
state: directory
- name: Install NFS-U
apt:
pkg: nfs-common
state: present
update_cache: yes
- name: mount point directory as client
mount:
path: /root/nfs
src: 192.168.0.196:/root/nfs_shared // ansible server IP
fstype: nfs
opts: nfsvers=3
state: mounted
# ansible-playbook nfs.yml -k
โ๏ธ~/env/ansible_env.yml์์
# vi wordpress.yml
- name: Setup for webserver
hosts: 192.168.1.44
gather_facts: no
tasks:
- name: Install http
yum:
name: "{{ item }}"
state: present
with_items:
- httpd
- php
- php-mysql
- php-gd
- php-mbstring
- wget
- unzip
- name: Unarchive a file that needs to be downloaded (added in 2.0)
ansible.builtin.unarchive:
src: https://ko.wordpress.org/wordpress-4.8.2-ko_KR.zip
dest: /var/www/html
remote_src: yes
- name: chown
file:
path: /var/www/html/wordpress
owner: "apache"
group: "apache"
recurse: "yes"
- name: web service restart
service:
name: httpd
state: restarted
- name: Setup for dbserver
hosts: dbserver
gather_facts: no
tasks:
- name: Install mariadb
apt:
pkg: mariadb-server
state: present
update_cache: yes
- name: Install pymysql
apt:
pkg: python-pymysql
state: present
- name: Install pymysql
apt:
pkg: python3-pymysql
state: present
- name: set root password
mysql_user:
name: 'root'
password: '{{ mysql_root_password }}'
login_unix_socket: /var/run/mysqld/mysqld.sock
state: present
- name: edit file
replace:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: "bind-address"
replace: "#bind-address"
- name: db service restart
service:
name: mysql
state: restarted
- name: Create database
mysql_db:
db: wordpress
login_unix_socket: /var/run/mysqld/mysqld.sock
state: present
- name: Create database user
mysql_user:
user: wpuser
password: wppass
priv: "wordpress.*:ALL,GRANT"
host: '%'
login_unix_socket: /var/run/mysqld/mysqld.sock
state: present
# anp wordpress.yml --extra-vars "mysql_root_password=kosa0401"
๋ธ๋ผ์ฐ์ ์์ 192.168.1.45/wordpress ์ง์
,
๋ฐ์ดํฐ๋ฒ ์ด์ค ํธ์คํธ 192.168.1.47(db์๋ฒ ip๋ฃ๊ธฐ)
โ๏ธ ์ค์ต์ ํตํด ์จํ๋ ๋ฏธ์ค ์๋ฒ๋ฅผ ๊ตฌ์ฑ๊ด๋ฆฌ ํด๋ณด์๋ค!!
Dock : ํญ๊ตฌ
Docker : ํญ๊ตฌ์์ ์ผํ๋ ์ฌ๋๋ค
๋์ปค๊ฐ ์ค์น๋ ์ฅ์น๋ฅผ ๋์ปค ์์ง์ด๋ผ๊ณ ํจ.
๊ฒฝ๋์ผ๋ก ๋ง๋ ๊ฐ์ํ ๋๊ตฌ
๋์ปค์ ์ฌ์ฉ๋๋ ์ด๋ฏธ์ง ํ์ผ ์์ฒด๋ ํฌ๊ธฐ๊ฐ ์๋ค.
์ผ๋ฐ vm๋ณด๋ค ๋น ๋ฅด๋ค. ์๊ธฐ ๋๋ฌธ์. ์ฑ๋ฅ์ ๊ฐ๊ฑฐ๋ ์ข๋ค.
๋์ปค ์ด๋ฏธ์ง๊ฐ ์๊ณ ๋์ปค ์ปจํ
์ด๋๊ฐ ์๋ค.
์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ vm์ ๋์ปค ์ปจํ
์ด๋์ฒ๋ผ ์๊ฐํด๋ ๋จ.
push; upload
pull; download
run; ๋ฐฐํฌ ์คํ. => ์ปจํ
์ด๋๋ก ๊ตฌ๋. ์คํ์ํค๋ฉด ์น์๋ฒ ์๋.
=> ์ปจํ
์ด๋๋ ๊ณง ์๋ฒ๋ค.
๐โ๏ธโ๏ธ๐ขโญ๏ธ๐
=> .ssh/known_hostsํด๋๊ฐ ์์ด์ ๋ถ๊ฐ.
[root@ansible-server ~]# ssh root@192.168.1.44
The authenticity of host '192.168.1.44 (192.168.1.44)' can't be established.
ECDSA key fingerprint is SHA256:UyPJ/WrMoKKgfyreluHWprW0lWE9hRkn+sgKCIMql+o.
ECDSA key fingerprint is MD5:42:9a:ee:79:8e:ed:99:fc:76:56:88:89:a4:83:ce:df.
Are you sure you want to continue connecting (yes/no)? ^C
์ด๋ ๊ฒ ํ๋ฒ ํด์ฃผ๋ฉด ์๊น.