https://daaa0555.tistory.com/381
# 블록
play와 task의 중간 단계에 있는 논리적인 작업 그룹, 논리적으로 작업을 그룹으로 묶는다.
devops@control:~/wp$ cat wordpress.yaml
- name: MySQL Deployment
hosts: 192.168.200.101
vars_files:
- ./vars/vars.yaml
tasks:
- name: Database in Debian
block:
- name: Install MySQL Package
apt:
update_cache: true
name: mysql-server, python3-pymysql
state: present
- name: Starting MySQL Service
service:
name: mysql
state: started
enabled: yes
- name: Create Wordpress Database
mysql_db:
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_db_name'] }}"
state: present
- name: Create Wordpress User
mysql_user:
check_implicit_admin: yes
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_user_name'] }}"
password: "{{ db['mysql_user_passwd'] }}"
host: localhost
priv: wpdb.*:ALL,GRANT
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Database in RedHat
block:
- name: Install MariaDB Package
yum:
update_cache: true
name: mariadb-server, python3-pymysql
state: present
- name: Starting Maria Service
service:
name: mariadb
state: started
- name: Create Wordpress Database
mysql_db:
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_db_name'] }}"
state: present
enabled: yes
- name: Create Wordpress User
mysql_user:
check_implicit_admin: yes
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_user_name'] }}"
password: "{{ db['mysql_user_passwd'] }}"
host: localhost
priv: wpdb.*:ALL,GRANT
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Wordpress Deployment
hosts: 192.168.200.102
vars_files:
- ./vars/vars.yaml
tasks:
- name: Install Apache2 Package in Debian
block:
- name: Install Apache2 Package
apt:
name: apache2, php, php-mysql, php-gd, php-mbstring
state: present
- name: Change Apache2 Service Ports
template:
src: jinja/ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- Restart web services
- name: Starting Apache2 Service
service:
name: apache2
state: started
enabled: yes
- name: Download Wordpress Source
get_url:
url: "{{ web['wp_url'] }}"
# checksum: "{{ web['wp_checksum'] }}"
dest: "{{ web['wp_tar'] }}"
- name: Unarchive Wordpress Source
unarchive:
src: "{{ web['wp_tar'] }}"
remote_src: true
dest: /var/www/html
- name: Setting Wordpress Database Configuration
template:
src: jinja/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
when: ansible_facts['os_family'] == "Debian"
- name: Install Apache2 Package in RedHat
block:
- name: Install Httpd Package
apt:
name: httpd, php, php-mysql, php-gd, php-mbstring
state: present
- name: Change Httpd Service Ports
template:
src: jinja/ports.conf.j2
dest: /etc/httpd/conf/https.conf
notify:
- Restart web services
- name: Starting Httpd Service
service:
name: httpd
state: started
enabled: yes
- name: Download Wordpress Source
get_url:
url: "{{ web['wp_url'] }}"
# checksum: "{{ web['wp_checksum'] }}"
dest: "{{ web['wp_tar'] }}"
- name: Unarchive Wordpress Source
unarchive:
src: "{{ web['wp_tar'] }}"
remote_src: true
dest: /var/www/html
- name: Setting Wordpress Database Configuration
template:
src: jinja/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
when: ansible_facts['os_family'] == "RedHat"
handlers:
- name: Restart web services
service:
name: apache2
state: restarted
Database in Debian Debian에서의 데이터베이스, 워드프레스 구성
Database in RedHat RedHat에서의 데이터베이스 구성, 워드프레스 구성
Install Apache2 Package in Debian Debian에서 apache2 구성
Install Apache2 Package in RedHat RedHat에서 httpd구성
4개의 논리적인 작업 그룹으로 묶는다.
-> 블록 이름을 잘못 설정 하였으므로 수정하는것을 권장한다
# 비동기 작업
시간이 오래 걸리는 작업을 백그라운드로 실행 시켜놓고 후속 작업을 동시에 실행 시킨다.
devops@control:~/wp$ cat wordpress.yaml
- name: MySQL Deployment
hosts: 192.168.200.101
vars_files:
- ./vars/vars.yaml
tasks:
- name: Database in Debian
block:
- name: Install MySQL Package
apt:
update_cache: true
name: mysql-server, python3-pymysql
state: present
- name: Starting MySQL Service
service:
name: mysql
state: started
enabled: yes
- name: Create Wordpress Database
mysql_db:
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_db_name'] }}"
state: present
- name: Create Wordpress User
mysql_user:
check_implicit_admin: yes
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_user_name'] }}"
password: "{{ db['mysql_user_passwd'] }}"
host: localhost
priv: wpdb.*:ALL,GRANT
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Database in RedHat
block:
- name: Install MariaDB Package
yum:
update_cache: true
name: mariadb-server, python3-pymysql
state: present
- name: Starting Maria Service
service:
name: mariadb
state: started
- name: Create Wordpress Database
mysql_db:
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_db_name'] }}"
state: present
enabled: yes
- name: Create Wordpress User
mysql_user:
check_implicit_admin: yes
login_unix_socket: "{{ db['mysql_sock'] }}"
name: "{{ db['mysql_user_name'] }}"
password: "{{ db['mysql_user_passwd'] }}"
host: localhost
priv: wpdb.*:ALL,GRANT
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Wordpress Deployment
hosts: 192.168.200.102
vars_files:
- ./vars/vars.yaml
tasks:
- name: Install Apache2 Package in Debian
block:
- name: Download Wordpress Source
get_url:
url: "{{ web['wp_url'] }}"
# checksum: "{{ web['wp_checksum'] }}"
dest: "{{ web['wp_tar'] }}"
async: 300
poll: 0
register: wp_download_result
- name: Install Apache2 Package
apt:
name: apache2, php, php-mysql, php-gd, php-mbstring
state: present
- name: Change Apache2 Service Ports
template:
src: jinja/ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- Restart web services
- name: Starting Apache2 Service
service:
name: apache2
state: started
enabled: yes
- name: Sync Download Wordpress
async_status:
jid: "{{ wp_download_result.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 100
delay: 5
- name: Unarchive Wordpress Source
unarchive:
src: "{{ web['wp_tar'] }}"
remote_src: true
dest: /var/www/html
- name: Setting Wordpress Database Configuration
template:
src: jinja/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
when: ansible_facts['os_family'] == "Debian"
- name: Install Apache2 Package in RedHat
block:
- name: Download Wordpress Source
get_url:
url: "{{ web['wp_url'] }}"
# checksum: "{{ web['wp_checksum'] }}"
dest: "{{ web['wp_tar'] }}"
async: 300
poll: 0
register: wp_download_result
- name: Install Httpd Package
apt:
name: httpd, php, php-mysql, php-gd, php-mbstring
state: present
- name: Change Httpd Service Ports
template:
src: jinja/ports.conf.j2
dest: /etc/httpd/conf/https.conf
notify:
- Restart web services
- name: Starting Httpd Service
service:
name: httpd
state: started
enabled: yes
- name: Sync Download Wordpress
async_status:
jid: "{{ wp_download_result.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 100
delay: 5
- name: Unarchive Wordpress Source
unarchive:
src: "{{ web['wp_tar'] }}"
remote_src: true
dest: /var/www/html
- name: Setting Wordpress Database Configuration
template:
src: jinja/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
when: ansible_facts['os_family'] == "RedHat"
handlers:
- name: Restart web services
service:
name: apache2
state: restarted
async: 300 -> 300초동안 세션 타임아웃 방지
poll: 0 -> 비동기 작업
register: wp_download_result -> 결과 이름 등록
작업을 비동기화 시킨다 .
- name: Sync Download Wordpress
async_status:
jid: "{{ wp_download_result.ansible_job_id }}" -> 작업이 끝났는지 안끝난건지 확인
register: job_result -> 결과 이름 등록
until: job_result.finished -> 반복문으로 테스크 작업이 참이 될때까지 반복
retries: 100 -> 재시도 횟수
delay: 5 -> 지연 시간
비동기 였던 작업을 다시 동기화 시킨다.
# playbook 실행
'클라우드 > 앤서블(Ansible)' 카테고리의 다른 글
앤서블 Ansible 작업 제어 8 / 비동기 (0) | 2021.08.24 |
---|---|
앤서블 Ansible: Apache + Wordpress + MySQL 배포 3 (0) | 2021.08.09 |
앤서블 Ansible: Apache + Wordpress + MySQL 배포 1 (0) | 2021.08.09 |
앤서블 Ansible 작업 제어 6 / 작업 오류 처리 (0) | 2021.08.04 |
앤서블 Ansible AWX (0) | 2021.08.04 |