- Learn Ansible
- Russ McKendrick
- 314字
- 2025-04-04 16:37:53
Adminer
The final task for the playbook is going to be to install a PHP script called Adminer; this provides a PHP-powered interface for interacting with and managing your databases. There are three steps to install Adminer, all of which use the following nested variables that can be found in roles/php/defaults/main.yml:
adminer:
install: true
path: "/usr/share/adminer"
download: "https://github.com/vrana/adminer/releases/download/v4.6.2/adminer-4.6.2-mysql.php"
As you can see, we are using nest variables again, this time to tell our playbook to install the tool, where it should be installed, and also where it can be downloaded from. The first task in roles/php/tasks/main.yml is to create the directory where we are going to be installing Adminer:
- name: create the document root for adminer
file:
dest: "{{ adminer.path }}"
state: "directory"
mode: "0755"
when: adminer.install == true
Now that we have somewhere on our Vagrant box to install Adminer, we should download it. This time, as we are not downloading an archive, we are using the get_url module:
- name: download adminer
get_url:
url: "{{ adminer.download }}"
dest: "{{ adminer.path }}/index.php"
mode: 0755
when: adminer.install == true
As you can see, we are downloading the adminer-4.6.2-mysql.php file from GitHub and saving it to /usr/share/adminer/index.php, so how do we access it? The final part of the task uses the template module to upload an additional Apache configuration file to /etc/httpd/conf.d/adminer.conf:
- name: copy the vhost.conf to /etc/httpd/conf.d/
template:
src: "adminer.conf.j2"
dest: "/etc/httpd/conf.d/adminer.conf"
when: adminer.install == true
notify: "restart httpd"
The adminer.conf.j2 template, which should be placed in roles/php/templates, looks as follows:
# {{ ansible_managed }}
Alias /adminer "{{ adminer.path }}"
<Directory "{{ adminer.path }}">
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
As you can see, it is creating an alias called /adminer, which then points at index.php in /usr/share/adminer/. As we are adding to the Apache configuration file, we are also notifying the restart httpd handler so that Apache restarts, picking up our updated configuration.