Ansible demands installing MySQL-python despite it

2019-05-05 17:14发布

问题:

I am trying to create a new MySQL database with Ansible controller running on Mac OSX. When I first got msg: the python mysqldb module is required error message, I added a task to install the MySQL-python with pip. It got installed correctly, however still I am still getting an error from Ansible demanding its installation.

My minimal playbook is:

- hosts: all
  tasks:
  - name: Ensure MySQL-python module is installed
    pip:
      name: MySQL-python
      executalbe: /usr/local/Cellar/python/2.7.10_2/bin/pip

  - name: Create test_db MySQL database
    mysql_db:
      name: test_db
      state: present

when I run the playbook with:

ansible-playbook -i "localhost," -c local mysql-test.yml

I get the following result (with changed for the first task upon first run):

TASK: [Ensure MySQL-python module is installed] **************************************
ok: [localhost]

TASK: [Create test_db MySQL database] *********************************************
failed: [localhost] => {"failed": true}
msg: the python mysqldb module is required

pip show MySQL-python shows the package got installed correctly.

I am running Python 2.7.10 and Ansible 1.9.4 both installed with homebrew, thus I don't use sudo.

What is missing?


  • I checked the playbook against ubuntu/trusty64 Vagrant machine and it worked with no problem (with OSX being the Ansible controller, the only difference was a requirement for sudo in pip module).

  • I checked the playbook on a second Mac both locally with -c local and remotely via SSH and got the same error as in original question (for pip to work correctly through SSH I had to add executalbe=/usr/local/Cellar/python/2.7.10_2/bin/pip otherwise it reported msg: Failed to find required executable pip)

The results of the task when run with -c local -vvvv:

<localhost> REMOTE_MODULE mysql_db name=test_db state=present
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037 && echo $HOME/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037']
<localhost> PUT /var/folders/nw/2vnhg_gj77v_cyfv0p1vdfj80000gn/T/tmpK3DT_j TO /Users/techraf/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037/mysql_db
<localhost> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/techraf/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037/mysql_db; rm -rf /Users/techraf/.ansible/tmp/ansible-tmp-1446497958.1-90296161052037/ >/dev/null 2>&1']
failed: [localhost] => {"failed": true}
msg: the python mysqldb module is required

回答1:

The reason for the problem was that Ansible used the default OSX's Python (/usr/bin/python) which is visible in the results when run with -vvvv option.

First task succeeded because default OSX's Python called Homebrew's pip executable and installed the MySQL-python module for the Homebrew's Python.

The second task failed because it run default OSX's Python again which required MySQL-python, but the module was not installed for this version.

The solution was to use the option to specify the path to the Python interpreter to be used by Ansible:

ansible-playbook -i "localhost," -c local --extra-vars "ansible_python_interpreter=/usr/local/bin/python" mysql-test.yml

or to add ansible_python_interpreter=/usr/local/bin/python to the inventory file.

The same problem was mentioned, it contains answers with other possible solutions.