441 Redirect SHH to jails
Use case
Create multiple jails with auto UUID names. In the inventory, compose the variables
ansible_host and ansible_port to connect the jails via the redirected SSH
ports. See the example 440 Configure DHCP and pf how pf is configured.
Tree
shell> tree .
.
├── ansible.cfg
├── group_vars
│ └── all
│ └── project-hosts.yml
├── hosts
│ ├── 06_iocage.yml
│ └── 99_constructed.yml
├── host_vars
│ └── iocage_06.yml
├── iocage.ini
└── pb-test.yml
Synopsis
At a managed node:
In the playbook vbotka.freebsd.pb_iocage_ansible_clients.yml:
create jails
start jails
optionally, stop and destroy the jails.
Create dynamic inventory to redirect SSH to the jails.
At all created jails:
In the playbook
pb-test.yml:connect to the created jails
display basic configuration of the jails.
Requirements
root privilege in the managed nodes
template
ansible_clientcreated in 202 Create iocage templates. Clone DHCP jails.
Notes
The only difference between this example and the example 442 Connection jailexec instead of ssh are the following two lines in the inventory configuration file
ansible_host: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)')).vmm
ansible_port: iocage_ip4 | split('.') | last | int - 100 + 2200
See also
example 050 Connection jailexec
Templates at iocage_06
[iocage_06]# 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-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 |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
ansible.cfg
[defaults]
callback_result_format = yaml
deprecation_warnings = false
display_skipped_hosts = false
gathering = explicit
[connection]
pipelining = true
Inventory iocage.ini
iocage_06
[iocage]
iocage_06
[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent
group_vars
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
host_vars
properties:
bpf: 1
dhcp: 1
vnet: 1
notes: "vmm={{ inventory_hostname }}"
defaultrouter: "{{ project_hosts[inventory_hostname]['defaultrouter'] }}"
swarms:
sw_01:
count: 3
template: ansible-client
Hint
If the default iocage option defaultrouter=auto doesn’t work set it. This may be
needed if the jails are provided with the DHCP on the bridge. In this case, the
defaultrouter for the jails is the IP address of the bridge. pf must provide NAT and
redirection. See example 440 Configure DHCP and pf.
Playbook output - Create and start swarms
(env) > ansible-playbook -i iocage.ini \
-t swarm \
-e swarm=true \
-e debug=true \
vbotka.freebsd.pb_iocage_ansible_clients.yml
PLAY [Create and start jails. Optionally stop and destroy jails.] **************
TASK [Get iocage facts] ********************************************************
ok: [iocage_06]
TASK [Reject jails with empty notes] *******************************************
ok: [iocage_06]
TASK [Debug cmd_create debug=true] *********************************************
ok: [iocage_06] => (item=sw_01) =>
msg: |-
iocage create --short --template ansible-client --count 3 bpf=1 dhcp=1 vnet=1 defaultrouter=172.16.99.1 notes="vmm=iocage_06 swarm=sw_01"
TASK [Create swarms] ***********************************************************
ok: [iocage_06] => (item=sw_01)
TASK [Debug create swarms debug=true] ******************************************
ok: [iocage_06] =>
out:
changed: false
failed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
changed_when_result: false
cmd:
- iocage
- create
- --short
- --template
- ansible-client
- --count
- '3'
- bpf=1
- dhcp=1
- vnet=1
- defaultrouter=172.16.99.1
- notes=vmm=iocage_06 swarm=sw_01
delta: '0:00:01.219412'
end: '2026-08-30 14:59:47.975056'
failed: false
item:
key: sw_01
value:
count: 3
template: ansible-client
msg: ''
rc: 0
start: '2026-08-30 14:59:46.755644'
stderr: ''
stderr_lines: []
stdout: |-
9b516a9a successfully created!
5b7a65b6 successfully created!
a730c895 successfully created!
stdout_lines:
- 9b516a9a successfully created!
- 5b7a65b6 successfully created!
- a730c895 successfully created!
TASK [Get iocage facts] ********************************************************
ok: [iocage_06]
TASK [Reject jails with empty notes] *******************************************
ok: [iocage_06]
TASK [Debug cmd_start debug=true] **********************************************
ok: [iocage_06] => (item=sw_01) =>
msg: |-
iocage start 5b7a65b6 a730c895 9b516a9a
TASK [Start swarms] ************************************************************
ok: [iocage_06] => (item=sw_01)
TASK [Debug start swarms debug=true] *******************************************
ok: [iocage_06] =>
out:
changed: false
failed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
changed_when_result: false
cmd:
- iocage
- start
- 5b7a65b6
- a730c895
- 9b516a9a
delta: '0:00:10.816758'
end: '2026-08-30 15:00:10.309098'
failed: false
item:
key: sw_01
value:
count: 3
template: ansible-client
msg: ''
rc: 0
start: '2026-08-30 14:59:59.492340'
stderr: |-
No default gateway found for ipv6.
No default gateway found for ipv6.
No default gateway found for ipv6.
stderr_lines:
- No default gateway found for ipv6.
- No default gateway found for ipv6.
- No default gateway found for ipv6.
stdout: |-
* Starting 5b7a65b6
+ Started OK
+ Using devfs_ruleset: 1000 (iocage generated default)
+ Configuring VNET OK
+ Using IP options: vnet
+ Starting services OK
+ Executing poststart OK
+ DHCP Address: 172.16.99.145/24
* Starting a730c895
+ Started OK
+ Using devfs_ruleset: 1001 (iocage generated default)
+ Configuring VNET OK
+ Using IP options: vnet
+ Starting services OK
+ Executing poststart OK
+ DHCP Address: 172.16.99.151/24
* Starting 9b516a9a
+ Started OK
+ Using devfs_ruleset: 1002 (iocage generated default)
+ Configuring VNET OK
+ Using IP options: vnet
+ Starting services OK
+ Executing poststart OK
+ DHCP Address: 172.16.99.152/24
stdout_lines:
- '* Starting 5b7a65b6'
- ' + Started OK'
- ' + Using devfs_ruleset: 1000 (iocage generated default)'
- ' + Configuring VNET OK'
- ' + Using IP options: vnet'
- ' + Starting services OK'
- ' + Executing poststart OK'
- ' + DHCP Address: 172.16.99.145/24'
- '* Starting a730c895'
- ' + Started OK'
- ' + Using devfs_ruleset: 1001 (iocage generated default)'
- ' + Configuring VNET OK'
- ' + Using IP options: vnet'
- ' + Starting services OK'
- ' + Executing poststart OK'
- ' + DHCP Address: 172.16.99.151/24'
- '* Starting 9b516a9a'
- ' + Started OK'
- ' + Using devfs_ruleset: 1002 (iocage generated default)'
- ' + Configuring VNET OK'
- ' + Using IP options: vnet'
- ' + Starting services OK'
- ' + Executing poststart OK'
- ' + DHCP Address: 172.16.99.152/24'
PLAY RECAP *********************************************************************
iocage_06 : ok=10 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
Jails at iocage_06
[iocage_06]# iocage list -l
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+=====+==========+======+=======+======+=================+=======================+=====+================+==========+
| 16 | 5b7a65b6 | off | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.145 | - | ansible-client | no |
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 18 | 9b516a9a | off | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.152 | - | ansible-client | no |
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 17 | a730c895 | off | up | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.151 | - | ansible-client | no |
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
Inventory hosts
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
sudo: true
get_properties: true
hooks_results:
- /var/db/dhclient-hook.address.epair0b
compose:
ansible_host: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)')).vmm | d('none')
ansible_port: iocage_ip4 | split('.') | last | int - 100 + 2200
ansible_user: "'admin'"
ansible_python_interpreter: "'auto_silent'"
iocage_tags: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)'))
Note
In the example 440 Configure DHCP and pf, the variables
ssh_rdr_start=2200anddhcp_ip_start=100are used in the playbookpb-pf-setup.ymlto calculate the ports to redirect SSH from, and to create the filepf-rdr-ssh.conf.For example, from the controller, the following command connects to the jail at <bsd_dhcpd_subnet>.106:
shell> ssh -p 2206 admin@iocage_06
See the variable
bsd_dhcpd_subnetin the example 440 Configure DHCP and pf
plugin: ansible.builtin.constructed
keyed_groups:
- prefix: swarm
key: iocage_tags.swarm
- prefix: vmm
key: iocage_tags.vmm
Display inventory
(env) > ansible-inventory -i hosts --graph
@all:
|--@ungrouped:
|--@swarm_sw_01:
| |--a730c895
| |--9b516a9a
| |--5b7a65b6
|--@vmm_iocage_06:
| |--a730c895
| |--9b516a9a
| |--5b7a65b6
Playbook pb-test.yml
- name: Test the SSH redirection to the jails.
hosts: swarm_sw_01
gather_facts: false
tasks:
- ansible.builtin.debug:
msg: |
ansible_connection: {{ ansible_connection }}
ansible_host: {{ ansible_host }}
ansible_port: {{ ansible_port }}
ansible_user: {{ ansible_user }}
iocage_ip4: {{ iocage_ip4 }}
iocage_tags: {{ iocage_tags }}
- ansible.builtin.command: uname -a
register: out
- ansible.builtin.debug:
var: out.stdout
Playbook output - Test SSH redirection
(env) > ansible-playbook pb-test.yml -i hosts
PLAY [Test the SSH redirection to the jails.] **********************************
TASK [ansible.builtin.debug] ***************************************************
ok: [a730c895] =>
msg: |-
ansible_connection: ssh
ansible_host: iocage_06
ansible_port: 2251
ansible_user: admin
iocage_ip4: 172.16.99.151
iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}
ok: [5b7a65b6] =>
msg: |-
ansible_connection: ssh
ansible_host: iocage_06
ansible_port: 2245
ansible_user: admin
iocage_ip4: 172.16.99.145
iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}
ok: [9b516a9a] =>
msg: |-
ansible_connection: ssh
ansible_host: iocage_06
ansible_port: 2252
ansible_user: admin
iocage_ip4: 172.16.99.152
iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}
TASK [ansible.builtin.command] *************************************************
changed: [a730c895]
changed: [9b516a9a]
changed: [5b7a65b6]
TASK [ansible.builtin.debug] ***************************************************
ok: [a730c895] =>
out.stdout: FreeBSD a730c895 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
GENERIC amd64
ok: [9b516a9a] =>
out.stdout: FreeBSD 9b516a9a 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
GENERIC amd64
ok: [5b7a65b6] =>
out.stdout: FreeBSD 5b7a65b6 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
GENERIC amd64
PLAY RECAP *********************************************************************
5b7a65b6 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
9b516a9a : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
a730c895 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Hint
The below play stops and destroys the jails in swarms
ansible-playbook -i iocage.ini \
-t swarm_destroy \
-e swarm_destroy=true \
vbotka.freebsd.pb_iocage_ansible_clients.yml