- Ansible 2 Cloud Automation Cookbook
- Aditya Patawari Vikas Aggarwal
- 349字
- 2021-06-24 18:43:36
How to do it…
The application deployment can be done using Ansible. If we are going to deploy the application using SQLite then the following tasks for the phonebook role are good enough:
---
- name: install epel repository
package:
name: epel-release
state: present
- name: install dependencies
package:
name: "{{ item }}"
state: present
with_items:
- git
- python-pip
- gcc
- python-devel
- name: install python libraries
pip:
name: "{{ item }}"
state: present
with_items:
- flask
- flask-sqlalchemy
- flask-migrate
- uwsgi
- name: get the application code
git:
repo: git@github.com:ansible-cookbook/phonebook-sqlite.git
dest: /opt/phone-book
- name: upload systemd unit file
copy:
src: phone-book.service
dest: /etc/systemd/system/phone-book.service
- name: start phonebook
systemd:
state: started
daemon_reload: yes
name: phone-book
enabled: yes
In the case of MySQL, we need to add some more tasks and information to work with Ansible:
---
- name: include secrets
include_vars: secrets.yml
- name: install epel repository
package:
name: epel-release
state: present
- name: install dependencies
package:
name: "{{ item }}"
state: present
with_items:
- git
- python-pip
- gcc
- python-devel
- mysql-devel
- name: install python libraries
pip:
name: "{{ item }}"
state: present
with_items:
- flask
- flask-sqlalchemy
- flask-migrate
- uwsgi
- MySQL-python
- name: get the application code
git:
repo: git@github.com:ansible-cookbook/phonebook-mysql.git
dest: /opt/phone-book
force: yes
- name: upload systemd unit file
copy:
src: phone-book.service
dest: /etc/systemd/system/phone-book.service
- name: upload app config file
template:
src: config.py
dest: /opt/phone-book/config.py
- name: create phonebook database
mysql_db:
name: phonebook
state: present
login_host: "{{ mysql_host }}"
login_user: root
login_password: "{{ mysql_root_password }}"
- name: create app user for phonebook database
mysql_user:
name: app
password: "{{ mysql_app_password }}"
priv: 'phonebook.*:ALL'
host: "%"
state: present
login_host: "{{ mysql_host }}"
login_user: root
login_password: "{{ mysql_root_password }}"
- name: start phonebook
systemd:
state: started
daemon_reload: yes
name: phone-book
enabled: yes
Accordingly, we will create a secrets.yml in vars directory and encrypt it using ansible-vault. The unencrypted data will look like this:
---
mysql_app_password: appSecretPassword
mysql_root_password: secretPassword
mysql_host: 35.199.168.191
The phone-book.service will take care of initializing the database and running the uwsgi server for serving the application for both SQLite and MySQL based setups:
[Unit]
Description=Simple Phone Book
[Service]
WorkingDirectory=/opt/phone-book
ExecStartPre=/bin/bash /opt/phone-book/init.sh
ExecStart=/usr/bin/uwsgi --http-socket 0.0.0.0:8080 --manage-script-name --mount /phonebook=app:app
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Throughout the coming chapters, we will use this role to deploy our phone book application.
推薦閱讀
- 零起步輕松學(xué)單片機(jī)技術(shù)(第2版)
- Go Machine Learning Projects
- ETL with Azure Cookbook
- 圖形圖像處理(Photoshop)
- 自動(dòng)檢測(cè)與傳感技術(shù)
- 工業(yè)機(jī)器人工程應(yīng)用虛擬仿真教程:MotoSim EG-VRC
- 塊數(shù)據(jù)5.0:數(shù)據(jù)社會(huì)學(xué)的理論與方法
- Visual Basic.NET程序設(shè)計(jì)
- 大數(shù)據(jù)驅(qū)動(dòng)的機(jī)械裝備智能運(yùn)維理論及應(yīng)用
- 突破,Objective-C開發(fā)速學(xué)手冊(cè)
- 電腦上網(wǎng)輕松入門
- 電氣控制與PLC原理及應(yīng)用(歐姆龍機(jī)型)
- 21天學(xué)通Linux嵌入式開發(fā)
- Creating ELearning Games with Unity
- 手把手教你學(xué)Photoshop CS3