Ansible

Ansible 사용하여 nginx 설치 및 index 페이지 변경

황동리 2024. 2. 14. 21:26
반응형

오늘은 nginx 설치와 index페이지 경로와 파일을 변경하는 것 부터 시작해보겠습니다.


nginx의 패키지 명은 nginx 입니다.
nginx의 설정파일의 위치는 /etc/nginx/nginx.conf 입니다.

nginx 설치 및 설정 명령어

---
- name: 4 server nginx html file create
  hosts: db
  tasks:
        - name: yum install
          yum:
                name: "{{ item }}"
                state: latest
          loop:
                - epel-release
                - nginx
        - name: change root directory & index file-1
          replace:
                path: /etc/nginx/nginx.conf
                regexp: 'root         /usr/share/nginx/html;'
                replace: 'root         /html;'
        - name: change root directory & index file-2
          blockinfile:
                path: /etc/nginx/nginx.conf
                insertafter: 'server_name  _;' => 이 문장 밑에 index hoho.html을 넣어주기 위해 # babo 주석처리를 이용했다.
                block: | # babo => 사용을 할 때 꼭 파이프 기호를 넣어줘야한다.
                        index   hoho.html;
        - name: create /html directory
          file:
                path: /html
                state: directory
                owner: root
                group: root
                mode: '0755'
        - name: create hoho.html
          file:
                path: /html/hoho.html
                state: touch
                owner: root
                group: root
        - name: insert content
          blockinfile: => 이것을 사용하면 파일 안의 내용을 적을 수 있다.
                path: /html/hoho.html
                block: |"html""body""h1"ksh-nginx-webserver"/h1""/body""/html"
        - name: firewall open
          firewalld:
                port: 80/tcp
                state: enabled
        - name: nginx restart
          service:
                name: nginx
                state: started
        - name: nginx restart
          service:
                name: nginx
                state: restarted

nginx 설정 변경


vi /etc/nginx/nginx.conf 에서 수정을 해줍니다.
42: root /html => 최상위 디렉터리부터 시작하는 시작파일의 디렉터리 지정
43: index hoho.html => index.html 대신 첫 페이지로 사용할 파일의 이름


이번엔 mysql을 ansible을 사용하여 설치하고 설정 해보도록 하겠습니다.

mysql 설치 및 설정 명령어

---
- name: install mysql5.7
  hosts: db
  tasks:
        - name: mysql repo install
          shell: 'yum install -y http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
          ignore_errors: yes
#          yum_repository:
#                baseurl: http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
#                ignore_error: yes
        - name: mysql-server, client install & repo gpg_check disable
          yum:
                name: mysql-community-server
                disable_gpg_check: yes
                state: present
        - name: mysql-Client install & repo gpg_check disable
          yum:
                name: "{{ item }}"
                disable_gpg_check: yes
                state: present
          loop:
                - mysql-community-server
                - mysql-community-client
        - name: mysql-5.7 server service start
          systemd:
                name: mysqld
                state: started
        - name: pip install & mysql-python
          yum:
                name:
                        - python3-pip
                        - MySQL-python
        - name: change mysql root password
          shell: |
                password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'`
                echo $password_match
                mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'It12345!'; flush privileges; "
                password=It12345! > /root/.my.cnf
                mysql -uroot -p$password -e "grant all privileges on *.* to 'root'@'%' identified by 'It12345!'; flush privileges;"
        - name: create database
          mysql_db:
                name: wordpress
                state: present
                login_user: root
                login_password: 'It12345!'
        - name: port open
          firewalld:
                port: 3306/tcp
                immediate: yes
                permanent: yes
                state: enabled                                 

이렇게 설정을 다 마치면 proxy 설정을 해놓은 10.0.0.3에 접속하면 워드프레스 화면이 나옵니다.

반응형