420 Role vbotka.freebsd.apache

Use case

Use iocage property host_hostname to create a jail. Use the role vbotka.freebsd.apache to configure Apache HTTP Server.

Tree

shell> tree .
.
├── ansible.cfg
├── hosts
├── host_vars
│   ├── iocage_04
│   │   └── ansible-client-apache.yml
│   └── www-1
│       └── apache.yml
├── iocage.ini
└── pb-apache.yml

Synopsis

Requirements

Notes

  • iocage property host_hostname provides “The hostname of the jail.”. Please note that iocage option --name provides “NAME instead of a UUID for the new jail”.

  • In case of DHCP, host_hostname resolves, however --name does not.

ansible.cfg

[defaults]
gathering = explicit
callback_result_format = yaml
display_skipped_hosts = false

[connection]
pipelining = true

Inventory iocage.ini

iocage_04 ansible_host=10.1.0.29

[iocage]
iocage_04

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

host_vars

host_vars/iocage_04/ansible-client-apache.yml
properties:
  notes: "vmm={{ inventory_hostname }}"
  bpf: 1
  dhcp: 1
  vnet: 1

clones_host_hostname:
  www-1:
    template: ansible_client_apache
host_vars/www-1/apache.yml
apache_install: false
apache_confd: false
apache_servername: www-1
apache_serveradmin: admin@www-1

apache_directory_blocks:
  - Directory: /usr/local/www/apache24/data/
    Includefile: usr-local-www-apache-data.conf
    Conf:
      - "DirectoryIndex index.html index.htm"
      - "Options Indexes FollowSymLinks"
      - "AllowOverride All"
      - "Require all granted"

Create and start jails

(env) > ansible-playbook vbotka.freebsd.pb_iocage_ansible_clients.yml \
                         -i iocage.ini \
                         -t clone_host_hostname -e clone_host_hostname=true \
                         -e debug=true -e debug2=true
PLAY [Create and start jails. Optionally stop and destroy jails.] **************

TASK [Debug cmd_create debug=true] *********************************************
ok: [iocage_04] => (item=www-1 ansible_client_apache) => 
    msg: |-
        iocage create --short --template ansible_client_apache notes="vmm=iocage_04" bpf=1 dhcp=1 vnet=1 host_hostname=www-1

TASK [Create clones host_hostname] *********************************************
changed: [iocage_04] => (item=www-1 ansible_client_apache)

TASK [Debug create clones host_hostname debug2=true] ***************************
ok: [iocage_04] => 
    out_create:
        changed: true
        msg: All items completed
        results:
        -   ansible_facts:
                discovered_interpreter_python: /usr/local/bin/python3.11
            ansible_loop_var: item
            changed: true
            cmd:
            - iocage
            - create
            - --short
            - --template
            - ansible_client_apache
            - notes=vmm=iocage_04
            - bpf=1
            - dhcp=1
            - vnet=1
            - host_hostname=www-1
            delta: '0:00:00.642988'
            end: '2025-08-18 16:39:15.496648'
            failed: false
            invocation:
                module_args:
                    _raw_params: |-
                        iocage create --short --template ansible_client_apache notes="vmm=iocage_04" bpf=1 dhcp=1 vnet=1 host_hostname=www-1
                    _uses_shell: false
                    argv: null
                    chdir: null
                    creates: null
                    executable: null
                    expand_argument_vars: true
                    removes: null
                    stdin: null
                    stdin_add_newline: true
                    strip_empty_ends: true
            item:
                key: www-1
                value:
                    template: ansible_client_apache
            msg: ''
            rc: 0
            start: '2025-08-18 16:39:14.853660'
            stderr: ''
            stderr_lines: []
            stdout: 68af99e0 successfully created!
            stdout_lines:
            - 68af99e0 successfully created!
        skipped: false

TASK [Debug cmd_start debug=true] **********************************************
ok: [iocage_04] => 
    msg: |-
        iocage start 68af99e0

TASK [Start clones host_hostname] **********************************************
changed: [iocage_04]

TASK [Debug start clones host_hostname debug2=true] ****************************
ok: [iocage_04] => 
    out_start:
        changed: true
        cmd:
        - iocage
        - start
        - 68af99e0
        delta: '0:00:13.174462'
        end: '2025-08-18 16:39:28.959602'
        failed: false
        msg: ''
        rc: 0
        start: '2025-08-18 16:39:15.785140'
        stderr: No default gateway found for ipv6.
        stderr_lines:
        - No default gateway found for ipv6.
        stdout: |-
            * Starting 68af99e0
              + Started OK
              + Using devfs_ruleset: 1010 (iocage generated default)
              + Configuring VNET OK
              + Using IP options: vnet
              + Starting services OK
              + Executing poststart OK
              + DHCP Address: 10.1.0.157/24
        stdout_lines:
        - '* Starting 68af99e0'
        - '  + Started OK'
        - '  + Using devfs_ruleset: 1010 (iocage generated default)'
        - '  + Configuring VNET OK'
        - '  + Using IP options: vnet'
        - '  + Starting services OK'
        - '  + Executing poststart OK'
        - '  + DHCP Address: 10.1.0.157/24'

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

Inventory hosts

hosts
www-1

[www]
www-1

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

Playbook pb-apache.yml

- name: Create Apache HTTP Server.
  hosts: www
  gather_facts: true

  roles:
    - vbotka.freebsd.apache

Playbook output - Create server

(env) > ansible-playbook pb-apache.yml -i hosts
PLAY [Create Apache HTTP Server.] **********************************************

TASK [Gathering Facts] *********************************************************
ok: [www-1]

TASK [Vars: Include OS vars.] **************************************************
included: vbotka.freebsd.lib for www-1

TASK [vbotka.freebsd.lib : Al_include_os_vars_path: Vars from /scratch/collections/ansible_collections/vbotka/freebsd/roles/apache/vars/defaults] ***
ok: [www-1]

TASK [vbotka.freebsd.apache : Httpd-conf: Configure parameters in /usr/local/etc/apache24/httpd.conf] ***
changed: [www-1] => (item={'regexp': 'ServerName', 'line': 'www-1'})
changed: [www-1] => (item={'regexp': 'ServerAdmin', 'line': 'admin@www-1'})
changed: [www-1] => (item={'regexp': 'ServerTokens', 'line': 'Prod'})

TASK [vbotka.freebsd.apache : Httpd-dirs: Configure directories in /usr/local/etc/apache24/Includes/] ***
changed: [www-1] => (item=/usr/local/www/apache24/data/)

TASK [vbotka.freebsd.apache : Rc-conf: Enable and Start Apache] ****************
changed: [www-1]

RUNNING HANDLER [vbotka.freebsd.apache : Enable and start apache] **************
changed: [www-1]

RUNNING HANDLER [vbotka.freebsd.apache : Reload apache] ************************
ok: [www-1]

PLAY RECAP *********************************************************************
www-1                      : ok=8    changed=4    unreachable=0    failed=0    skipped=34   rescued=0    ignored=0   

Results

  • Test the configuration

    (env) > ssh admin@www-1 sudo service apache24 configtest
    Performing sanity check on apache24 configuration:
    Syntax OK
    
  • In a browser, open the page http://www-1/. The content should be

    It works!