【发布时间】:2014-03-07 13:51:40
【问题描述】:
我正在尝试在 ansible 中编写自定义模块。使用import MySQLdb 时出现错误
failed: [127.0.0.1] => {"failed": true, "parsed": false}
invalid output was: Traceback (most recent call last):
File "/root/.ansible/tmp/ansible-1394199347.29-33439012674717/inventory", line 11, in <module>
import MySQLdb
ImportError: No module named MySQLdb
使用
Python 版本: 2.6.6
MySQL-python 版本: 1.2.3
Python 代码:-
#!/usr/bin/python
import datetime
import sys
import json
import os
import shlex
import MySQLdb
db = MySQLdb.connect("localhost","user","pwd","db_name" )
cursor = db.cursor()
cursor.execute("SELECT * FROM hosts")
data = cursor.fetchone()
print data
db.close()
我写了一个剧本来运行 ansible 模块:-
inventory.yaml:-
---
- hosts: webservers
user: root
sudo: True
vars:
act: list
tasks:
- name: Run module inventory
action: inventory act="{{act}}" prod="roop"
我正在使用以下命令运行此剧本:-
ansible-playbook -v playboook/path/inventory.yaml
在 python 命令行 (
在我的 ansible 模块中,其他代码正在运行。 ansible需要做任何配置设置吗?
【问题讨论】:
-
你确定你用来运行脚本的用户有足够的权限来运行 MySQLdb 模块吗?
-
我以 root 用户身份运行此脚本
-
你能用 ANSIBLE_KEEP_REMOTE_FILES=1 运行并显示 /root/.ansible/tmp/ansible-xxx/inventory 代码吗?你在用 virtualenv 吗?
-
@leucos。我尝试使用 ANSIBLE_KEEP_REMOTE_FILES 显示相同的错误消息。和我在 python 代码中提到的库存代码块相同。不,我没有使用 virtualenv
-
我的意思是使用 ANSIBLE_KEEP_REMOTE_FILES 并检查生成的文件(参见关于模块开发的 ansible 文档)
标签: python mysql-python ansible