527 iocage template ansible-pkg-repo
Use case
Create a jail that serves pkg repo for other jails. Create iocage template
ansible-pkg-repo and configure web server to publish the repo. Create a jail
from the template and fetch packages to the repo.
Tree
shell > tree .
.
├── ansible.cfg
├── group_vars
│ ├── all
│ │ ├── project-hosts.yml
│ │ ├── project.yml
│ │ └── template.yml
│ └── fetch_pkg_repo
│ └── pkg-repo.yml
├── hosts
│ └── 06_iocage2.yml
├── host_vars
│ └── iocage_06
│ ├── local-pkg-conf.yml
│ ├── nginx-pkg-repo.yml
│ └── template.yml
├── iocage.ini
├── pb-iocage-template.yml
├── pb-pkg-repo.yml
└── templates
├── local.conf.j2
└── nginx-pkg-repo.conf.j2
Synopsis
At a managed node:
Use the role vbotka.freebsd.iocage_template to create the template
ansible-pkg-repoIn the playbook vbotka.freebsd.pb_iocage_project_create_from_templates.yml create a jail from the template.
In the inventory group
fetch_pkg_repofetch the selected packages to the repo.
Requirements
playbook vbotka.freebsd.pb_iocage_project_create_from_templates.yml
package repository created in 322 Create local package repository
See also
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
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:
fetch_pkg_repo: iocage_classes is contains('pkg-repo')
keyed_groups:
- prefix: state
key: iocage_state
- prefix: vmm
key: iocage_tags.vmm
group_vars
project_hosts:
iocage_06:
defaultrouter: 172.16.99.1
default_pkg_repo: 172.16.99.1
local_pkg_repo: 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
ansible_pull_syslogng_server: 172.16.99.31
project:
pkg-repo:
notes: alias=pkg_repo
vmm: iocage_06
template: ansible-pkg-repo
class: [pkg-repo]
properties:
ip4: disable
ip6: disable
ip6_addr: none
boot: 1
defaultrouter: "{{ project_hosts[inventory_hostname]['defaultrouter'] }}"
ip4_addr: "vnet0|{{ project_hosts[inventory_hostname]['pkg_repo'] }}/24"
vnet: 1
vmm: "{{ (project | vbotka.freebsd.project).vmm }}"
fit_pkg_install: true
fit_pkg_pkgsite: local
fit_pkg_cached: true
fit_pkg_conf:
/usr/local/etc/pkg/repos:
- {src: local.conf.j2, dest: local.conf}
fit_dir_options:
/usr/local/www/nginx:
owner: root
group: www
mode: '0755'
/usr/local/repo:
owner: root
group: wheel
mode: '0755'
/usr/local/etc/pkg/repos:
owner: root
group: wheel
mode: '0755'
fit_file_options:
/usr/local/www/nginx/index.yml:
owner: root
group: www
mode: '0644'
/usr/local/etc/nginx/nginx-pkg-repo.conf:
owner: root
group: wheel
mode: '0644'
/root/.profile:
assignment_operator: '='
owner: root
group: wheel
mode: '0644'
/usr/local/etc/pkg/repos/local.conf:
owner: root
group: wheel
mode: '0644'
pkg_fetch: true
pkg_fetch_dir: /usr/local/www/pkgrepo
pkg_fetch_list: []
pkg_fetch_dict_select:
- minimal
- syslog
pkg_dict:
- pkglist: minimal
packages:
- shells/bash
- devel/git@default
- archivers/gtar
- ports-mgmt/pkg
- ports-mgmt/portmaster
- ports-mgmt/portupgrade
- net/rsync
- security/sudo
- ftp/wget
- pkglist: syslog
packages:
- sysutils/syslog-ng
- sysutils/lnav
host_vars
local_pkg_conf:
local:
url: "http://{{ project_hosts[inventory_hostname]['default_pkg_repo'] }}"
enabled: true
signature_type: "none"
nginx_pkg_repo_conf:
worker_processes: ['1']
events:
worker_connections: ['256']
use: ['kqueue']
http:
include: ['mime.types']
default_type: ['application/octet-stream']
sendfile: ['on']
tcp_nopush: ['on']
tcp_nodelay: ['on']
keepalive_timeout: ['15']
server:
listen: ['80']
server_name: ['{{ project_hosts[inventory_hostname]["pkg_repo"] }}']
location /:
root: ['/usr/local/www/pkgrepo']
autoindex: ['on']
Important
This configuration is minimal and functional for an isolated lab or trusted internal LAN, but it poses several security risks in production or shared network environments.
fit_templates:
ansible-pkg-repo:
release: 15.1-RELEASE
pkg_conf: "{{ fit_pkg_conf | dict2items }}"
pkg:
- nginx-lite
- python312
- python
- python3
templates: "{{ fit_file_templates | dict2items }}"
commands: "{{ fit_commands }}"
rcconf: "{{ fit_rcconf | dict2items }}"
properties:
ip4: disable
ip6: disable
ip6_addr: none
bpf: 1
dhcp: 1
vnet: 1
notes: '"class=pkg-repo alias=ansible_pkg_repo"'
fit_rcconf:
nginx_enable: "YES"
fit_commands:
- mkdir -p /usr/local/www/pkgrepo
- chown www:www /usr/local/www/pkgrepo
- chmod 755 /usr/local/www/pkgrepo
fit_file_templates:
/usr/local/etc/nginx:
- {src: nginx-pkg-repo.conf.j2, dest: nginx.conf}
templates
# {{ ansible_managed }}
{{ local_pkg_conf | vbotka.freebsd.to_ucl }}
# {{ ansible_managed }}
{{ nginx_pkg_repo_conf | vbotka.freebsd.dict_to_ast | vbotka.freebsd.ast_to_nginx }}
Playbook pb-iocage-template.yml
---
- name: Create iocage templates.
hosts: iocage
gather_facts: true
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 [Gathering Facts] *********************************************************
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Setup: Get iocage list of templates.] ***
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Setup: All templates already created.] ***
ok: [iocage_06] =>
msg: All templates already created. End role.
PLAY RECAP *********************************************************************
iocage_06 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 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-init | 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 |
+------+------------------+------+-------+----------+-----------------+--------------------+-----+----------+----------+
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=pkg-repo)
TASK [Ai-vars-class: Create ansible init vars dirs.] ***************************
changed: [iocage_06] => (item=pkg-repo)
TASK [Ai-vars-jail: Create ansible init vars dirs.] ****************************
ok: [iocage_06] => (item=pkg-repo)
TASK [Properties: Set properties.] *********************************************
ok: [iocage_06] => (item=pkg-repo)
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
| |--bar
| |--repos
| |--repos_devel
| |--pkg_repo
| |--log_server_01
| |--www_02
| |--www_01
|--@vmm_iocage_06:
| |--foo
| |--bar
| |--repos
| |--repos_devel
| |--pkg_repo
| |--log_server_01
| |--www_02
| |--www_01
|--@fetch_pkg_repo:
| |--pkg_repo
List jails
shell > ssh admin@iocage_06 sudo iocage list -l
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+=====+===============+======+=======+======+=================+=======================+=====+==================+==========+
| 9 | bar | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.143 | - | ansible-init | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 8 | foo | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.160 | - | ansible-init | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 10 | log-server-01 | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.10/24 | - | ansible-init | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 13 | pkg-repo | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.23/24 | - | ansible-pkg-repo | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 6 | repos | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | - | ansible-repos | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 7 | repos-devel | on | up | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | - | ansible-repos | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 11 | www-01 | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.159 | - | ansible-init | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 12 | www-02 | on | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.147 | - | ansible-init | no |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
Playbook pb-pkg-repo.yml
---
- name: Fetch packages to repo.
hosts: fetch_pkg_repo
tasks:
- name: Fetch packages to repo.
ansible.builtin.import_role:
name: vbotka.freebsd.packages
tasks_from: pkg-fetch.yml
Playbook output - Fetch packages to repo
(env) > ansible-playbook -i hosts pb-pkg-repo.yml
PLAY [Fetch packages to repo.] *************************************************
TASK [vbotka.freebsd.packages : Packages_fetch: Create /usr/local/www/pkgrepo] ***
changed: [pkg_repo]
TASK [vbotka.freebsd.packages : Packages_fetch: Fetch selected lists from pkg_dict] ***
changed: [pkg_repo] => (item=minimal)
changed: [pkg_repo] => (item=syslog)
RUNNING HANDLER [vbotka.freebsd.packages : Generate repo metadata] *************
ok: [pkg_repo]
PLAY RECAP *********************************************************************
pkg_repo : ok=3 changed=2 unreachable=0 failed=0 skipped=8 rescued=0 ignored=0
List repo
shell > ssh admin@iocage_06 fetch -qo - http://172.16.99.23/
<html>
<head><title>Index of /</title></head>
<body>
<h1>Index of /</h1><hr><pre><a href="../">../</a>
<a href="All/">All/</a> 04-Sep-2026 11:57 -
<a href="data.pkg">data.pkg</a> 04-Sep-2026 11:59 58743
<a href="meta">meta</a> 04-Sep-2026 11:59 179
<a href="meta.conf">meta.conf</a> 04-Sep-2026 11:59 179
<a href="packagesite.pkg">packagesite.pkg</a> 04-Sep-2026 11:59 58720
</pre><hr></body>
</html>