529 iocage template ansible-init (local repo)

Use case

Create iocage template ansible-init. Enable firstboot service ansible_init that runs ansible-pull from the repositories on project_hosts.repos.

Tree

shell > tree .
.
├── ansible.cfg
├── files
│   └── ansible-init.sh
├── group_vars
│   └── all
│       ├── project-hosts.yml
│       └── template.yml
├── host_vars
│   └── iocage_06
│       ├── local-pkg-conf.yml
│       └── template.yml
├── iocage.ini
├── pb-iocage-template.yml
└── templates
    └── local.conf.j2

Synopsis

Requirements

Note

TBD

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

group_vars

group_vars/all/project-hosts.yml
project_hosts:
  iocage_06:
    defaultrouter: 172.16.99.1
    default_pkg_repo: 172.16.99.1
    local_pkg_repo: 172.16.99.1
    log_server: 172.16.99.10
    repos: 172.16.99.21
    repos_devel: 172.16.99.22
    pkg_repo: 172.16.99.23
    ansible_pull_syslogng_server: 172.16.99.31
group_vars/all/template.yml
fit_pkg_install: true
fit_pkg_pkgsite: local
fit_pkg_cached: true

fit_pkg_conf:
  /usr/local/etc/pkg/repos:
    - {src: local.conf.j2, dest: local.conf}

fit_dir_options:
  /usr/local/www/nginx:
    owner: root
    group: www
    mode: '0755'
  /usr/local/repo:
    owner: root
    group: wheel
    mode: '0755'
  /usr/local/etc/pkg/repos:
    owner: root
    group: wheel
    mode: '0755'

fit_file_options:
  /usr/local/www/nginx/index.yml:
    owner: root
    group: www
    mode: '0644'
  /usr/local/etc/nginx/nginx-pkg-repo.conf:
    owner: root
    group: wheel
    mode: '0644'
  /root/.profile:
    assignment_operator: '='
    owner: root
    group: wheel
    mode: '0644'
  /usr/local/etc/pkg/repos/local.conf:
    owner: root
    group: wheel
    mode: '0644'

host_vars

host_vars/iocage_06/local-pkg-conf.yml
local_pkg_conf:
  local:
    url: "http://{{ project_hosts[inventory_hostname]['default_pkg_repo'] }}"
    enabled: true
    signature_type: "none"
host_vars/iocage_06/template.yml
fit_templates:
  ansible-init:
    release: 15.1-RELEASE
    pkg_conf: "{{ fit_pkg_conf | dict2items }}"
    pkg:
      - git
      - py312-ansible
    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: "{{ lookup('ansible.builtin.file', 'files/ansible-init.sh', rstrip=False) }}"

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

files

files/ansible-init.sh
#!/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"

templates

templates/local.conf.j2
# {{ ansible_managed }}
{{ local_pkg_conf | vbotka.freebsd.to_ucl }}

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: Get activated pool.] *************
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Create: Get iocage list of jails.] ******
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Create: Create jails.] ******************
ok: [iocage_06] => (item=ansible-init 15.1-RELEASE)

TASK [vbotka.freebsd.iocage_template : Start: Get iocage list of jails.] *******
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Start: Start jails.] ********************
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Pkg: Get iocage list of jails.] *********
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Pkg: Configure repos.] ******************
included: /scratch/collections/ansible_collections/vbotka/freebsd/roles/iocage_template/tasks/fn/templates.yml for iocage_06 => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Fn/templates: Create directory.] ********
changed: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Fn/templates: Template files.] **********
changed: [iocage_06] => (item={'src': 'local.conf.j2', 'dest': 'local.conf'})

TASK [vbotka.freebsd.iocage_template : Pkg: Update repos.] *********************
included: /scratch/collections/ansible_collections/vbotka/freebsd/roles/iocage_template/tasks/fn/pkg.yml for iocage_06 => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Fn/pkg: Update repos.] ******************
ok: [iocage_06] => (item=local)

TASK [vbotka.freebsd.iocage_template : Pkg: Install packages.] *****************
changed: [iocage_06] => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Lines: Configure lines in files.] *******
included: /scratch/collections/ansible_collections/vbotka/freebsd/roles/iocage_template/tasks/fn/file_lines.yml for iocage_06 => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Fn/file_lines: Configure lines in files.] ***
changed: [iocage_06] => (item=ansible-init /root/.profile LANG=en_US.UTF-8)
changed: [iocage_06] => (item=ansible-init /root/.profile LC_ALL=en_US.UTF-8)

TASK [vbotka.freebsd.iocage_template : Firstboot: Create scripts.] *************
changed: [iocage_06] => (item=ansible-init ansible_init)

TASK [vbotka.freebsd.iocage_template : Firstboot: Touch /firstboot.] ***********
changed: [iocage_06] => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Cron: Configure cron files.] ************
included: /scratch/collections/ansible_collections/vbotka/freebsd/roles/iocage_template/tasks/fn/cron.yml for iocage_06 => (item=ansible-init)

TASK [vbotka.freebsd.iocage_template : Fn/cron: Remove exclusive jobs from cron files.] ***
changed: [iocage_06] => (item=ansible-init /usr/libexec/atrun)

TASK [vbotka.freebsd.iocage_template : Fn/cron: Configure cron files.] *********
changed: [iocage_06] => (item=ansible-init Run atrun execution daemon in 1 minute intervals.)

TASK [vbotka.freebsd.iocage_template : Rcconf: Configure /etc/rc.conf] *********
changed: [iocage_06] => (item=ansible-init ansible_init_enable YES)
changed: [iocage_06] => (item=ansible-init ansible_init_host git://172.16.99.21)

TASK [vbotka.freebsd.iocage_template : Stop: Get iocage list of jails.] ********
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Stop: Stop jails.] **********************
ok: [iocage_06]

TASK [vbotka.freebsd.iocage_template : Template: Set template.] ****************
ok: [iocage_06] => (item=ansible-init)

PLAY RECAP *********************************************************************
iocage_06                  : ok=24   changed=9    unreachable=0    failed=0    skipped=40   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-init     | 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       |
+------+------------------+------+-------+----------+-----------------+--------------------+-----+----------+----------+