525 iocage template ansible-init (class)

Use case

Use the iocage template ansible-init created in 524 iocage template ansible-init. Configure the repo ansible-conf-init to pull the jails’ configuration from the repo ansible-conf-test. Create jails from the template. Use class=test to select the configuration. Run ansible-pull asynchronously.

Tree

shell > tree .
.
├── ansible.cfg
├── group_vars
│   └── all
│       └── project.yml
├── hosts
│   └── 05_iocage.yml
└── iocage.ini

Synopsis

Note

The only difference compared to 524 iocage template ansible-init is the dictionary used for the jails’ configuration. In this example, the jails (baz and qux) are not included in the ai_db_host dictionary; instead, they are configured from the ai_db_class dictionary. See the ansible-conf-init repository.

Requirements

Note

See also

GitHub repositories:

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_05

[iocage]
iocage_05

[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent

hosts

hosts/05_iocage.yml
plugin: vbotka.freebsd.iocage
host: iocage_05
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_jail_host: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)')).vmm | d('none')
  ansible_jail_name: iocage_jid
  ansible_jail_privilege_escalation: "'sudo'"
# ansible options
  ansible_python_interpreter: "'auto_silent'"

groups:
  pull_init: iocage_classes is contains('init')

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

group_vars

group_vars/all/project.yml
project:
  baz:
    vmm: iocage_05
    template: ansible-init
    class: [test]
    custom_facts: class.fact.j2
  qux:
    vmm: iocage_05
    template: ansible-init
    class: [test]
    custom_facts: class.fact.j2

properties:
  ip4: disable
  ip6: disable
  ip6_addr: none
  bpf: 1
  dhcp: 1
  vnet: 1
  boot: 1

vmm_groups: "{{ dict(project | dict2items | groupby('value.vmm')) }}"
vmm: "{{ dict(vmm_groups.keys() | zip(vmm_groups.values() | map('items2dict'))) }}"

Playbook output - Create project jails from iocage templates

(env) > ansible-playbook vbotka.freebsd.pb_iocage_project_create_from_templates.yml -i iocage.ini -i hosts
PLAY [Create and start project jails from iocage templates.] *******************

TASK [Setup: Get activated pool.] **********************************************
ok: [iocage_05]

TASK [Create: Create jails.] ***************************************************
ok: [iocage_05] => (item=qux)
ok: [iocage_05] => (item=baz)

TASK [Facts: Create custom facts dirs.] ****************************************
changed: [iocage_05] => (item=baz)
changed: [iocage_05] => (item=qux)

TASK [Facts: Create custom facts files.] ***************************************
changed: [iocage_05] => (item=baz)
changed: [iocage_05] => (item=qux)

TASK [Properties: Set properties.] *********************************************
ok: [iocage_05] => (item=baz)
ok: [iocage_05] => (item=qux)

TASK [Start: Start jails.] *****************************************************
ok: [iocage_05]

PLAY RECAP *********************************************************************
iocage_05                  : ok=6    changed=2    unreachable=0    failed=0    skipped=7    rescued=0    ignored=0   

Inventory graph

shell > ansible-inventory -i hosts --graph
@all:
  |--@ungrouped:
  |--@state_down:
  |  |--ansible-pull-syslogng-client
  |  |--ansible-pull-syslogng-server
  |  |--ansible-pull-test
  |--@state_up:
  |  |--bar
  |  |--baz
  |  |--foo
  |  |--qux
  |  |--repos
  |  |--repos_devel
  |--@vmm_iocage_05:
  |  |--bar
  |  |--baz
  |  |--foo
  |  |--qux
  |  |--repos
  |  |--repos_devel

List jails

shell > ssh admin@iocage_05 sudo iocage list -l
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| JID  |             NAME             | BOOT | STATE |   TYPE   |   RELEASE    |          IP4          | IP6 |   TEMPLATE    | BASEJAIL |
+======+==============================+======+=======+==========+==============+=======================+=====+===============+==========+
| None | ansible-pull-syslogng-client | off  | down  | pluginv2 | 15.0-RELEASE | DHCP (not running)    | -   | -             | yes      |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| None | ansible-pull-syslogng-server | off  | down  | pluginv2 | 15.0-RELEASE | vnet0|172.16.99.31/24 | -   | -             | yes      |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| None | ansible-pull-test            | off  | down  | pluginv2 | 15.0-RELEASE | DHCP (not running)    | -   | -             | yes      |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 13   | bar                          | on   | up    | jail     | 15.0-RELEASE | epair0b|172.16.99.103 | -   | ansible-init  | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 14   | baz                          | on   | up    | jail     | 15.0-RELEASE | epair0b|172.16.99.172 | -   | ansible-init  | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 12   | foo                          | on   | up    | jail     | 15.0-RELEASE | epair0b|172.16.99.196 | -   | ansible-init  | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 15   | qux                          | on   | up    | jail     | 15.0-RELEASE | epair0b|172.16.99.104 | -   | ansible-init  | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 6    | repos                        | on   | up    | jail     | 15.0-RELEASE | vnet0|172.16.99.21/24 | -   | ansible-repos | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+
| 7    | repos-devel                  | on   | up    | jail     | 15.0-RELEASE | vnet0|172.16.99.22/24 | -   | ansible-repos | no       |
+------+------------------------------+------+-------+----------+--------------+-----------------------+-----+---------------+----------+

Display the test files

shell > ssh admin@iocage_05 sudo iocage exec baz "cat /tmp/ansible-hello-world.txt"
[ansible-test] Hello world!
shell > ssh admin@iocage_05 sudo iocage exec qux "cat /tmp/ansible-hello-world.txt"
[ansible-test] Hello world!