524 iocage template ansible-init
Use case
Create iocage template ansible-init. Configure firstboot service
ansible_init that runs ansible-pull and uses the repo
ansible-conf-init. Configure the repo ansible-conf-init to pull the jails’
configuration from the repo ansible-conf-test. Create jails from the
template. Use the hostname to select the configuration. Run ansible-pull
asynchronously.
Tree
shell > tree .
.
├── ansible.cfg
├── files
│ ├── pkgs-15_0.json
│ └── pkgs-15_1.json
├── group_vars
│ └── all
│ ├── project-hosts.yml
│ └── project.yml
├── hosts
│ └── 06_iocage2.yml
├── host_vars
│ └── iocage_06
│ └── template.yml
├── iocage.ini
├── pb-iocage-template.yml
└── templates
└── project-hosts.yml.j2
Synopsis
At a managed node:
Use the role vbotka.freebsd.iocage_template to create the template
ansible-initIn the playbook vbotka.freebsd.pb_iocage_project_create_from_templates.yml create jails from the template.
Wait for ansible-pull to configure the jails and display the test files.
Requirements
Note
See the option
firstboot_sentinelin man rc.conf
See also
The example 523 iocage template ansible-repos
The
Troubleshootingsection in 526 Log server and clients (ansible_init)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_06
[iocage]
iocage_06
[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent
hosts
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
project_hosts:
iocage_05:
defaultrouter: 172.16.99.1
log_server: 172.16.99.10
# repositories
repos: 172.16.99.21
repos_devel: 172.16.99.22
# plugins
ansible_pull_syslogng_server: 172.16.99.31
iocage_06:
defaultrouter: 172.16.99.1
log_server: 172.16.99.10
# repositories
repos: 172.16.99.21
repos_devel: 172.16.99.22
# plugins
ansible_pull_syslogng_server: 172.16.99.31
project:
foo:
vmm: iocage_06
template: ansible-init
class: [test]
ai_vars_templates:
- project-hosts.yml
bar:
vmm: iocage_06
template: ansible-init
class: [test]
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'))) }}"
host_vars
fit_templates:
ansible-init:
release: 15.1-RELEASE
pkglist: /tmp/ansible/ansible_init/pkgs-15_1.json
cron: "{{ fit_cron | dict2items }}"
file_lines: "{{ fit_file_lines | dict2items }}"
firstboot: "{{ fit_firstboot | dict2items }}"
rcconf: "{{ fit_rcconf | dict2items }}"
properties:
ip4: disable
ip6: disable
ip6_addr: none
bpf: 1
dhcp: 1
vnet: 1
notes: '"class=init alias=ansible_init"'
fit_firstboot:
ansible_init:
content: |
#!/bin/sh
#
# PROVIDE: ansible_init
# REQUIRE: FILESYSTEMS NETWORKING
# KEYWORD: firstboot
. /etc/rc.subr
name="ansible_init"
desc="Firstboot ansible-pull initialization"
rcvar="ansible_init_enable"
# Default configuration settings
: ${ansible_init_enable:="NO"}
: ${ansible_init_host:="http://localhost"}
: ${ansible_init_repo:="ansible-conf-init"}
: ${ansible_init_dest:="/root"}
: ${ansible_init_vars:="/root/ansible-vars"}
: ${ansible_init_playbook:="pb-init.yml"}
# Define the command to run
command="/usr/local/bin/ansible-pull"
# Pass the environment variables
ansible_init_env="\
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin \
LANG=en_US.UTF-8 \
LC_ALL=en_US.UTF-8"
# Construct the arguments
load_rc_config $name
command_args="\
-i hosts \
-U ${ansible_init_host}/${ansible_init_repo} \
-d ${ansible_init_dest}/${ansible_init_repo} \
-e 'ai_vars=${ansible_init_vars}' \
-e 'ai_pull_mode=true' \
${ansible_init_playbook}"
run_rc_command "$1"
fit_rcconf:
ansible_init_enable: "YES"
ansible_init_host: "git://{{ project_hosts[inventory_hostname]['repos'] }}"
fit_cron:
/etc/cron.d/at:
- name: Run atrun execution daemon in 1 minute intervals.
minute: '*/1'
hour: '*'
day: '*'
month: '*'
weekday: '*'
user: root
job: /usr/libexec/atrun
exclusive: true
fit_file_lines:
/root/.profile:
LANG: en_US.UTF-8
LC_ALL: en_US.UTF-8
fit_file_options:
/root/.profile:
assignment_operator: '='
owner: root
group: wheel
mode: '0644'
templates
project_hosts:
{{ project_hosts[inventory_hostname] }}
files
{
"pkgs": [
"py312-ansible",
"git"
]
}
Playbook pb-iocage-template.yml
---
- name: Create iocage templates.
hosts: iocage
roles:
- vbotka.freebsd.iocage_template
Playbook output - Create iocage templates
(env) > ansible-playbook -i iocage.ini pb-iocage-template.yml
PLAY [Create iocage templates.] ************************************************
TASK [vbotka.freebsd.iocage_template : Setup: Get iocage list of templates.] ***
ok: [iocage_06]
TASK [vbotka.freebsd.iocage_template : Setup: All templates already created.] ***
ok: [iocage_06] =>
msg: All templates already created. End host.
PLAY RECAP *********************************************************************
iocage_06 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
List templates
shell > ssh admin@iocage_06 sudo 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-client-apache | off | down | template | 15.1-RELEASE-p1 | DHCP (not running) | - | - | no |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-client-pull | 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 |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
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=bar)
ok: [iocage_06] => (item=foo)
TASK [Ai-vars-templates: Create ansible init vars dirs.] ***********************
changed: [iocage_06] => (item=foo)
changed: [iocage_06] => (item=bar)
TASK [Ai-vars-templates: Create ansible init vars from templates.] *************
changed: [iocage_06] => (item=foo project-hosts.yml)
changed: [iocage_06] => (item=bar project-hosts.yml)
TASK [Ai-vars-class: Create ansible init vars dirs.] ***************************
ok: [iocage_06] => (item=foo)
ok: [iocage_06] => (item=bar)
TASK [Ai-vars-jail: Create ansible init vars dirs.] ****************************
ok: [iocage_06] => (item=foo)
ok: [iocage_06] => (item=bar)
TASK [Properties: Set properties.] *********************************************
ok: [iocage_06] => (item=foo)
ok: [iocage_06] => (item=bar)
TASK [Start: Start jails.] *****************************************************
ok: [iocage_06]
PLAY RECAP *********************************************************************
iocage_06 : ok=8 changed=2 unreachable=0 failed=0 skipped=21 rescued=0 ignored=0
Inventory graph
shell > ansible-inventory -i hosts --graph
@all:
|--@ungrouped:
|--@state_up:
| |--foo
| |--bar
| |--repos
| |--test_153
| |--log_server
| |--repos_devel
| |--pkg_repo
| |--www_02
| |--test_152
| |--www_01
| |--test_151
|--@vmm_iocage_06:
| |--foo
| |--bar
| |--repos
| |--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 |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 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 |
+------+-------------+------+-------+------+-----------------+-----------------------+-----+-------------------------+----------+
| 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 foo "cat /tmp/ansible-hello-world.txt"
[ansible-test] Hello world!
shell > ssh admin@iocage_06 sudo iocage exec bar "cat /tmp/ansible-hello-world.txt"
[ansible-test] Hello world!