我正在尝试通过将构建服务器中的wordpress实例部署到另一台主机服务器来自学。两个服务器都是Ubuntu 16.04,一切正常,直到构建运行mysql tasks main.yml文件时,我得到以下错误:
“python mysqldb模块是必需的”
我在我的server / tasks / main.yml文件中包含了python-mysqldb,因此不确定错误是什么。有人能指出我正确的方向吗?
MySQL的/任务/ 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
服务器/任务/ 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
运行时出现控制台输出错误: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"}
答案 0 :(得分:4)
您可以按照以下要求安装:
- 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
如果这不起作用,那么你可以这样做:
- 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
答案 1 :(得分:1)
好吧,让我们考虑一下为什么会这样。 默认情况下,Ubuntu 16.04安装了Python 3.5.1作为python3二进制文件。 Python 2仍然可以安装,但不是默认的。
当我们使用带有ansible的ubuntu 16.04时,通常我们会有一些破坏性的更改,因为大多数ansible模块都是由Python 2创建的,这个错误是由python问题引起的,因此修复它的简单方法是在运行之前安装python 2 ansible,让我们展示两种方法。
1 - 只需安装python 2然后运行ansible
sudo apt-get install -y python
2 - 例如,如果您正在使用vagrant,请在运行ansible之前添加shell脚本文件作为vm provision,如下所示:
在vagrantfile中,在ansible call之前添加此行
config.vm.provision "shell", path: "provision.sh"
使用这个简单的安装python创建provision.sh:
#!/bin/sh
#
sudo apt-get install -y python
正常运行流浪汉,一切都应该运作良好
我希望我能帮助你们所有人
答案 2 :(得分:0)
我遇到了与错误相同的问题
{“已更改”:false,“failed”:true,“msg”:“python mysqldb模块是必需的”}
运行此剧本解决了我的问题。在流浪盒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
...
或强>
第1步: apt-get install mysql-server python-pip python-dev libmysqlclient-dev
第2步:pip install --index=https://pypi.python.org/pypi/ MySQL-python==1.2.3
答案 3 :(得分:0)
在我看来,这可行。
- apt:
name: "{{ item }}"
state: present
update_cache: True
with_items:
- mysql-server
- python3-pip
- libmysqlclient-dev
- python3-dev
- python3-mysqldb