420 Role vbotka.freebsd.apache

Use case

Use the role vbotka.freebsd.apache to configure Apache HTTP Server.

Tree

shell> tree .
.
├── ansible.cfg
├── hosts
│   ├── 06_iocage2.yml
│   └── 99_constructed.yml
├── host_vars
│   ├── iocage_06
│   │   └── ansible-client-apache.yml
│   └── www_1
│       └── apache.yml
├── iocage.ini
└── pb-apache.yml

Synopsis

On a managed node:

Requirements

Notes

  • iocage option --name provides “NAME instead of a UUID for the new jail”.

  • iocage property host_hostname provides “The hostname of the jail. Default: UUID”.

  • Make sure DHCP and dynamic DNS are configured so that host_hostname and --name resolve.

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

The value of the iocage tag alias is used as the inventory alias.

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
get_properties: true
inventory_hostname_tag: alias
hooks_results:
  - /var/db/dhclient-hook.address.epair0b

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

# For the default ssh connection use this dictionary
# compose:
#  iocage_tags: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)'))
#  iocage_classes: iocage_properties.notes | regex_findall('(?<=class=)[\w\-]+|(?<=,)[\w\-]+')
#  ansible_host: (iocage_hooks.0 == '-') | ternary(iocage_ip4, iocage_hooks.0)
#  ansible_python_interpreter: "'auto_silent'"
hosts/99_constructed.yml
plugin: ansible.builtin.constructed

groups:
  www: "'http' in iocage_classes"
keyed_groups:
  - prefix: state
    key: iocage_state
  - prefix: vmm
    key: iocage_tags.vmm

host_vars

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

create_hosts:
  www-1:
    template: ansible-client-apache
    properties:
      notes: alias=www_1
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 -i iocage.ini \
                         -t create_host -e create_host=true \
                         -e debug=true -e debug2=true \
                         vbotka.freebsd.pb_iocage_ansible_clients.yml
PLAY [Create and start jails. Optionally stop and destroy jails.] **************

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

TASK [Create jails] ************************************************************
ok: [iocage_06] => (item=www-1 ansible-client-apache)

TASK [Start created hosts] *****************************************************
ok: [iocage_06]

PLAY RECAP *********************************************************************
iocage_06                  : ok=3    changed=0    unreachable=0    failed=0    skipped=9    rescued=0    ignored=0   

Playbook pb-apache.yml

- name: Configure and start Apache HTTP Server.
  hosts: www_1
  gather_facts: true

  roles:
    - vbotka.freebsd.apache

Playbook output - Configure and start server

(env) > ansible-playbook -i hosts pb-apache.yml
PLAY [Configure and start 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 vbotka.freebsd.apache] ***
changed: [www_1]

RUNNING HANDLER [vbotka.freebsd.apache : Reload vbotka.freebsd.apache] *********
ok: [www_1]

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

Inventory graph

shell > ansible-inventory -i hosts --graph
@all:
  |--@ungrouped:
  |--@state_up:
  |  |--repos
  |  |--www_1
  |  |--repos_devel
  |  |--pkg_repo
  |  |--log_server_01
  |--@vmm_iocage_06:
  |  |--repos
  |  |--www_1
  |  |--repos_devel
  |  |--pkg_repo
  |  |--log_server_01
  |--@www:
  |  |--www_1

List jails

shell > ssh admin@iocage_06 sudo iocage list -l
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+
| JID |     NAME      | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |       TEMPLATE        | BASEJAIL |
+=====+===============+======+=======+======+=================+=======================+=====+=======================+==========+
| 1   | log-server-01 | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.10/24 | -   | ansible-init          | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+
| 2   | pkg-repo      | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.23/24 | -   | ansible-pkg-repo      | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+
| 3   | repos         | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.21/24 | -   | ansible-repos         | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+
| 4   | repos-devel   | on   | up    | jail | 15.1-RELEASE-p1 | vnet0|172.16.99.22/24 | -   | ansible-repos         | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+
| 14  | www-1         | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.114 | -   | ansible-client-apache | no       |
+-----+---------------+------+-------+------+-----------------+-----------------------+-----+-----------------------+----------+

Results

  • Test the configuration

    [iocage_06]# iocage exec www-1 service apache24 configtest
    Performing sanity check on apache24 configuration:
    Syntax OK
    
  • Test the server is running

    [iocage_06]# iocage exec www-1 service apache24 status
    apache24 is running as pid 57456.
    
  • Test the server is working. See the IP in the list of the jails.

    [iocage_06]# lynx <IP>
    
    It works!