Ansible 활용 NFS 및 Wordpress설치하기

2022. 7. 7. 15:41Cloud/Ansible

아래의 yml파일을 설치하도록  하자.

# 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)
==> ip 대역대 할당. 연결되어있는 서버들의 ip를 할당하도록 하자.

    - name: Install NFS
      yum:
        name: nfs-utils
        state: present
==> nfs를 설치한다

    - name: nfs service start
      service:
        name: nfs-server
        state: restarted
        enabled: yes
==> restarted를 실행해 정지되 있다면 실행을, 실행중이라면 다시 실행을 시킨다. 이 후 enabled를 반드시 해야한다.

- name: Setup for nfs clients
  hosts: centos
  gather_facts: no
==> centos에 대해 마운트를 할 것이다. 라는 기록

  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.192:/root/nfs_shared
        fstype: nfs
        state: mounted
==> 접속되어 있는 ansible 서버의 192.168.1.23를 입력.

- name: Setup for nfs clients U
  hosts: ubuntu
  gather_facts: no
==> 우분투에 NFS를 설치할 예쩡이다.


  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.1.23:/root/nfs_shared
        fstype: nfs
        opts: nfsvers=3
        state: mounted
==> 설치할 우분투 서버의 ip를 입력한 후 nfs 버전을 3으로, 마운트를 한다는 의미이다.


# ansible-playbook nfs.yml -k

 

워드프레스를 설치하는 yml파일은 아래와 같다.

# vi wordpress.yml
- name: Setup for webserver
  hosts: webserver
  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
==> wordpress를 다운받고 설치할 것인데, var/www/html의 위치에 파일이 있을 것 이다.


    - name: chown
      file:
        path: /var/www/html/wordpress
        owner: "apache"
        group: "apache"
        recurse: "yes"

==> 그룹과 권한을 아파치에게 전권부여한다.

    - name: web service restart
      service:
        name: httpd
        state: restarted
==> httpd서비스를 restart한다.


- name: Setup for dbserver
  hosts: dbserver
  gather_facts: no
==> DB서버에 대한 설명이다.


  tasks:
    - name: Install mariadb
      apt:
        pkg: mariadb-server
        state: present
        update_cache: yes

==> mariadb를 설치한다.

    - name: Install pymysql
      apt:
        pkg: python-pymysql
        state: present


    - name: Install pymysql
      apt:
        pkg: python3-pymysql
        state: present
==>python을 설치한다.


    - name: set root password
      mysql_user:
        name: 'root'
        password: '{{ mysql_root_password }}'
        login_unix_socket: /var/run/mysqld/mysqld.sock
        state: present
==> db사용자를 생성한다. name : root, password는 변수로 설정해 둔 후 차후에 선언한다. login_unix_socket명령어는 로컬 연결을위한 Unix 도메인 소켓의 경로이다.


    - name: edit file
      replace:
        path: /etc/mysql/mariadb.conf.d/50-server.cnf
        regexp: "bind-address"
        replace: "#bind-address"

==>  /etc/mysql/mariadb.conf.d/50-server.cnf 위치에 있는 bind-address를 찾은 후 # 을 붙여 주석처리 하겠다.

    - name: db service restart
      service:
        name: mysql
        state: restarted
==> mysql을 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
==>wordpress에 유저명과 password를 생성하며  상단에서 만든 모든 테이블에 권한을 주도록한다. 접근은 host(%)로 접근한다.
# anp wordpress.yml --extra-vars "mysql_root_password=kosa0401"

 

'Cloud > Ansible' 카테고리의 다른 글

Ansible 환경 설정 자동화  (0) 2022.07.07
Ansible 개괄 및 기초 환경, 명령어  (0) 2022.07.06