Problem: Installing Postgresql on Ubuntu 12.04 via ansible

Hi

I’m encountering an odd issue while installing Postgresql server 9.1 on a blank Ubuntu 12.04 machine using ansible.

My playbook has the following simple task:

  • name: Install postgresql
    apt: pkg=postgresql state=present

No errors while running the playbook, but when I log into the provisioned machine the postgresql service has not been created (“service postgresql start” has no effect). Also, it doesn’t create the conf directory /etc/postgresql/9.1/main/. When I do “apt-get install postgresql” it says that postgresql has already been installed, so the only fix is to purge postgresql, apt autoremove and then reinstall postgresql manually.

Has anyone else encountered this problem? (Or am I missing something really stupid?)

Many thanks for any help.

Patrick

I use ubuntu 12.04 and the following ‘just works’:

  • name: Installing PosgreSQL
    apt: name={{item}} state=latest
    with_items:

  • postgresql-{{postgresql_version}}

  • postgresql-contrib-{{postgresql_version}}

  • python-psycopg2

  • name: Make sure its running
    service: name=postgresql state=started enabled=yes

but be careful when installing more than 1 version (it is possible) or if you have old config files as postgresql package will ‘disable’ it’s own startup, check

/etc/postgresql//main/start.conf, must be set to auto, can be manual or disabled.

The AWX setup playbooks also install Postgres and support 12.04, you may wish to read ours:

http://ansibleworks.com/releases/awx/setup/awx-setup-latest.tar.gz

Many thanks for your replies.

I’m baffled, I seem to be doing exactly the same as you but can’t get postgresql installed correctly. Specifically, it doesn’t do the postinstall jobs like moving the default configuration files, creating the postgres user and starting the database service (using the service command in my playbook has no effect).

Am I right in assuming that installing a package via the ansible apt module should do exactly the same things as when I install the package manually on the command line? It certainly seems to be the case for all other packages that I am installing. (Apologies if I’m missing something obvious here…)

Thanks again for your help.

ansible just uses apt and aptitude, the apt module is a wrapper to those
tools.

Yep that’s what I thought.

Was just wondering if it might be defaulting to “non-interactive” mode (or something like that) where it was omitting post-install jobs such as creating the user, starting the service, etc.

yes, it is non-interactive, but in my case it still sets up (and starts) the service, creates users and initializes db.

Solved this in the end by installing postgresql-9.3 from Postgres APT repo:


- name: Add postgres repository
  apt_repository: repo='deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' state=present
- name: Add postgres repository key
  apt_key: url=http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc state=present
- name: Install postgresql
  apt: pkg=postgresql-9.3 state=present force=yes
- name: Restart postgresql server
  service: name=postgresql state=started enabled=yes

That works without problems. But why it doesn’t work using the default ubuntu repo is a mystery. :slight_smile: