Ansible - Wordpress install & configuration

FromCloud·2023년 6월 12일
0

Ansible

목록 보기
6/10

Ansible을 이용한 Wordpress 구축

---
- name: wordpress download & install, php7.4 install
  hosts: web1
  tasks:
  - name: wget, httpd, php repository, php7.4 repo install
    yum:
      name: "{{ item }}"
      state: present
    loop:
      - wget
      - httpd
      - yum-utils
      - epel-release
      - http://rpms.remirepo.net/enterprise/remi-release-7.rpm

  - name: yum-config-manager config
    shell:
      yum-config-manager --enable remi-php74

  - name: php7.4 install
    yum:
      name: "{{ packages }}"
    vars:
      packages:
        - php
        - php-common
        - php-opcache
        - php-mcrypt
        - php-cli
        - php-gd
        - php-curl
        - php-mysqlnd

  - name: url use file download
    get_url:
      url: https://ko.wordpress.org/wordpress-5.8.6-ko_KR.tar.gz
      dest: ./

  - name: unarchive
    unarchive:
      src: wordpress-5.8.6-ko_KR.tar.gz
      dest: ./
      remote_src: yes

  - name: index.html change index.php
    lineinfile:
      path: /etc/httpd/conf/httpd.conf
      regexp: 'DirectoryIndex index.html'
      line: 'DirectoryIndex index.php'

  - name: wordpress directory all file copy to /var/www/html
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
      remote_src: yes
    loop:
      - {src: './wordpress/', dest: '/var/www/html/' }
      - {src: '/var/www/html/wp-config-sample.php', dest: '/var/www/html/wp-config.php' }
      

  - name: wp-config.php file change
    replace:
      path: /var/www/html/wp-config.php
      regexp: "{{ item.src }}"
      replace: "{{ item.dest }}"
    loop:
      - {src: "database_name_here", dest: "wordpress" }
      - {src: "username_here", dest: "root" }
      - {src: "password_here", dest: "It12345@" }
      - {src: "localhost", dest: "10.0.0.4" }

  - name: httpd service started
    service:
      name: httpd
      state: started

  - name: httpd firewall open
    firewalld:
      port: 80/tcp
      state: enabled

이전과 달리 코드가 상당히 길어졌다 하지만 어려운 코드는 없으니 천천히 설명하자면
처음에 나오는 "{{ item }}" 은 loop 라고 해서 yum에서 많은 다운로드나 아니면 다른 모듈에서 많은 작업을 시행할때 코드를 최적화 하기 위해 많이 사용한다 뒤에 꼭 들여쓰기에 맞게 loop를 사용해 줘야한다

"{{ package }}"는 변수를 설정해준것이라서 밑에 보면 vars 아래에 package가 있는 것이 보인다

get_urllinux에서 wget 과 같은것이며 ansible module로서 보자면 get_url을 사용하면 된다

unarchive 또한 linux에서 tar 즉 압축을 해제하는 명령어와 같으며 일반 shelltar xvfz를 하면 ansible에서도 unarchive을 사용하는 것을 추천하는 모습을 볼 수 있다

특히하게 밑에 나오는 remote_src는 무엇이냐면 현재 ansible을 실행하는 controller에는 ansible을 실행하지 않는다 그래서 작업을 하는 노드에는 어떤 파일이 있는지 몰라 원격으로 제어를 하기 위해서 사용하는 명령어 이다 원격으로 같은 디렉터리에 파일을 복사한다던지 하는 작업을 할때는 꼭 넣어줘야 Error를 방지 할 수 있다

lineinfile 은 먼저 path: 를 설정해줘야하는데 작업을 해줘야하는 위치를 나타내는 것이다 regexp에 적혀있는 단어들은 path안에 있는 이름 중에 변경을 요하는 단어들을 제시 하여 준것이며 line:에 적혀있는 파일들로 regexp에 들어있는 라인전체를 바꿔 준다
하지만!!! lineinfile을 써야할때는 중요한 점이 하나 있다 무엇가를 바꿀때 전체 라인을 바꿔주기 때문에 한 단어를 바꾸는 것이 안되기 때문에 특정 단어를 바꿔줘야 한다면 replace 모듈을 추천한다

copy 모듈 src, dest가 필수적으로 들어가야하며 이것 또한 원격지에서 파일을 복사 하는 모듈 이기때문에 remote_src가 들어있는 모습을 볼 수 있다

"{{ item }}" 과 달리 "{{ item.src }}", "{{ item.dest }}"는 무엇인가??
이것은 .뒤에 나와있는 것은 변수를 이야기 하는 것이다 그래서 밑에 보면 {src: "database_name_here", dest: "wordpress" } 이렇게 설정한 변수에 맞게 src, dest가 들어있는 모습을 볼 수 있다 물론 변수 이기 때문에 내가 원하는데로 "{{ item.abc }}","{{ item.def }}"이런식으로 바꿔줘도 무방하지만 이것 또한 주의 할 점은 loop 부분에서도{abc: "database_name_here", def: "wordpress" } 이렇게 변경을 해줘야한다

wordpress를 구축하기 위해 스크립트도 작성해봤지만 스크립트보다 더욱 간편하게 구성이 가능한 장점을 느꼇다 처음 코드 짤때에는 시간이 좀 걸리지만 그래도 한번 만들어 놓으면 언제든지 재사용이 가능한 장점이 있다

profile
매일 발전하는 Cloud Engineer

0개의 댓글