442 Connection jailexec instead of SSH

This example extends 441 Redirect SSH to jails.

Use case

Use connection vbotka.freebsd.jailexec instead of the default ansible.builtin.ssh.

Tree

shell> tree .
.
├── ansible.cfg
├── hosts
│   ├── 06_iocage2.yml
│   └── 99_constructed.yml
└── pb-test.yml

Synopsis

  • Create a dynamic inventory to connect to the jails via connection vbotka.freebsd.jailexec.

  • For all created jails, in the playbook pb-test.yml:

    • connect to the jails

    • display the basic configuration of the jails.

Requirements

Notes

  • The only difference between this example and 441 Redirect SSH to jails is the following three lines in the inventory configuration file:

ansible_connection: "'vbotka.freebsd.jailexec'"
ansible_jail_host: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)')).vmm | d('none')
ansible_jail_privilege_escalation: "'sudo'"

See also

Example 050 Connection jailexec

ansible.cfg

[defaults]
callback_result_format = yaml
deprecation_warnings = False
display_skipped_hosts = False
gathering = explicit
interpreter_python = auto_silent

[connection]
pipelining = true

hosts

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
sudo: true
get_properties: true
hooks_results:
  - /var/db/dhclient-hook.address.epair0b
compose:
  ansible_connection: "'vbotka.freebsd.jailexec'"
  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_user: "'admin'"
  ansible_python_interpreter: "'auto_silent'"
  iocage_tags: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)'))

Note

hosts/99_constructed.yml
plugin: ansible.builtin.constructed
keyed_groups:
  - prefix: swarm
    key: iocage_tags.swarm
  - prefix: vmm
    key: iocage_tags.vmm

Graph

(env) > ansible-inventory -i hosts --graph
@all:
  |--@ungrouped:
  |--@swarm_sw_01:
  |  |--0b152d76
  |  |--9b33de6e
  |  |--cb4d5365
  |--@vmm_iocage_06:
  |  |--0b152d76
  |  |--repos-devel
  |  |--9b33de6e
  |  |--pkg-repo
  |  |--cb4d5365
  |  |--log-server-01
  |  |--repos

Jails

[iocage_06]# iocage list -l
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| JID |     NAME      | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |     TEMPLATE     | BASEJAIL |
+=====+===============+======+=======+======+=================+=======================+=====+==================+==========+
| 22  | 0b152d76      | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.168 | -   | ansible-client   | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 23  | 9b33de6e      | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.172 | -   | ansible-client   | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 24  | cb4d5365      | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.146 | -   | ansible-client   | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 1   | log-server-01 | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.10/24 | -   | ansible-init     | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 2   | pkg-repo      | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.23/24 | -   | ansible-pkg-repo | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 3   | repos         | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | -   | ansible-repos    | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+
| 4   | repos-devel   | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | -   | ansible-repos    | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+------------------+----------+

Playbook pb-test.yml

- name: Test the connection plugin jailexec.
  hosts: swarm_sw_01
    
  tasks:
    
    - ansible.builtin.debug:
        msg: |
          ansible_connection: {{ ansible_connection }}
          ansible_host: {{ ansible_host }}
          ansible_user: {{ ansible_user }}

          ansible_jail_host: {{ ansible_jail_host }}
          ansible_jail_name: {{ ansible_jail_name }} 
          ansible_jail_user: {{ ansible_jail_user | d('UNDEFINED') }} 
          ansible_jail_privilege_escalation: {{ ansible_jail_privilege_escalation }}

          iocage_ip4: {{ iocage_ip4 }}
          iocage_tags: {{ iocage_tags }}

    - ansible.builtin.command: uname -a
      register: out

    - ansible.builtin.debug:
        var: out.stdout

Playbook output - Test jailexec connection plugin

(env) > ansible-playbook -i hosts pb-test.yml
PLAY [Test the connection plugin jailexec.] ************************************

TASK [ansible.builtin.debug] ***************************************************
ok: [9b33de6e] => 
    msg: |-
        ansible_connection: vbotka.freebsd.jailexec
        ansible_host: 9b33de6e
        ansible_user: admin

        ansible_jail_host: iocage_06
        ansible_jail_name: 23
        ansible_jail_user: UNDEFINED
        ansible_jail_privilege_escalation: sudo

        iocage_ip4: 172.16.99.172
        iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}
ok: [0b152d76] => 
    msg: |-
        ansible_connection: vbotka.freebsd.jailexec
        ansible_host: 0b152d76
        ansible_user: admin

        ansible_jail_host: iocage_06
        ansible_jail_name: 22
        ansible_jail_user: UNDEFINED
        ansible_jail_privilege_escalation: sudo

        iocage_ip4: 172.16.99.168
        iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}
ok: [cb4d5365] => 
    msg: |-
        ansible_connection: vbotka.freebsd.jailexec
        ansible_host: cb4d5365
        ansible_user: admin

        ansible_jail_host: iocage_06
        ansible_jail_name: 24
        ansible_jail_user: UNDEFINED
        ansible_jail_privilege_escalation: sudo

        iocage_ip4: 172.16.99.146
        iocage_tags: {'vmm': 'iocage_06', 'swarm': 'sw_01'}

TASK [ansible.builtin.command] *************************************************
changed: [0b152d76]
changed: [cb4d5365]
changed: [9b33de6e]

TASK [ansible.builtin.debug] ***************************************************
ok: [9b33de6e] => 
    out.stdout: FreeBSD 9b33de6e 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
        GENERIC amd64
ok: [cb4d5365] => 
    out.stdout: FreeBSD cb4d5365 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
        GENERIC amd64
ok: [0b152d76] => 
    out.stdout: FreeBSD 0b152d76 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf
        GENERIC amd64

PLAY RECAP *********************************************************************
0b152d76                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
9b33de6e                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
cb4d5365                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0