반응형
Ansible 설치 및 설정에 이어서 진행 해보도록 하겠습니다.
우선 wordpress를 2번 서버에 설치하고 설정하는 작업을 ansible을 사용하여 해보겠습니다.
처음 작성한 코드
--- - name: wordpress down and start hosts: web tasks: - name: wget, httpd install yum: name: "{{ item }}" state: latest loop: - wget - httpd - epel-release - yum-utils - http://rpms.remirepo.net/enterprise/remi-release-7.rpm - php - php-common - php-opcache - php-mcrypt - php-cli - php-gd - php-curl - php-mysqlnd - name: yum confg & php install shell: 'yum-config-manager --enable remi-php7.4' - name: wget link down shell: 'wget https://ko.wordpress.org/wordpress-5.8.6-ko_KR.tar.gz' - name: tar xvfz shell: 'tar xvfz wordpress-5.8.6-ko_KR.tar.gz' - name: directory permission change shell: 'chown -R root:root /root/wordpress' - name: wordpress file copy shell: 'cp -r /root/wordpress/* /var/www/html/' - name: wp-config.php copy copy: src: /var/www/html/wp-config-sample.php dest: /var/www/html/wp-config.php - name: change index.php lineinfile: path: /etc/httpd/conf/httpd.conf regexp: 'DirectoryIndex health.html' line: 'DirectoryIndex index.php' - name: change database_name replace: path: /var/www/html/wp-config.php regexp: 'database_name_here' replace: 'wordpress' - name: change username replace: path: /var/www/html/wp-config.php regexp: 'username_here' replace: 'root' - name: change password replace: path: /var/www/html/wp-config.php regexp: 'password_here' replace: 'It12345!' - name: change localhost replace: path: /var/www/html/wp-config.php regexp: 'localhost' replace: '10.0.0.3' - name: httpd service start service: name: httpd state: restarted
처음 작성한 코드에서는 shell 명령어를 사용하여 작성한 부분이 있는데, ansible 모듈에서 해당 부분을 찾아서 다시 수정을 해보았습니다.
수정한 코드
--- - name: wordpress down and start hosts: web tasks: - name: wget, httpd install yum: name: "{{ item }}" state: latest loop: **=> [loop를 사용하여 반복문 처럼 아래 패키지 설치]** - httpd - epel-release - yum-utils - http://rpms.remirepo.net/enterprise/remi-release-7.rpm - php - php-common - php-opcache - php-mcrypt - php-cli - php-gd - php-curl - php-mysqlnd - name: yum confg & php install shell: 'yum-config-manager --enable remi-php7.4' - name: wordpress.tar file download 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: directory permission change shell: 'chown -R root:root /root/wordpress' - name: wordpress file copy copy: src: /root/wordpress/ dest: /var/www/html/ remote_src: yes **=> [하위 폴더에까지 적용시키거나 원격으로 파일을 건드릴 때 사용하는 옵션]** - name: wp-config.php copy copy: src: /var/www/html/wp-config-sample.php dest: /var/www/html/wp-config.php - name: change index.php lineinfile: path: /etc/httpd/conf/httpd.conf regexp: 'DirectoryIndex health.html' line: 'DirectoryIndex index.php' - name: change database_name replace: path: /var/www/html/wp-config.php regexp: "{item.regexp}" replace: "{item.replace}" loop: - {regexp: "datebasename_here", replace: "wordpress" } - {regexp: "username_here", replace: "root" } - {regexp: "password_here", replace: "It12345!" } - {regexp: "localhost", replace: "10.0.0.4" } - name: httpd service start service::ㅈㅂ name: httpd state: restarted
그 후 3번 서버에도 wordpress를 설치해주고 haproxy도 설치 후 설정을 해주도록 하겠습니다.
3번서버 wordpress & haproxy 설치 및 설정
--- - name: wordpress down and start hosts: was tasks: - name: package install yum: name: "{{ item }}" state: latest loop: - httpd - epel-release - yum-utils - http://rpms.remirepo.net/enterprise/remi-release-7.rpm - haproxy - name: yum config shell: 'yum-config-manager --enable remi-php7.4' - name: php7.4 install yum: name: "{{ item }}" state: latest loop: - php - php-common - php-opcache - php-mcrypt - php-cli - php-gd - php-curl - php-mysqlnd - name: wordpress.tar file download 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: directory permission change shell: 'chown -R root:root /root/wordpress' - name: wordpress file copy copy: src: /root/wordpress/ dest: /var/www/html/ remote_src: yes - name: wp-config.php copy copy: src: /var/www/html/wp-config-sample.php dest: /var/www/html/wp-config.php - name: change index.php & Listen port lineinfile: path: /etc/httpd/conf/httpd.conf regexp: "{{ item.src }}" line: "{{ item.dest }}" loop: - {src: 'DirectoryIndex health.html', dest: 'DirectoryIndex health.php'} - {src: 'Listen 80', dest: 'Listen 8080'} - name: change database_name replace: path: /var/www/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: haproxy.cfg line change replace: path: /etc/haproxy/haproxy.cfg regexp: "{{ item.src }}" replace: " {{ item.dest }}" loop: - {src: ":5000", dest: ":80" } - {src: "127.0.0.1:5001", dest: "10.0.0.2:80" } - {src: "127.0.0.1:5002", dest: "10.0.0.3:8080" } - name: haproxy.cfg line delete lineinfile: path: /etc/haproxy/haproxy.cfg regexp: "{{ item.regexp }}" line: "{{ item.line }}" loop: - {regexp: "127.0.0.1:5003", line: "#" } - {regexp: "127.0.0.1:5004", line: "#" } - name: httpd service start service: name: "{{ item }}" state: restarted loop: - httpd - haproxy
위 설정 및 설치를 완료하면 3번 서버로 인터넷에서 url을 입력하면 로드밸런싱을 통해 2번서버나 3번서버가 나오게 된다.
반응형
'Ansible' 카테고리의 다른 글
AWX 설치 (k8s 환경) (0) | 2024.05.13 |
---|---|
Ansible 사용하여 vSphere 환경에서 가상머신 생성 (0) | 2024.03.27 |
Ansible 사용하여 nginx 설치 및 index 페이지 변경 (0) | 2024.02.14 |
Ansible 모듈 사용해보기 (shell, user, file, copy ...등) (0) | 2024.02.05 |
Ansible 설치 및 설정 (0) | 2024.01.18 |