How to deploy ownCloud using Ansible

Introduction

With ownCloud, you can achieve similar functionality to the widely used Dropbox or Google Drive. In this tutorial, you will learn how to deploy ownCloud automatically with Ansible on Fuga cloud.

Note: This tutorial is tested with Ansible 2.7.9 and Ubuntu 16.04 LTS. If your running the latest ansible version and experience any problems with this tutorial, please contact us.

 

Prerequisites

You can deploy the instance and volume needed for this tutorial using the following Ansible tutorial. How to deploy Fuga Cloud infrastructure with Ansible.
If you have followed that tutorial you can skip step 1 and 2

Step 1: Creating security groups for HTTP(S) access

To access ownCloud from outside your instance, you need to open the HTTP and HTTPS ports.

  1. Open the Fuga Cloud dashboard.
  2. Click on the networking tab and select Security Group
  3. Click on the button Create security group and give it a name and a description to your liking. Click on the button Create security group
  4. Click on the button Manage Rules that belong to the newly created security group.
  5. Click the button Add Rule.
  6. From the dropdown list Rule select HTTP and click on Add.
  7. Repeat step 4 and select HTTPS and click on Add.
  8. To assign the new security group to your Instance open the Compute tab and select Instances.
  9. Click on the down arrow right of the Create snapshot button and click on Edit security groups.
  10. Click on the + button that belongs to your newly made security group.
  11. Click Save to update the security groups.

Step 2: Creating an empty Volume

  1. Click on the Storage tab and select Volume Store
  2. Click on the button Create Volume
  3. Give the volume a name and description to your liking
  4. In the dropdown list, Volume Source select No source, empty volume
  5. In the dropdown list Type select SSD
  6. Give the volume the size that you like for example 100GB
  7. Click on the button Create Volume
    Your volume should now be visible in the Volumes tab
  8. Click on the down arrow right of the Edit Volume button and select Manage Attachments
  9. Select your instance in the Attach to Instance dropdown list and click on Attach volume

Step 3: Using Ansible to deploy ownCloud on your Fuga instance

  1. Install Ansible with pip3 on your machine where you will run the ansible script (not your ownCloud instance):

    sudo apt update
    sudo apt install python3-pip
    sudo pip3 install ansible
  2. Create a new folder called ansible:

    mkdir ansible
  3. The Ansible script uses a template file to make sure Ansible is being executed with Python3 on your instance. You need to create this template file inside your ansible directory by using the following command:

    touch fuga_hosts
  4. Open fuga_hosts (nano fuga_hosts) and copy paste the following:

    [fuga_vms:vars]
    ansible_python_interpreter=/usr/bin/env python3

    [fuga_vms]
    YOUR_INSTANCE_IP ansible_ssh_private_key_file=YOUR_KEY_PATH
  5. Make sure to replace YOUR_INSTANCE_IP with the (Floating) IP from your instance. This is where Ansible will be connecting to. YOUR_KEY_PATH is the path to your private SSH key.

  6. Save the fuga_hosts file

  7. Inside your ansible directory also create a file called owncloud.j2. This will configure Apache to make ownCloud reachable for your browser.

    touch owncloud.j2
  8. Open owncloud.j2 (nano owncloud.j2) and paste in the following code:

    Alias /owncloud "/var/www/owncloud/"

    <Directory /var/www/owncloud/>
    Options +FollowSymlinks
    AllowOverride All

    <IfModule mod_dav.c>
    Dav off
    </IfModule>

    SetEnv HOME /var/www/owncloud
    SetEnv HTTP_HOME /var/www/owncloud

    </Directory>
  9. Inside of your ansible directory create a file called deploy_owncloud.yml:

    touch deploy_owncloud.yml
  10. Now it’s time to create our Ansible playbook file. This playbook is responsible for executing all the commands needed for your ownCloud installation. All the steps are separated with an attribute called name under tasks. Open deploy_owncloud.yml (nano deploy_owncloud.yml) and paste the following code inside:

- name: Deploy
remote_user: ubuntu
hosts: fuga_vms
become: yes
tasks:
- name: Create mount dir
file:
path: /var/www/owncloud/data
state: directory

- name: Format volume
filesystem:
fstype: ext4
dev: /dev/vdb

- name: Create mountpoint
mount:
path: /var/www/owncloud/data
src: /dev/vdb
fstype: ext4
state: mounted

- name: Update permissions
file:
path: /var/www/owncloud/data
state: directory
owner: www-data
group: www-data

- name: Install required packages for ownCloud
apt: name={{item}} state=present update-cache=yes
with_items:
- apache2
- mariadb-server
- libapache2-mod-php7.0
- openssl
- php-imagick
- php7.0-common
- php7.0-curl
- php7.0-gd
- php7.0-imap
- php7.0-intl
- php7.0-json
- php7.0-ldap
- php7.0-mbstring
- php7.0-mcrypt
- php7.0-mysql
- php7.0-pgsql
- php-smbclient
- php-ssh2
- php7.0-sqlite3
- php7.0-xml
- php7.0-zip
- python3-mysqldb
- python3-dev
- libmysqlclient-dev

- name: Add apt-key from ownCloud
apt_key:
url: https://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/Release.key
state: present

- name: Add ownCloud repository
apt_repository:
repo: deb https://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/ /
state: present

- name: Install / Download ownCloud
apt:
name: owncloud-files
update-cache: yes

- name: Create 'owncloud' Database
mysql_db:
name: owncloud
state: present

- name: Change password of owncloud database user
command: >
mysql -u root --execute="GRANT ALL ON owncloud.* to 'owncloud'@'localhost' IDENTIFIED BY 'MY_PASSWORD_HERE'; FLUSH PRIVILEGES;"

- name: Copy ownCloud Apache config
template:
src: owncloud.j2
dest: "/etc/apache2/sites-available/owncloud.conf"

- name: Create syslink to available-sites
become: yes
file:
src: "/etc/apache2/sites-available/owncloud.conf"
dest: "/etc/apache2/sites-enabled/owncloud.conf"
state: link

- name: Restart Apache
command: "systemctl reload apache2"
  1. In the task Change password of the ownCloud database user, we have to update MY_PASSWORD_HERE to a safer password. Don’t forget this password, you will need it when setting up ownCloud itself.
  2. Save the file and run this Ansible playbook file with:

    ansible-playbook deploy_owncloud.yml -i fuga_hosts
  3. Your Ansible playbook script will now automatically install all the required packages and setup ownCloud.

Setting up ownCloud

  1. Open your favorite web browser and browse to INSTANCE-IP/owncloud. For example:

    http://185.XX.XX.XX/owncloud

    If you don’t know the IP address of your instance, you can either find it in the fuga_hosts file or in your Fuga Cloud dashboard.

  2. Fill in your desired username and password for your desired ownCloud account.

  3. Click on Storage & database and select MySQL/MariaDB as database.

  4. Enter owncloud as database user.

  5. Enter your own password as database password.

  6. In the database field, enter owncloud again.

It should look something like this:

Owncloud setup

7. Now click on Finish setup and your own ownCloud is ready to be used!