Here’s an example Ansible task to install Shopware on an already provisioned web server and database:
- name: Install Shopware
hosts: webserver
become: true
vars:
# Define variables for Shopware installation
shopware_version: "6.4.4.0"
shopware_download_url: "https://github.com/shopware/platform/releases/download/{{ shopware_version }}/Shopware_{{ shopware_version }}.zip"
shopware_download_dir: "/tmp/shopware"
tasks:
# Create temporary directory for Shopware download
- name: Create Shopware download directory
file:
path: "{{ shopware_download_dir }}"
state: directory
# Download Shopware
- name: Download Shopware
get_url:
url: "{{ shopware_download_url }}"
dest: "{{ shopware_download_dir }}/Shopware_{{ shopware_version }}.zip"
mode: '0644'
# Unzip Shopware
- name: Unzip Shopware
unarchive:
src: "{{ shopware_download_dir }}/Shopware_{{ shopware_version }}.zip"
dest: "/var/www/html/shopware"
remote_src: true
owner: apache
group: apache
# Set file permissions
- name: Set file permissions for Shopware
file:
path: "/var/www/html/shopware"
state: directory
recurse: yes
owner: apache
group: apache
mode: "0775"
# Create Shopware database
- name: Create Shopware database
mysql_db:
name: "shopware"
state: present
login_user: "root"
login_password: "root"
# Create Shopware database user
- name: Create Shopware database user
mysql_user:
name: "shopware"
password: "password"
priv: "shopware.*:ALL"
host: "localhost"
state: present
login_user: "root"
login_password: "root"
# Configure Shopware
- name: Configure Shopware
template:
src: "shopware.conf.j2"
dest: "/etc/httpd/conf.d/shopware.conf"
owner: root
group: root
mode: "0644"
# Restart Apache
- name: Restart Apache
service:
name: httpd
state: restarted
This task assumes that the web server is running Apache and the database is MySQL. You’ll need to modify the variables and configuration files to fit your specific environment. In addition, you’ll need to create the shopware.conf.j2
file with the appropriate Apache configuration for your setup.
An example Ansible task to create the Shopware configuration file:
- name: Create Shopware configuration file
hosts: webserver
become: true
vars:
# Define variables for Shopware configuration
shopware_db_host: "localhost"
shopware_db_name: "shopware"
shopware_db_user: "shopware"
shopware_db_password: "password"
tasks:
# Generate Shopware configuration
- name: Generate Shopware configuration
template:
src: "config.php.j2"
dest: "/var/www/html/shopware/config.php"
owner: apache
group: apache
mode: "0644"
vars:
shopware_db_host: "{{ shopware_db_host }}"
shopware_db_name: "{{ shopware_db_name }}"
shopware_db_user: "{{ shopware_db_user }}"
shopware_db_password: "{{ shopware_db_password }}"
This task assumes that you’ve already installed Shopware and created the database and user. You’ll need to modify the variables in the vars
section to match your database settings. You’ll also need to create the config.php.j2
template file with the appropriate PHP code for your setup. In this example, I’m passing in the database connection details as variables to the template. You can use any other method of passing in these values, such as reading them from a file or using Ansible Vault to encrypt them.
You can also use an Ansible block to add content to an existing file on the web server. Here’s an example of how to add the Shopware database configuration block to an existing config.php
file:
- name: Add Shopware database configuration to config.php
hosts: webserver
become: true
vars:
# Define variables for Shopware configuration
shopware_db_host: "localhost"
shopware_db_name: "shopware"
shopware_db_user: "shopware"
shopware_db_password: "password"
tasks:
# Add Shopware database configuration to config.php
- name: Add Shopware database configuration block
blockinfile:
path: "/var/www/html/shopware/config.php"
block: |
// Shopware database configuration
'db' => [
'username' => '{{ shopware_db_user }}',
'password' => '{{ shopware_db_password }}',
'host' => '{{ shopware_db_host }}',
'port' => '3306',
'dbname' => '{{ shopware_db_name }}',
'socket' => ''
],
backup: yes
This task adds a block of PHP code to the config.php
file that configures the database connection for Shopware. The |
character tells Ansible to treat the block as multi-line text. The blockinfile
module will search for the // Shopware database configuration
line and replace it with the entire block of PHP code. If the block doesn’t exist, it will be added to the end of the file. The backup: yes
option tells Ansible to create a backup of the original file before making any changes.