435 Role vbotka.freebsd.nginx
Use case
Create iocage template ansible-nginx. Create jails from the template.
Tree
shell> tree .
├── ansible.cfg
├── files
│ └── index.html
├── group_vars
│ ├── all
│ │ ├── project-hosts.yml
│ │ └── project.yml
│ └── nginx
│ └── nginx.yml
├── hosts
│ └── 06_iocage2.yml
├── host_vars
│ └── iocage_06
│ └── template.yml
├── iocage.ini
├── pb-iocage-template.yml
└── pb-nginx.yml
Synopsis
At a managed node:
Use the role vbotka.freebsd.iocage_template to create the template
ansible-nginxIn the playbook vbotka.freebsd.pb_iocage_project_create_from_templates.yml create jails from the template.
In the inventory group
nginxuse the role vbotka.freebsd.nginx to configure the Nginx servers.
Requirements
Notes
TBD
See also
TBD
ansible.cfg
[defaults]
callback_result_format = yaml
deprecation_warnings = false
display_skipped_hosts = false
gathering = explicit
interpreter_python = auto_silent
log_path = /var/log/ansible.log
[connection]
pipelining = true
Inventory iocage.ini
iocage_06
[iocage]
iocage_06
[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent
hosts
hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
sudo: true
get_properties: true
inventory_hostname_tag: alias
compose:
iocage_tags: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)'))
iocage_classes: iocage_properties.notes | regex_findall('(?<=class=)[\w\-]+|(?<=,)[\w\-]+')
# connection plugin vbotka.freebsd.jailexec
ansible_connection: "'vbotka.freebsd.jailexec'"
ansible_ssh_user: "'admin'"
ansible_jail_host: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)')).vmm | d('none')
ansible_jail_name: iocage_jid
ansible_jail_privilege_escalation: "'sudo'"
# ansible option
ansible_python_interpreter: "'auto_silent'"
groups:
nginx: iocage_classes is contains('nginx')
keyed_groups:
- prefix: state
key: iocage_state
- prefix: vmm
key: iocage_tags.vmm
group_vars
group_vars/all/project-hosts.yml
project_hosts:
iocage_06:
defaultrouter: 172.16.99.1
log_server: 172.16.99.10
repos: 172.16.99.21
repos_devel: 172.16.99.22
pkg_repo: 172.16.99.23
group_vars/all/project.yml
project:
www-01:
notes: alias=www_01
vmm: iocage_06
template: ansible-nginx
class: [nginx]
properties:
bpf: 1
dhcp: 1
vnet: 1
boot: 1
www-02:
notes: alias=www_02
vmm: iocage_06
template: ansible-nginx
class: [nginx]
properties:
bpf: 1
dhcp: 1
vnet: 1
boot: 1
vmm_groups: "{{ dict(project | dict2items | groupby('value.vmm')) }}"
vmm: "{{ dict(vmm_groups.keys() | zip(vmm_groups.values() | map('items2dict'))) }}"
group_vars/nginx/nginx.yml
nginx_install: false
nginx_enable: true
nginx_debug: false
nginx_backup_conf: true
nginx_packages:
- www/nginx-lite
nginx_conf:
- path: "{{ nginx_conf_dir }}/nginx/nginx.conf"
conf:
worker_processes: ['1']
events:
worker_connections: ['1024']
http:
include: ['mime.types']
default_type: ['application/octet-stream']
sendfile: ['on']
keepalive_timeout: ['65']
server:
listen: ['80']
server_name: ['localhost']
location /:
root: ['/usr/local/www/nginx']
index: ['index.html', 'index.htm']
host_vars
host_vars/iocage_06/template.yml
fit_pkg_install: true
# fit_pkg_cached: true
# fit_pkg_pkgsite: local
fit_templates:
ansible-nginx:
release: 15.1-RELEASE
pkg:
- www/nginx-lite
- lang/python312
- lang/python
- lang/python3
files: "{{ fit_files | dict2items }}"
properties:
ip4: disable
ip6: disable
ip6_addr: none
bpf: 1
dhcp: 1
vnet: 1
notes: '"class=nginx alias=ansible_nginx"'
fit_files:
/usr/local/www/nginx:
- index.html
fit_file_options:
/usr/local/www/nginx:
owner: root
group: www
mode: '0755'
/usr/local/www/nginx/index.yml:
owner: root
group: www
mode: '0644'
files
files/index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>It works! Nginx</title>
</head>
<body>
<p>It works!</p>
</body>
</html>
Playbook pb-iocage-template.yml
---
- name: Create iocage templates.
hosts: iocage
roles:
- vbotka.freebsd.iocage_template
Playbook output - Create iocage templates
(env) > ansible-playbook -i iocage.ini pb-iocage-template.yml
PLAY [Create iocage templates.] ************************************************
TASK [vbotka.freebsd.iocage_template : Setup: Get iocage list of templates.] ***
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Setup: Get activated pool.] *************
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Create: Get iocage list of jails.] ******
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Create: Create jails.] ******************
ok: [iocage_06] => (item=ansible-nginx 15.1-RELEASE)
TASK [vbotka.freebsd.iocage_template : Start: Get iocage list of jails.] *******
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Start: Start jails.] ********************
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Pkg: Get iocage list of jails.] *********
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Pkg: Install packages.] *****************
changed: [iocage_06] => (item=ansible-nginx)
TASK [vbotka.freebsd.iocage_template : Files: Copy files.] *********************
included: /scratch/collections/ansible_collections/vbotka/freebsd/roles/iocage_template/tasks/fn/files.yml for iocage_06 => (item=ansible-nginx)
TASK [vbotka.freebsd.iocage_template : Fn/files: Create directory.] ************
changed: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Fn/files: Copy files.] ******************
changed: [iocage_06] => (item=index.html)
TASK [vbotka.freebsd.iocage_template : Stop: Get iocage list of jails.] ********
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Stop: Stop jails.] **********************
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Template: Set template.] ****************
ok: [iocage_06] => (item=ansible-nginx)
PLAY RECAP *********************************************************************
iocage_06 : ok=14 changed=3 unreachable=0 failed=0 skipped=38 rescued=0 ignored=0
List templates
shell > ssh admin@iocage_06 sudo iocage list -lt
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+======+=========================+======+=======+==========+=================+=======================+=====+==========+==========+
| None | ansible-client | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-client-apache | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-client-pull | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init-devel | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init-example | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-nginx | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-pkg-repo | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-repos | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-syslogng-client | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-syslogng-server | off | down | template | 15.1-RELEASE-p1 | vnet0|172.16.99.41/24 | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
Playbook output - Create project jails from iocage templates
(env) > ansible-playbook -i iocage.ini -i hosts vbotka.freebsd.pb_iocage_project_create_from_templates.yml
PLAY [Create and start project jails from iocage templates.] *******************
TASK [Setup: Get activated pool.] **********************************************
ok: [iocage_06]
TASK [Create: Create jails.] ***************************************************
ok: [iocage_06] => (item=www-02)
ok: [iocage_06] => (item=www-01)
TASK [Ai-vars-class: Create ansible init vars dirs.] ***************************
changed: [iocage_06] => (item=www-01)
changed: [iocage_06] => (item=www-02)
TASK [Ai-vars-jail: Create ansible init vars dirs.] ****************************
ok: [iocage_06] => (item=www-01)
ok: [iocage_06] => (item=www-02)
TASK [Properties: Set properties.] *********************************************
ok: [iocage_06] => (item=www-01)
ok: [iocage_06] => (item=www-02)
TASK [Start: Start jails.] *****************************************************
ok: [iocage_06]
PLAY RECAP *********************************************************************
iocage_06 : ok=6 changed=1 unreachable=0 failed=0 skipped=23 rescued=0 ignored=0
Inventory graph
shell > ansible-inventory -i hosts --graph
@all:
|--@ungrouped:
|--@state_up:
| |--foo
| |--baz
| |--bar
| |--repos
| |--qux
| |--repos_devel
| |--pkg_repo
| |--log_server_01
| |--www_02
| |--www_01
|--@vmm_iocage_06:
| |--foo
| |--baz
| |--bar
| |--repos
| |--qux
| |--test_153
| |--db1
| |--repos_devel
| |--db2
| |--pkg_repo
| |--log_server_01
| |--www_02
| |--test_152
| |--www_01
| |--test_151
|--@state_down:
| |--test_153
| |--db1
| |--db2
| |--test_152
| |--test_151
|--@nginx:
| |--www_02
| |--www_01
List jails
shell > ssh admin@iocage_06 sudo iocage list -l
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+======+===============+======+=======+======+=================+=======================+=====+==================+==========+
| None | 09caf271 | off | down | jail | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| None | 1718af82 | off | down | jail | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 1 | bar | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.143 | - | ansible-init | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 2 | baz | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.141 | - | ansible-init | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 3 | foo | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.138 | - | ansible-init | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 4 | log-server-01 | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.10/24 | - | ansible-init | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 5 | pkg-repo | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.23/24 | - | ansible-pkg-repo | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 6 | qux | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.144 | - | ansible-init | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 7 | repos | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | - | ansible-repos | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 8 | repos-devel | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | - | ansible-repos | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| None | test-151 | off | down | jail | 15.1-RELEASE-p1 | DHCP (not running) | - | ansible-client | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| None | test-152 | off | down | jail | 15.1-RELEASE-p1 | DHCP (not running) | - | ansible-client | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| None | test-153 | off | down | jail | 15.1-RELEASE-p1 | DHCP (not running) | - | ansible-client | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 25 | www-01 | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.116 | - | ansible-nginx | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 26 | www-02 | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.147 | - | ansible-nginx | no |
+------+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
Playbook pb-nginx.yml
---
- name: Configure Nginx servers.
hosts: nginx
roles:
- vbotka.freebsd.nginx
Playbook output - Configure Nginx servers
(env) > ansible-playbook -i hosts pb-nginx.yml
PLAY [Configure Nginx servers.] ************************************************
TASK [vbotka.freebsd.nginx : Conf: Configure Nginx.] ***************************
changed: [www_02] => (item=/usr/local/etc/nginx/nginx.conf)
changed: [www_01] => (item=/usr/local/etc/nginx/nginx.conf)
TASK [vbotka.freebsd.nginx : Service: Enable and Start Nginx.] *****************
changed: [www_01]
changed: [www_02]
RUNNING HANDLER [vbotka.freebsd.nginx : Enable and start nginx] ****************
changed: [www_02]
changed: [www_01]
RUNNING HANDLER [vbotka.freebsd.nginx : Reload nginx] **************************
ok: [www_02]
ok: [www_01]
PLAY RECAP *********************************************************************
www_01 : ok=4 changed=3 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0
www_02 : ok=4 changed=3 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0
Results
Test the configuration
[iocage_06]# iocage exec www-01 service nginx configtest Performing sanity check on nginx configuration: nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Test the server is running
[iocage_06]# iocage exec www-01 service nginx status nginx is running as pid 51207.
Test the server is working
[iocage_06]# lynx 172.16.99.116 It works!