021 Inventory plugin iocage2

Use case

In the inventory plugin vbotka.freebsd.iocage2, use the option inventory_hostname_tag to tell the plugin which tag to create inventory aliases from.

Tree

shell> tree .
.
├── ansible.cfg
├── group_vars
│   └── all
│       └── swarms.yml
├── hosts
│   ├── 06_iocage2.yml
│   └── 99_constructed.yml
├── host_vars
│   └── iocage_06
│       └── iocage.yml
├── iocage.ini
├── pb-test-all.yml
├── pb-test-connection.yml
└── pb-test-db.yml

Synopsis

Requirements

Notes

  • The inventory files in the directory hosts are evaluated in alphabetical order.

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
interpreter_python = auto_silent

[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

group_vars/all/swarms.yml
project: sandbox_020
properties:
  bpf: 1
  dhcp: 1
  vnet: 1

host_vars

host_vars/iocage_06/iocage.yml
names: [db1, db2]
swarms:
  db:
    count: 2
    template: ansible_client

hosts

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
cache: true
cache_plugin: ansible.builtin.jsonfile
cache_connection: /var/tmp/inventory_cache
cache_timeout: 3600
cache_prefix: iocage_06_
get_properties: true
inventory_hostname_tag: alias
hooks_results:
  - /var/db/dhclient-hook.address.epair0b

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'"
hosts/99_constructed.yml
plugin: ansible.builtin.constructed

keyed_groups:
  - prefix: project
    key: iocage_tags.project
  - prefix: swarm
    key: iocage_tags.swarm
  - prefix: state
    key: iocage_state
  - prefix: vmm
    key: iocage_tags.vmm

Jails at iocage_06

[iocage_06]# iocage list -l
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| JID |   NAME   | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |    TEMPLATE    | BASEJAIL |
+=====+==========+======+=======+======+=================+=======================+=====+================+==========+
| 11  | 828ef935 | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.133 | -   | ansible-client | no       |
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 12  | cbf2fb0c | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.134 | -   | ansible-client | no       |
+-----+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+

Jails graph at iocage_06

(env) > ansible-inventory -i hosts -i iocage.ini --graph
@all:
  |--@ungrouped:
  |--@project_sandbox_020:
  |  |--db2
  |  |--db1
  |--@swarm_db:
  |  |--db2
  |  |--db1
  |--@state_down:
  |  |--db2
  |  |--db1
  |--@vmm_iocage_06:
  |  |--db2
  |  |--db1
  |--@iocage:
  |  |--iocage_06

Playbook pb-test-all.yml

- name: Display all groups.
  hosts: all

  tasks:

    - debug:
        msg: |
          {{ iocage_properties.host_hostuuid | d('UNDEFINED') }}
          {{ iocage_tags | d({}) | to_yaml }}

    - debug:
        msg: |
          {% for group in groups %}
          {{ group }}: {{ groups[group] }}
          {% endfor %}
      run_once: true

Playbook output - Display all groups

(env) > ansible-playbook -i hosts --flush-cache pb-test-all.yml
PLAY [Display all groups.] *****************************************************

TASK [debug] *******************************************************************
ok: [db1] => 
    msg: |-
        cbf2fb0c
        {alias: db1, project: sandbox_020, swarm: db, vmm: iocage_06}
ok: [db2] => 
    msg: |-
        828ef935
        {alias: db2, project: sandbox_020, swarm: db, vmm: iocage_06}

TASK [debug] *******************************************************************
ok: [db2] => 
    msg: |-
        all: ['db2', 'db1']
        ungrouped: []
        project_sandbox_020: ['db2', 'db1']
        swarm_db: ['db2', 'db1']
        state_up: ['db2', 'db1']
        vmm_iocage_06: ['db2', 'db1']

PLAY RECAP *********************************************************************
db1                        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
db2                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Playbook pb-test-db.yml

- name: Group swarm_db
  hosts: swarm_db

  tasks:

    - debug:
        var: iocage_properties.host_hostuuid

Playbook output - Group swarm_db

(env) > ansible-playbook -i hosts pb-test-db.yml
PLAY [Group swarm_db] **********************************************************
skipping: no hosts matched

PLAY RECAP *********************************************************************

Playbook pb-test-connection.yml

- name: Test connection and get hostname.
  hosts: all

  tasks:

    - command: hostname
      register: out

    - debug:
        msg: |
          {{ inventory_hostname }} {{ out.stdout }}

Playbook output - Test connection and get hostname

(env) > ansible-playbook -i hosts pb-test-connection.yml
PLAY [Test connection and get hostname.] ***************************************

TASK [command] *****************************************************************
changed: [db1]
changed: [db2]

TASK [debug] *******************************************************************
ok: [db1] => 
    msg: |-
        db1 cbf2fb0c
ok: [db2] => 
    msg: |-
        db2 828ef935

PLAY RECAP *********************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
db2                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0