442 Connection jailexec instead of ssh

Extending example 441 Redirect SHH to jails.

Use case

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

Tree

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

Synopsis

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

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

    • connect to the jails

    • display basic configuration of the jails.

Requirements

Notes

The only difference between this example and the example 441 Redirect SHH to jails are 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

[connection]
pipelining = true

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

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
sudo: true
get_properties: true
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

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 connection plugin jailexec.
  hosts: swarm_sw_01
  gather_facts: false
    
  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_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 pb-test.yml -i hosts
PLAY [Test the connection plugin jailexec.] ************************************

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

        ansible_jail_host: iocage_06
        ansible_jail_privilege_escalation: sudo

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

        ansible_jail_host: iocage_06
        ansible_jail_privilege_escalation: sudo

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

        ansible_jail_host: iocage_06
        ansible_jail_privilege_escalation: sudo

        iocage_ip4: -
        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