Skip to content
Logo Theodo

SIMPLE ANSIBLE PROVISIONING FOR SYMFONY2 WITH POSTGRESQL

Maxime Thoonsen4 min read

Ansible is a IT automation tool very popular on GitHub. It’s written in Python and has been designed to be simple to use.

This article explains how we have created an Ansible provisioning for a Symfony2 project working with a Postgresql database. This Ansible playbook has been tested with Vagrant 1.3.5 and Ansible 1.3.3. The goal of this playbook is to provide a simple Ansible provisioning of a Ubuntu VM to run a Symfony2 project.
We will show you some useful commands that we used on our Ubuntu 12.04 LTS. We let users of other OS translate the commands.

START A SYMFONY2 PROJECT WITH ANSIBLE

The use of this repository is very easy and needs only those steps.

1. Before you start you need to have Vagrant and Ansible installed on your machine.
You can download vagrant here: http://www.vagrantup.com/downloads.html
You can find Ansible here: http://docs.ansible.com/intro\_installation.html or simply install it with

 $ apt-get install ansible

2. If you haven’t yet, it’s time to clone the repository:

$ git clone git@github.com:MaximeThoonsen/ansible-devops.git

3. Update your /etc/hosts file or your OS equivalent and add the future ip of the VM: “199.199.199.51 dev-myapphost”

4. Add your ssh public key in the provisioning by editing the /files/var/www/.ssh/authorized_keys file

5. Now you are good to create the VM:

$ vagrant up --provision

If the VM is up and running but the provision hasn’t been done, you can do:

$ vagrant provision

If some errors occur look at the lists of common errors at the end of this article. You have to wait 5-15 minutes the time for the vm to upgrade and install everything.

6. It’s ready! You should be able to see it at http://dev-myapphost

HOW TO CUSTOMIZE YOUR ANSIBLE PROVISIONING

I’ll start with a brief explanation of the purpose of each directory of the provisioning.

1. “group_vars”: This directory is used for the definition of most of the few useful variables. They are used in all the other files for the configuration of the VM.

2. The “files” directory copy the file with no change into the VM’s system. It’s an easy way to provide some configuration.

3. The “templates” directory is also for copying file but this time, as you may have guessed, you can use variables to create them dynamically. It contains the nginx’s configuration template which uses some variables defined in the group_vars/all file.

4. The “hosts” is used to define variables for the use of different deployment environments (staging, production, ..)

5. The “roles” directory contains the commands used to install all the required packages. (Postgres,git,vim, ..)

HOW TO INSTALL POSTGRESQL WITH ANSIBLE

The file that describes the needed ansible commands can be found at roles/backend/tasks/postgresql.yml

Ansible has to download the key of the repository for your VM’s OS. For ubuntu, you can find the postgres package’s key here. Adapt the following url or command to your OS using the ansible documentation and the postgres download page.

 - name: postgresql - add repository key
  apt_key: url=https://www.postgresql.org/media/keys/ACCC4CF8.asc state=present

Then you can add the repository.

- name: postgresql - add official postgresql repository
  apt_repository: repo="deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" state=present

You can generate the locale you want to use in the database with the following commands (works on ubuntu):

- name: postgresql - add locale
  lineinfile: dest=/etc/locale.gen create=yes line="fr_FR.UTF-8 UTF-8" regexp="^fr_FR.UTF-8" insertafter=EOF

- name: postgresql - regen locales
  command: /usr/sbin/locale-gen

You can finally install the package and configure the db. The database will be created with the app.name defined in the group_vars file. It’s the same for the user and the password. You can change the variables, create new ones or directly put the values you want in the ansible commands

- name: postgresql - install version 9.3
  apt: pkg=postgresql-9.3 state=latest update_cache=yes

- name: postgresql - create db
  sudo_user: postgres
  postgresql_db: name="{{ app.name }}" encoding="UTF-8" lc_collate="fr_FR.UTF-8" lc_ctype="fr_FR.UTF-8" template='template0'

- name: postgresql - create user
  sudo_user: postgres
  postgresql_user: db={{ app.name }} user={{ app.name }} password={{ app.name }}

- name: postgresql - apply privileges
  sudo_user: postgres
  postgresql_privs: db={{ app.name }} privs=ALL type=database role={{ app.name }}

- name: postgresql - install postgresql-contrib-9.3 --needed for fulltextsearch
  apt: pkg=postgresql-contrib-9.3 state=latest update_cache=yes

Thanks to Nicolas for his precious help!


List of common errors

The guest machine entered an invalid state while waiting for it to boot. Valid states are ‘starting, running’. The machine is in the ‘poweroff’ state. Please verify everything is configured properly and try again.

You may have to enable the virtualization in the bios of your machine.

fatal: [www-data@dev-myapphost] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help troubleshoot the issue

You may have not specify the IP of your VM.

  1. update your /etc/hosts file and enter the ip of your vm (like:“199.199.199.51 dev-myapphost”)
  2. add your key in the provisioning in provisioning/files/var/www/.ssh/authorized_keys

List of common warnings

…util/which.rb:32: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777

A potential solution

$ chmod go-w /usr/local/bin/

Liked this article?