6
votes

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"}
4
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

4 Answers

5
votes

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
2
votes

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

2
votes

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
1
votes

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