6

I'm trying to teach myself ansible by deploying a wordpress instance from a build server to another host server. Both servers are Ubuntu 16.04 and everything works fine until the build gets to running the mysql tasks main.yml file when i get the below error:

"the python mysqldb module is required"

I have included python-mysqldb in my server/tasks/main.yml file so not sure what the error is. Can anyone point me in the right direction please?

mysql/tasks/main.yml

---
# tasks file for mysql
- name: Create mysql database
  mysql_db: name={{ wp_mysql_db }} state=present

- name: Create mysql user
  mysql_user:
    name={{ wp_mysql_user }}
    password={{ wp_mysql_password }}
    priv=*.*:ALL

server/tasks/main.yml

---
# tasks file for server
- name: Update apt cache
  apt: update_cache=yes cache_valid_time=3600
  sudo: yes

- name: Install required software
  apt: name={{ item }} state=present
  sudo: yes
  with_items:
    - apache2
    - python-mysqldb
    - mysql-server
    - mysql-client
    - php7.0
    - php7.0-mysql
    - libapache2-mod-php7.0
    - php7.0-cli
    - php7.0-cgi
    - php7.0-gd
    - php7.0-mcrypt

console output error from running: ansible-playbook playbook.yml -i hosts -u jbloggs -K

TASK [mysql : Create mysql database] *******************************************
task path: /etc/ansible/roles/mysql/tasks/main.yml:3
fatal: [wordpress1]: FAILED! => {"changed": false, "failed": true, "msg": "the python mysqldb module is required"}
Phillip Hogan
  • 123
  • 1
  • 2
  • 9
  • Your playbook works ok on Vagrant's `bento/ubuntu-16.04` box, `python-mysqldb` from APT is properly recognised by Ansible, so your problem must be elsewhere. What Python do you use on your Ubuntu 16.04? It doesn't have Python 2 by default, right? So have you added it? Or do you try to use Python 3? – techraf Feb 25 '17 at 00:30

4 Answers4

5

You can install this as per-req:

- name: Install required software
  apt: name={{ item }} state=present
  sudo: yes
  with_items:
    - apache2
    - build-essential
    - python-dev
    - libmysqlclient-dev
    - python-mysqldb
    - mysql-server
    - mysql-client
    - php7.0
    - php7.0-mysql
    - libapache2-mod-php7.0
    - php7.0-cli
    - php7.0-cgi
    - php7.0-gd
    - php7.0-mcrypt

If that doesn't work then you can do like this:

- name: Install required software
  apt: name={{ item }} state=present
  sudo: yes
  with_items:
    - apache2
    - build-essential
    - python-dev
    - libmysqlclient-dev
    - python-pip
    - mysql-server
    - mysql-client
    - php7.0
    - php7.0-mysql
    - libapache2-mod-php7.0
    - php7.0-cli
    - php7.0-cgi
    - php7.0-gd
    - php7.0-mcrypt

- name: Install the MySQL-python through pip
  sudo: yes
  pip:
    name: "{{ item }}"
    state: forcereinstall
  with_items:
    - pip
    - MySQL-python
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
  • Thank you. I tried the first option and it failed with the same error, then the second which got a little further, but then failed with an error as below: – Phillip Hogan Feb 24 '17 at 16:00
  • 1
    TASK [server : Install the MySQL-python through pip] *************************** ----------------------------------------\n\n:stderr: Command \"python setup.py egg_info\" failed with error code 1 in /tmp/pip-build-3bpquj_j/MySQL-python/\nYou are using pip version 8.1.1, however version 9.0.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.\n"} – Phillip Hogan Feb 24 '17 at 16:01
  • @PhillipHogan please try the modified answer – Arbab Nazar Feb 24 '17 at 20:13
  • I tried the updated answer but am now getting the below error: – Phillip Hogan Mar 07 '17 at 15:21
  • failed: [wordpress1] (item=MySQL-python) => { "cmd": "/usr/local/bin/pip3 install -U --force-reinstall MySQL-python", "failed": true }, "item": "MySQL-python", ImportError: No module named 'ConfigParser'\n \n ----------------------------------------\n\n:stderr: Command \"python setup.py egg_info\" failed with error code 1 in /tmp/pip-build-3lsqwtrj/MySQL-python/\n" } – Phillip Hogan Mar 07 '17 at 15:23
3

I had the same issue with error

{"changed": false, "failed": true, "msg": "the python mysqldb module is required"}

Running this playbook fixed my issue. Tested on vagrant box ubuntu/precise64

---
- hosts: vagrant1
  gather_facts: no
  tasks:
  - name: "updating server"
    apt:
      update_cache: yes
  - name: "Installing apt dependencies"
    apt:
     name: "{{item}}"
    with_items:
      - python-pip
      - python-dev 
      - libmysqlclient-dev

  - name: "Installing pip dependencies"
    pip:
      name: MySQL-python
      extra_args: --index=https://pypi.python.org/pypi/
      version: 1.2.3

  - name: "Installing  mysql server"
    apt:
      name: mysql-server

  - name: "Creating mysql user"
    mysql_user:
      name: root #your mysql username
      password: root #your mysql password
      priv: '*.*:ALL'
      state: present
...

OR

Step 1: apt-get install mysql-server python-pip python-dev libmysqlclient-dev

Step 2: pip install --index=https://pypi.python.org/pypi/ MySQL-python==1.2.3

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Thanks - I'll give that a go. Do you know of a way I can get this all to run in Ansible though? Ideally I'd like everything to be as automated as possible. – Phillip Hogan Feb 24 '17 at 12:25
  • The shell or the command module will be your friend to automate command lines: http://docs.ansible.com/ansible/latest/list_of_commands_modules.html . Simply create a new task or a new role depending on your structure and give it a go :) – daneczech Nov 08 '17 at 10:30
2

In my case this works.

- apt:
    name: "{{ item }}"
    state: present
    update_cache: True
  with_items:
    - mysql-server
    - python3-pip
    - libmysqlclient-dev
    - python3-dev
    - python3-mysqldb
Kazuya Gosho
  • 996
  • 13
  • 14
1

Well, let's think about why this happens. Ubuntu 16.04 comes by default with Python 3.5.1 installed as the python3 binary. Python 2 is still installable but is not default.

When we use ubuntu 16.04 with ansible usually we have some broken changes because the most of ansible modules are created by Python 2 and this error is caused by issues with python so the easy way to fix it is to install python 2 before run ansible, let met show two ways to do that.

1 - Just install python 2 and then run ansible

sudo apt-get install -y python

2 - If you are using vagrant, for example, add shell script file before run ansible as vm provision, like this:

In vagrantfile add this line before ansible call

config.vm.provision "shell", path: "provision.sh"

Create provision.sh with this simple install python:

#!/bin/sh
#
sudo apt-get install -y python

run vagrant normally and everything should work well

I hope I can help you all

Paulo Victor
  • 3,814
  • 2
  • 26
  • 29