Ansible 中的 Unit§
Ansible 集合 由 XLAB Steampunk 提供,它提供了一些与 Ansible 配合使用的 Unit 相关任务;其中一些任务简化了安装和设置,而其他任务提供了常见的配置步骤。
首先,安装该集合
$ ansible-galaxy collection install steampunk.unit
安装后,可以在剧本中使用它。考虑使用此 WSGI 应用
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
return (b"Hello, Python on Unit!")
此 剧本 使用 Python 语言模块安装 Unit,复制应用文件并运行应用
---
- name: Install and run NGINX Unit
hosts: unit_hosts
become: true
tasks:
- name: Install Unit
include_role:
name: steampunk.unit.install
- name: Create a directory for our application
file:
path: /var/www
state: directory
- name: Copy application
copy:
src: files/wsgi.py
dest: /var/www/wsgi.py
mode: "644"
- name: Add application config to Unit
steampunk.unit.python_app:
name: sample
module: wsgi
path: /var/www
- name: Expose application via port 3000
steampunk.unit.listener:
pattern: "*:3000"
pass: applications/sample
最后的准备步骤是 主机清单,其中列出了受管理主机的地址
all:
children:
unit_hosts:
hosts:
203.0.113.1:
一切就绪后,启动剧本
$ ansible-playbook -i inventory.yaml playbook.yaml
PLAY [Install and run NGINX Unit] ***
...
TASK [Expose application via port 3000] ***
ok: [203.0.113.1]
PLAY RECAP ********************************
203.0.113.1 : ok=15 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如果正常,请尝试在清单中的主机地址和剧本中设置的端口号处使用该应用
$ curl 203.0.113.1:3000
Hello, Python on Unit!