FrancisV
FrancisV

Reputation: 1749

Ansible complains about "The MySQL-python module is required"

I have Ansible 2.6.1 installed in my local machine (WSL; Ubuntu):

ansible 2.6.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansile/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]

My target machine is running Ubuntu 16.04-LTS.

I'm using this task to install python3-mysqldb:

 - name: "Debian | Install Mysql Client package"
   apt:
     name: "{{ item }}"
     state: present
   with_items:
     - mysql-client
     - python3-dev
     - libmysqlclient-dev
     - python3-mysqldb
   when:
     - zabbix_server_database == 'mysql'
   tags:
     - zabbix-server
     - init
     - database

It fails in this task:

- name: "MySQL | Create database and import file >= 3.0"
  mysql_db:
    name: "{{ zabbix_server_dbname }}"
    encoding: "{{ zabbix_server_dbencoding }}"
    collation: "{{ zabbix_server_dbcollation }}"
    state: import
    target: "{{ ls_output_create.stdout }}"
  when:
    - zabbix_version is version_compare('3.0', '>=')
    - zabbix_database_sqlload
    - not done_file.stat.exists
  delegate_to: "{{ delegated_dbhost }}"
  tags:
    - zabbix-server
    - database

Here's the fail message:

fatal: [target_host -> target_host-db]: FAILED! => {"changed": false, "msg": "The MySQL-python module is required."}

I can confirm python3-mysqldb was indeed installed:

/.                                                                             
/usr                                                                           
/usr/share                                                                     
/usr/share/doc                                                                 
/usr/share/doc/python3-mysqldb                                                 
/usr/share/doc/python3-mysqldb/changelog.Debian.gz                             
/usr/share/doc/python3-mysqldb/copyright                                       
/usr/lib                                                                       
/usr/lib/python3                                                               
/usr/lib/python3/dist-packages                                                 
/usr/lib/python3/dist-packages/mysqlclient-1.3.7.egg-info                      
/usr/lib/python3/dist-packages/mysqlclient-1.3.7.egg-info/top_level.txt        
/usr/lib/python3/dist-packages/mysqlclient-1.3.7.egg-info/PKG-INFO             
/usr/lib/python3/dist-packages/mysqlclient-1.3.7.egg-info/dependency_links.txt 
/usr/lib/python3/dist-packages/_mysql_exceptions.py                            
/usr/lib/python3/dist-packages/_mysql.cpython-35m-x86_64-linux-gnu.so          
/usr/lib/python3/dist-packages/MySQLdb                                         
/usr/lib/python3/dist-packages/MySQLdb/connections.py                          
/usr/lib/python3/dist-packages/MySQLdb/release.py                              
/usr/lib/python3/dist-packages/MySQLdb/cursors.py                              
/usr/lib/python3/dist-packages/MySQLdb/constants                               
/usr/lib/python3/dist-packages/MySQLdb/constants/ER.py                         
/usr/lib/python3/dist-packages/MySQLdb/constants/CLIENT.py                     
/usr/lib/python3/dist-packages/MySQLdb/constants/REFRESH.py                    
/usr/lib/python3/dist-packages/MySQLdb/constants/FIELD_TYPE.py                 
/usr/lib/python3/dist-packages/MySQLdb/constants/FLAG.py                       
/usr/lib/python3/dist-packages/MySQLdb/constants/__init__.py                   
/usr/lib/python3/dist-packages/MySQLdb/constants/CR.py                         
/usr/lib/python3/dist-packages/MySQLdb/converters.py                           
/usr/lib/python3/dist-packages/MySQLdb/compat.py                               
/usr/lib/python3/dist-packages/MySQLdb/__init__.py                             
/usr/lib/python3/dist-packages/MySQLdb/times.py    

I also tried installing the python package MySQL-python using pip but I also got the same error message.

I'm stumped. I don't know what to do anymore.

EDIT: I also tried installing Python 2.7.x on the target machine and made sure that /usr/bin/python is symlinked to Python 2.7.x but I'm still getting the same error. I'm using DJ Wasabi's zabbix-server role

Upvotes: 4

Views: 7225

Answers (2)

Shashwot Risal
Shashwot Risal

Reputation: 121

add ansible_python_interpreter in ansible.cfg as follows:

[test-server]
server1 ansible_ssh_host=x.x.x.x ansible_ssh_user=test ansible_python_interpreter=/usr/bin/python3

Upvotes: 0

Cans
Cans

Reputation: 454

I think you are mixing things up with your delegation. I would simplify things.

Option one: run everything locally. Assumes your DB server is reachable through the network:

- hosts: localhost
  connection: local
  tasks:
    - name: "Debian | Install Mysql Client package"
      apt:
        name: "{{ item }}"
        state: present
      with_items:
        - mysql-client
        - python3-dev
        - libmysqlclient-dev
        - python3-mysqldb
      when:
        - zabbix_server_database == 'mysql'
      tags:
        - zabbix-server
        - init
        - database

    - name: "MySQL | Create database and import file >= 3.0"
      mysql_db:
        name: "{{ zabbix_server_dbname }}"
        encoding: "{{ zabbix_server_dbencoding }}"
        collation: "{{ zabbix_server_dbcollation }}"
        state: import
        target: "{{ ls_output_create.stdout }}"
      when:
        - zabbix_version is version_compare('3.0', '>=')
        - zabbix_database_sqlload
        - not done_file.stat.exists
      tags:
        - zabbix-server
        - database

Option two: run the SQL commands from the DB server (then you don't need mysql-python on your local machine, but you need python and mysql-python on the remote server hosting MySQL):

- hosts: dbserver

  tasks:
    - name: "Debian | Install Mysql Client package"
      apt:
        name: "{{ item }}"
        state: present
      with_items:
        - mysql-client
        - python3-dev
        - libmysqlclient-dev
        - python3-mysqldb
      when:
        - zabbix_server_database == 'mysql'
      tags:
        - zabbix-server
        - init
        - database

    - name: "MySQL | Create database and import file >= 3.0"
      mysql_db:
        name: "{{ zabbix_server_dbname }}"
        encoding: "{{ zabbix_server_dbencoding }}"
        collation: "{{ zabbix_server_dbcollation }}"
        state: import
        target: "{{ ls_output_create.stdout }}"
      when:
        - zabbix_version is version_compare('3.0', '>=')
        - zabbix_database_sqlload
        - not done_file.stat.exists
      tags:
        - zabbix-server
        - database

Upvotes: 1

Related Questions