Ansible 5일차

권수한·2023년 6월 14일
0

Ansible 사용해보기

목록 보기
2/3

저번까지는 apache + wordpress를 구성을 해보았다면

컨테이너 사용하기에 앞서 nginx + wordpress를 ansible을 사용하여 구성해보겠습니다.


명령어

---
- name: nginx + wordpress
  hosts: nginx
  tasks:
        - name: yum install package
          yum:
                name: "{{ item }}"
          loop:
                - wget
                - yum-utils
                - epel-release
                - nginx
                - http://rpms.remirepo.net/enterprise/remi-release-7.rpm
        - name: get url
          get_url:
                url: https://ko.wordpress.org/wordpress-5.8.6-ko_KR.tar.gz
                dest: /root
        - name: tar unarchive
          unarchive:
                src: /root/wordpress-5.8.6-ko_KR.tar.gz
                dest: /root
                remote_src: yes
        - name: wordpress directory chown
          shell: 'chown -R root:root /root/wordpress'
        - name: copy wordpress files to /usr/share/nginx/html
          copy:
                src: /root/wordpress/
                dest: /usr/share/nginx/html
                remote_src: yes
        - name: copy wp-config-sample to wp-config
          copy:
                src: /usr/share/nginx/html/wp-config-sample.php
                dest: /usr/share/nginx/html/wp-config.php
                remote_src: yes
        - name: yum config change
          shell: 'yum-config-manager --enable remi-php74'
        - name: yum install php *
          yum:
                name: "{{ item }}"
          loop:
                - php
                - php-common
                - php-opcache
                - php-mcrypt
                - php-cli
                - php-gd
                - php-curl
                - php-mysqlnd
                - php-fpm
        - name: /etc/php-fpm.d/www.conf content change
          lineinfile:
                path: /etc/php-fpm.d/www.conf
                regexp: "{{ item.regexp }}"
                line: "{{ item.line }}"
          loop:
                - { regexp: "user = apache", line: "user = nginx" }
                - { regexp: "group = apache", line: "group=nginx" }
                - { regexp: ";listen.owner = nobody", line: "listen.owner = nginx" }
                - { regexp: ";listen.group = nobody", line: "listen.group = nginx" }
        - name: nginx.conf content add
          shell: "sed -i 's/_;/_; index      index.php;/g' /etc/nginx/nginx.conf"
        - name: nginx.conf content add-2
          blockinfile:
                path: /etc/nginx/nginx.conf
                insertafter: "listen       80;"
                block: |
                        location ~ \.php$ {
                            try_files $uri =404;
                            fastcgi_pass unix:/run/php-fpm/www.sock;
                            fastcgi_index   index.php;
                            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                            include fastcgi_params;
                        }
        - name: /etc/php-fpm.d/www.conf content add
          blockinfile:
                path: /etc/php-fpm.d/www.conf
                insertafter: ";listen = 127.0.0.1:9000"
                block: >
                       listen = /run/php-fpm/www.sock
        - name: change database_name
          replace:
                path: /usr/share/nginx/html/wp-config.php
                regexp: "{{ item.regexp }}"
                replace: "{{ item.replace }}"
          loop:
                - {regexp: "database_name_here", replace: "wordpress" }
                - {regexp: "username_here", replace: "root" }
                - {regexp: "password_here", replace: "It12345!" }
                - {regexp: "localhost", replace: "10.0.0.4" }
        - name: firewall open
          firewalld:
                port: 80/tcp
                state: enabled
        - name: service start
          systemd:
                name: "{{ item }}"
                state: started
          loop:
                - php-fpm
                - nginx

위 명령어로 yml파일을 만들어서 playbook을 실행하고 url창에 10.0.0.5로 들어가면 워드프레스 화면이 나옵니다.

다음에는 지금까지 사용했었던 모듈에 대해 정리해보도록 하겠습니다.

0개의 댓글