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-hosts.yml
│       └── project.yml
├── hosts
│   └── 06_iocage2.yml
├── iocage.ini
└── templates
    └── project-hosts.yml.j2

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

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_06

[iocage]
iocage_06

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

hosts

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
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_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'"
# 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_06
    template: ansible-init
    class: [test]
    custom_facts: class.fact.j2
    ai_vars_templates:
      - project-hosts.yml
  qux:
    vmm: iocage_06
    template: ansible-init
    class: [test]
    custom_facts: class.fact.j2
    ai_vars_templates:
      - project-hosts.yml

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'))) }}"

templates

templates/project-hosts.yml.j2
project_hosts:
  {{ project_hosts[inventory_hostname] }}

Playbook output - Create project jails from iocage templates

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

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

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

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

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

TASK [Ai-vars-templates: Create ansible init vars dirs.] ***********************
changed: [iocage_06] => (item=baz)
changed: [iocage_06] => (item=qux)

TASK [Ai-vars-templates: Create ansible init vars from templates.] *************
changed: [iocage_06] => (item=baz project-hosts.yml)
changed: [iocage_06] => (item=qux project-hosts.yml)

TASK [Ai-vars-class: Create ansible init vars dirs.] ***************************
ok: [iocage_06] => (item=baz)
ok: [iocage_06] => (item=qux)

TASK [Ai-vars-jail: Create ansible init vars dirs.] ****************************
ok: [iocage_06] => (item=baz)
ok: [iocage_06] => (item=qux)

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

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

PLAY RECAP *********************************************************************
iocage_06                  : ok=10   changed=4    unreachable=0    failed=0    skipped=19   rescued=0    ignored=0   

Inventory graph

shell > ansible-inventory -i hosts --graph
@all:
  |--@ungrouped:
  |--@state_up:
  |  |--foo
  |  |--baz
  |  |--bar
  |  |--repos
  |  |--qux
  |  |--repos_devel
  |--@vmm_iocage_06:
  |  |--foo
  |  |--baz
  |  |--bar
  |  |--repos
  |  |--qux
  |  |--repos_devel

List jails

shell > ssh admin@iocage_06 sudo iocage list -l
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| JID |    NAME     | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |   TEMPLATE    | BASEJAIL |
+=====+=============+======+=======+======+=================+=======================+=====+===============+==========+
| 15  | bar         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.143 | -   | ansible-init  | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| 16  | baz         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.141 | -   | ansible-init  | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| 14  | foo         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.138 | -   | ansible-init  | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| 17  | qux         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.144 | -   | ansible-init  | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| 7   | repos       | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | -   | ansible-repos | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+
| 8   | repos-devel | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | -   | ansible-repos | no       |
+-----+-------------+------+-------+------+-----------------+-----------------------+-----+---------------+----------+

Display the test files

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