Quick start guide
For users who want to try the role quickly, this guide provides an example of how to install and configure lighttpd on a single FreeBSD host. The procedure is generic and can be easily modified to install and configure other applications on other systems. See examples in the directory contrib. The control node of this example is Linux, and the user running the playbook on the controller is a member of the group adm.
Install the role, collections, and linter
Install the role
vbotka.config_lightshell> ansible-galaxy role install vbotka.config_light
Install the collections if necessary
shell> ansible-galaxy collection install ansible.posix shell> ansible-galaxy collection install community.general shell> ansible-galaxy collection install vbotka.freebsd
Install
yamllintto use the default validation of the created handlers and assembled data. See the variables cl_assemble_validate and cl_handlers_validate in defaults/main.yml. Optionally, useansible-lintor disable the validation by clearing the variablescl_assemble_validate: '' cl_handlers_validate: ''
Create project
Create a project
shell> tree .
.
├── conf-light
│ ├── files.d
│ │ ├── lighttpd-index.yml
│ │ ├── lighttpd-lighttpdconf.yml
│ │ └── lighttpd-rcconf.yml
│ ├── handlers.d
│ │ └── lighttpd-freebsd.yml
│ ├── packages.d
│ │ └── lighttpd.yml
│ ├── services.d
│ │ └── lighttpd.yml
│ └── states.d
│ └── lighttpd-server-document-root.yml
├── group_vars
│ └── all
│ ├── cl-common.yml
│ ├── cl-lighttpd.yml
│ └── common.yml
└── pb.yml
Create the playbook
pb.ymlfor single host srv.example.com (1) and the role vbotka.config_light (10)
1- hosts: srv.example.com
2 gather_facts: true
3 connection: ssh
4 remote_user: admin
5 become: true
6 become_user: root
7 become_method: sudo
8
9 roles:
10 - vbotka.config_light
Configure role
Create common variables
1freebsd_install_method: packages
2freebsd_pkgng_use_globs: false
Configure the role. To speed up the execution, set the control-flow variables (1-3) to false and disable some steps. Enable these steps selectively when needed. The configuration data will be stored in the directory conf-light (8) in the current directory of the playbook. Set the ownership and permissions of the directories on the control node so that the user who is running the playbook will be able to both read and write the files, and create the directories cl_dird, cl_dira, and “{{ role_path }}/handlers”
1cl_sanity: false
2cl_setup: false
3cl_install: false
4cl_backup: true
5
6cl_dird_group: adm
7cl_dird_dmode: "0770"
8cl_dird: "{{ playbook_dir }}/conf-light"
9
10cl_dira_group: adm
11cl_dira_dmode: "0770"
12cl_dira_fmode: "0660"
13
14cl_handlers_dir_group: adm
Note
The configuration data will be assembled into the directory
cl_diraThe default value of
cl_dirais"{{ cl_dird }}/assemble"
Configure lighttpd
Configure the application. Start the server (1), run the server at boot (2), and configure two files (4,17)
1cl_service_lighttpd_enable: true
2cl_service_lighttpd_state: start
3
4# /usr/local/etc/lighttpd/lighttpd.conf
5cl_lighttpd_server_port: '80'
6cl_lighttpd_server_useipv6: disable
7cl_lighttpd_server_username: www
8cl_lighttpd_server_groupname: www
9cl_lighttpd_server_document_root: /usr/local/www/lighttpd
10cl_lighttpd_lighttpdconf_dict:
11 - {key: server.port, value: '"{{ cl_lighttpd_server_port }}"'}
12 - {key: server.use-ipv6, value: '"{{ cl_lighttpd_server_useipv6 }}"'}
13 - {key: server.username, value: '"{{ cl_lighttpd_server_username }}"'}
14 - {key: server.groupname, value: '"{{ cl_lighttpd_server_groupname }}"'}
15 - {key: server.document-root, value: '"{{ cl_lighttpd_server_document_root }}"'}
16
17# /etc/rc.conf
18cl_lighttpd_rcconf_lighttpd_enable: 'YES'
19cl_lighttpd_rcconf_dict:
20 - {key: lighttpd_enable, value: '"{{ cl_lighttpd_rcconf_lighttpd_enable }}"'}
Create configuration data in the directory
conf-light/
lighttpd-index:
path: "{{ cl_lighttpd_server_document_root }}/index.html"
owner: "{{ cl_lighttpd_server_username }}"
group: "{{ cl_lighttpd_server_groupname }}"
create: true
mode: "0644"
lines:
- line: Lighttpd works !
lighttpd-lighttpdconf:
path: /usr/local/etc/lighttpd/lighttpd.conf
create: true
owner: root
group: wheel
mode: '0644'
assignment: ' = '
dict: "{{ cl_lighttpd_lighttpdconf_dict }}"
handlers:
- reload lighttpd
lighttpd_rcconf:
path: /etc/rc.conf
create: true
owner: root
group: wheel
mode: '0644'
assignment: '='
dict: "{{ cl_lighttpd_rcconf_dict }}"
handlers:
- reload lighttpd
1lighttpd_freebsd:
2
3 template: handlers-auto3.yml.j2
4 handlers:
5
6 - handler: Start lighttpd
7 listen: start lighttpd
8 module: vbotka.freebsd.service
9 params:
10 - 'script: lighttpd'
11 - 'command: start'
12
13 - handler: Stop lighttpd
14 listen: stop lighttpd
15 module: vbotka.freebsd.service
16 params:
17 - 'script: lighttpd'
18 - 'command: stop'
19
20 - handler: Reload lighttpd
21 listen: reload lighttpd
22 module: vbotka.freebsd.service
23 params:
24 - 'script: lighttpd'
25 - 'command: reload'
26 conditions:
27 - '- cl_service_lighttpd_enable | bool'
28
29 - handler: Restart lighttpd
30 listen: restart lighttpd
31 module: vbotka.freebsd.service
32 params:
33 - 'script: lighttpd'
34 - 'command: restart'
35 conditions:
36 - '- cl_service_lighttpd_enable | bool'
37
38 - handler: Lighttpd check
39 listen: lighttpd check
40 module: ansible.builtin.command
41 params:
42 - 'cmd: /usr/local/sbin/lighttpd -t'
lighttpd:
module: pkgng
name:
- www/lighttpd
lighttpd:
name: lighttpd
state: "{{ cl_service_lighttpd_state }}"
enabled: "{{ cl_service_lighttpd_enable }}"
lighttpd_server_document_root:
state: directory
path: "{{ cl_lighttpd_server_document_root }}"
owner: "{{ cl_lighttpd_server_username }}"
group: "{{ cl_lighttpd_server_groupname }}"
mode: '0750'
Setup
Select and enable setup. This command will assemble the configuration data and create handlers on the control node. Take a look at the directory
conf-light/assemble/what files were created. Also, look at the directoryroles/vbotka.config_light/handlers, what handlers were createdshell> ansible-playbook pb.yml -t cl_setup -e cl_setup=true
Note
The tasks vars are tagged
always.The tasks setup and sanity are enabled by default
cl_setup=true, cl_sanity=true.
Enable and test sanity
shell> ansible-playbook pb.yml -t cl_sanity -e cl_sanity=true
Display variables
shell> ansible-playbook pb.yml -t cl_debug -e cl_debug=true
Run the play
Install packages
shell> ansible-playbook pb.yml -t cl_packages -e cl_install=true
Set files state
shell> ansible-playbook pb.yml -t cl_states
Create and modify files
shell> ansible-playbook pb.yml -t cl_files
Configure services
shell> ansible-playbook pb.yml -t cl_services
Hint
If you know what you are doing skip the above selection of particular tags and run the complete role at once
shell> ansible-playbook pb.yml -e cl_setup=true -e cl_sanity=true -e cl_install=true
See also
The collection vbotka.freebsd examples.
Note
The role and the configuration data in the examples are idempotent. Once the application is installed and configured ansible-playbook shouldn’t report any changes. To speedup the playbook disable setup, sanity, debug, and install. This way, the role will audit the required infrastructure
shell> ansible-playbook pb.yml
[...]
PLAY RECAP ***************************************************************************
srv.example.com: ok=32 changed=0 unreachable=0 failed=0 skipped=91 rescued=0 ignored=0
Results
Open the page in a browser
http://srv.example.com/. The content should beLighttpd works!