๐Ÿ‘ฉโ€๐Ÿ’ป0707[ansibl์•ค์„œ๋ธ” ํ™˜๊ฒฝ์„ค์ • ์ž๋™ํ™”,docker]

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

๐Ÿ“Œ Ansible

๐Ÿ“™ ์Šค๋ƒ…์ƒท ๋ณต์›ํ•˜๊ธฐ

๐Ÿ“™ ์•ค์„œ๋ธ” ์„ค์น˜ ๋ฐ ์ดˆ๊ธฐ์„ค์ •

# yum install epel-release -y
# yum --enablerepo=epel -y install ansible
# ansible --version

๐Ÿ“™ keyscan ์„ค์ •

์ ‘์† 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 ์•ˆ๋ฌผ์–ด๋ด„.

๐Ÿ“™ keypair ์„ค์ •

# 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

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

[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๋„ฃ๊ธฐ)

โœ๏ธ ์‹ค์Šต์„ ํ†ตํ•ด ์˜จํ”„๋ ˆ๋ฏธ์Šค ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑ๊ด€๋ฆฌ ํ•ด๋ณด์•˜๋‹ค!!

๐Ÿ“Œ Docker & Container ์ด๋ก 

Dock : ํ•ญ๊ตฌ
Docker : ํ•ญ๊ตฌ์—์„œ ์ผํ•˜๋Š” ์‚ฌ๋žŒ๋“ค
๋„์ปค๊ฐ€ ์„ค์น˜๋œ ์žฅ์น˜๋ฅผ ๋„์ปค ์—”์ง„์ด๋ผ๊ณ  ํ•จ.
๊ฒฝ๋Ÿ‰์œผ๋กœ ๋งŒ๋“  ๊ฐ€์ƒํ™” ๋„๊ตฌ

๋„์ปค์— ์‚ฌ์šฉ๋˜๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ž์ฒด๋„ ํฌ๊ธฐ๊ฐ€ ์ž‘๋‹ค.
์ผ๋ฐ˜ vm๋ณด๋‹ค ๋น ๋ฅด๋‹ค. ์ž‘๊ธฐ ๋•Œ๋ฌธ์—. ์„ฑ๋Šฅ์€ ๊ฐ™๊ฑฐ๋‚˜ ์ข‹๋‹ค.
๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๊ณ  ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žˆ๋‹ค.
์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” vm์„ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์ฒ˜๋Ÿผ ์ƒ๊ฐํ•ด๋„ ๋จ.

push; upload
pull; download
run; ๋ฐฐํฌ ์‹คํ–‰. => ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ตฌ๋™. ์‹คํ–‰์‹œํ‚ค๋ฉด ์›น์„œ๋ฒ„ ์ž‘๋™.
=> ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ณง ์„œ๋ฒ„๋‹ค.

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

๐Ÿ“Œ ๊ธฐํƒ€

โญ๏ธ '๐Ÿ“™keyscan ์„ค์ •' ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…


=> .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

์ด๋ ‡๊ฒŒ ํ•œ๋ฒˆ ํ•ด์ฃผ๋ฉด ์ƒ๊น€.

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

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