上一篇文档讲述了如何写一个playbook Ansible快速部署web application 这篇文章,讲述一下我们如何把playbook转换成role格式.
创建roles的目录结构
app.py inventory.txt playbook.yml roles
flask_web mysql_db python
defaults files handlers meta README.md tasks templates tests vars
这里可以看到 ansible-galaxy init 创建的目录结构都一样。接下来,我们开始填充roles目录里的内容。
mysql_db role为例
修改 tasks/main.yml ,填入mysql 安装,设置密码,建库,建表的操作。
---
- name: Add repository
yum_repository:
name: mysql57-community
description: MySQL 5.7 Community Server
baseurl: http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
gpgkey: http://repo.mysql.com/RPM-GPG-KEY-mysql
- name: Install and Configure Database
yum: name=mysql-community-server state=present
- name: Start MySQL Service
service: name=mysqld state=started enabled=yes
- name: check file /tmp/init-mysql.log exist
stat:
path: /tmp/init-mysql.log
register: init_mysql_log
- name: Init mysql password
shell: mysql -uroot -p`grep 'temporary password' /var/log/mysqld.log| awk '{print $NF}'` --connect-expired-password -e "alter user root@localhost identified by '{{ db_password }}' ;" > /tmp/init-mysql.log
when: not init_mysql_log.stat.exists
- name: Create application Database
mysql_db: name={{ db_name }} login_host=127.0.0.1 login_user=root login_password={{ db_password }} state=present
- name: Create Database User
mysql_user: name={{ db_user }} host=% password={{ db_password }} priv='*.*:ALL' login_user=root login_password={{ db_password }} state=present
- name: check file /tmp/table-mysql.log exist
stat:
path: /tmp/table-mysql.log
register: table_mysql_log
- name: Create Table and insert data
shell: |
mysql -uroot -p{{ db_password }} --connect-expired-password -e "USE employee_db;CREATE TABLE employees (name VARCHAR(20));INSERT INTO employees VALUES ('JasonLong');" > /tmp/table-mysql.log
when: not init_mysql_log.stat.exists
修改 vars/main.yml , 存放mysql_db role的变量
---
db_name: employee_db
db_user: db_user
db_password: zaq1@WSX123
templates/目录存放模板文件 files/目录存放所需的配置文件 我这里没有用到,就不做说明。
flask_web role的 tasks/main.yml
---
- name: pip update to 20.2.4
shell: /usr/bin/python -m pip install --upgrade pip==20.2.4
- name: pip update to latest
shell: /usr/bin/python -m pip install --upgrade pip
- name: Install Python Flask dependency
pip:
name: ['flask', 'flask-mysql']
state: present
- name: Copy source code
copy: src=app.py dest=/opt/app.py
- name: Running flask app
shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 &
python role的 tasks/main.yml
- name: Install all required dependencies
yum:
name: [ 'python', 'python-setuptools', 'python-pip', 'MySQL-python' ]
state: present
最后我们写一个调用role的playbook
[root@master-1 web_applications_roles]
app.py inventory.txt playbook.yml roles
[root@master-1 web_applications_roles]
- name: Deploy a web application with role
hosts: app
roles:
- python
- mysql_db
- flask_web
playbook.yml
- name: Deploy a web application with role
hosts: app
roles:
- python
- mysql_db
- flask_web
调用命令
[root@master-1 web]
PLAY [Deploy a web application] ****************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [db_and_web_server-2]
TASK [Install all required dependencies] *******************************************************************************
ok: [db_and_web_server-2]
TASK [Add repository] **************************************************************************************************
ok: [db_and_web_server-2]
TASK [Install and Configure Database] **********************************************************************************
ok: [db_and_web_server-2]
TASK [Start MySQL Service] *********************************************************************************************
ok: [db_and_web_server-2]
TASK [check file /tmp/init-mysql.log exist] ****************************************************************************
ok: [db_and_web_server-2]
TASK [Init mysql password] *********************************************************************************************
changed: [db_and_web_server-2]
TASK [Create application Database] *************************************************************************************
changed: [db_and_web_server-2]
TASK [Create Database User] ********************************************************************************************
[WARNING]: Module did not set no_log for update_password
changed: [db_and_web_server-2]
TASK [check file /tmp/table-mysql.log exist] ***************************************************************************
ok: [db_and_web_server-2]
TASK [Create Table and insert data] ************************************************************************************
changed: [db_and_web_server-2]
TASK [pip update to 20.2.4] ********************************************************************************************
changed: [db_and_web_server-2]
TASK [pip update to latest] ********************************************************************************************
changed: [db_and_web_server-2]
TASK [Install Python Flask dependency] *********************************************************************************
changed: [db_and_web_server-2]
TASK [Copy source code] ************************************************************************************************
changed: [db_and_web_server-2]
TASK [Running flask app] ***********************************************************************************************
changed: [db_and_web_server-2]
PLAY RECAP *************************************************************************************************************
db_and_web_server-2 : ok=16 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
最后网页访问 http://192.168.5.202:5000/ 即可
|