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=baz)
ok: [iocage_06] => (item=qux)

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
  |  |--test_153
  |  |--log_server
  |  |--repos_devel
  |  |--pkg_repo
  |  |--www_02
  |  |--test_152
  |  |--www_01
  |  |--test_151
  |--@vmm_iocage_06:
  |  |--foo
  |  |--baz
  |  |--bar
  |  |--repos
  |  |--qux
  |  |--test_153
  |  |--log_server
  |  |--db1
  |  |--repos_devel
  |  |--db2
  |  |--pkg_repo
  |  |--www_02
  |  |--test_152
  |  |--www_01
  |  |--test_151
  |--@state_down:
  |  |--db1
  |  |--db2

List jails

shell > ssh admin@iocage_06 sudo iocage list -l
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| JID  |    NAME     | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |        TEMPLATE         | BASEJAIL |
+======+=============+======+=======+======+=================+=======================+=====+=========================+==========+
| None | 09caf271    | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -                       | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| None | 1718af82    | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -                       | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 37   | bar         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.143 | -   | ansible-init            | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 38   | baz         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.141 | -   | ansible-init            | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 36   | foo         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.138 | -   | ansible-init            | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 31   | log-server  | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.10/24 | -   | ansible-syslogng-server | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 21   | pkg-repo    | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.23/24 | -   | ansible-pkg-repo        | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 39   | qux         | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.144 | -   | ansible-init            | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 34   | repos       | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | -   | ansible-repos           | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 35   | repos-devel | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | -   | ansible-repos           | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 28   | test-151    | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.189 | -   | ansible-client          | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 27   | test-152    | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.188 | -   | ansible-client          | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 26   | test-153    | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.187 | -   | ansible-client          | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 23   | www-01      | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.116 | -   | ansible-init            | no       |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 24   | www-02      | on   | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.147 | -   | ansible-init            | 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!