https://daaa0555.tistory.com/382
# 역할 (roles)
표준화 된 디렉토리 및 파일 구조를 기반으로 변수, 작업, 핸들러, 템플릿 등을 구조화 하며, Ansible Artifact를 자동으로 가져올 수 있다. 또한 서비스 와 기능별로 역할을 분리하면 쉽게 재사용하고 다른 사용자와 역할을 공유 할 수 있다.
- name: Database
hosts: dbserver
roles:
- database
- name: Web
hosts: webserver
roles:
- web
[defaults]
inventory= ./inventory
[webserver]
192.168.200.102
[dbserver]
192.168.200.101
[local]
192.168.200.100
service_port: 8080
# database
- name: Restart MySQL services
service:
name: mysql
state: restarted
- name: Restart MariaDB service
service:
name: mariadb
state: restarted
db:
mysql_sock: "/var/run/mysqld/mysqld.sock"
mysql_db_name: "wpdb"
mysql_user_name: "wpadm"
mysql_user_passwd: "qwer1234"
cat: o: No such file or directory
- name: Install Database in Debian
import_tasks: db_debian.yaml
when: ansible_facts['os_family'] == "Debian"
- name: Install Database in RedHat
import_tasks: db_redhat.yaml
when: ansible_facts['os_family'] == "RedHat"
- 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: "{{ groups['webserver'][0] }}"
priv: wpdb.*:ALL,GRANT
state: present
- name: External Connection Setting
lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
regexp: bind-address
line: bind-address=0.0.0.0
notify: Restart MySQL services
- 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
- name: External Connection Setting
lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
regexp: bind-address
line: bind-address=0.0.0.0
notify: Restart MariaDB services
# web
- name: Restart Apache2 services
service:
name: apache2
state: restarted
- name: Restart Httpd services
service:
name: httpd
state: restarted
db:
mysql_sock: "/var/run/mysqld/mysqld.sock"
mysql_db_name: "wpdb"
mysql_user_name: "wpadm"
mysql_user_passwd: "qwer1234"
web:
wp_url: "https://wordpress.org/wordpress-{{ wp_version }}.tar.gz"
wp_checksum: "https://wordpress.org/wordpress-{{ wp_version }}.tar.gz.sha1"
wp_tar: "/tmp/wordpress-{{ wp_version }}.tar.gz"
wp_config: "/var/www/html/wordpress/wp-config.php"
wp_version: "5.8"
port:
service_port: "{{ service_port }}"
- name: Install Wordpress in Debian
import_tasks: wp_debian.yaml
when: ansible_facts['os_family'] == "Debian"
- name: Install Wordpress in RedHat
import_tasks: wp_redhat.yaml
when: ansible_facts['os_family'] == "RedHat"
- 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: ../templates/ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- Restart Apache2 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: ../templates/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
- 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: ../templates/httpd.conf.j2
dest: /etc/httpd/conf/https.conf
notify:
- Restart Httpd 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: ../templates/wp-config.php.j2
dest: "{{ web['wp_config'] }}"
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen "{{ port['service_port'] }}"
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noeti
Listen "{{ port['service_port'] }}"
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME','{{ db['mysql_db_name'] }}' );
/** MySQL database username */
define( 'DB_USER', '{{ db['mysql_user_name'] }}' );
/** MySQL database password */
define( 'DB_PASSWORD', '{{ db['mysql_user_passwd'] }}' );
/** MySQL hostname */
define( 'DB_HOST', "{{ groups['dbserver'][0] }}" );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
# remove
- name: Wordpress Remove
hosts: 192.168.200.102
tasks:
- name: Stop Apache2 Service
service:
name: apache2
state: stopped
ignore_errors: true
- name: Remove Apache2 Package
apt:
name: apache2, php, php-mysql, php-gd, php-mbstring
state: absent
purge: yes
- name: Remove Dep-Apache2 Package
apt:
autoremove: yes
purge: yes
- name: Remove Apache Configuration
file:
path: /etc/apache2
state: absent
- name: Delete Wordpress Archive Files
file:
path: /tmp/wordpress-5.8.tar.gz
state: absent
- name: Delete Wordpress App
file:
path: /var/www/html
state: absent
- name: MySQL Remove
hosts: 192.168.200.101
tasks:
- name: Stop MySQL Service
service:
name: mysql
state: stopped
ignore_errors: true
- name: Remove MySQL Package
apt:
name: mysql-server, python3-pymysql, mysql-common, mysql-client
state: absent
purge: yes
- name: Remove MySQL Configuration
file:
path: /etc/mysql
state: absent
- name: Remove Dep-MySQL Package
apt:
autoremove: yes
purge: yes
- name: Cleanup Databases
file:
path: /var/lib/mysql
state: absent
'클라우드 > 앤서블(Ansible)' 카테고리의 다른 글
앤서블 Ansible 작업 제어 8 / 비동기 (0) | 2021.08.24 |
---|---|
앤서블 Ansible: Apache + Wordpress + MySQL 배포 2 (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 |