我是否需要明确切换Paramiko或Ansible以使用UTF-8处理su密码中的非ASCII字符?

时间:2016-06-30 20:02:20

标签: character-encoding ansible paramiko

我使用此Playbook命令从远程计算机获取数据库:

ansible-playbook -K -vvv ansible/fetch-database.yml

剧本的第一部分如下:

---

- name: Set this playbook to run on the live server
  hosts: webservers
  remote_user: jon
  become_method: su
  become_user: root

  roles:
    - fetch-database

角色的第一个任务就是从这个开始的(还有其他任务,但Ansible崩溃才能到达它们,所以我认为它们不相关):

---

- name: Install the MySQL packages to support MySQL exporting on the remote in Python
  apt: name={{ item }} state=installed update_cache=yes
  become: true
  with_items:
   - python-mysqldb

这样做的目的是在Ubuntu远程服务器上安装数据库库,这样我就可以进行MySQL转储,然后将SQL文件提取回我的本地主机。

我使用Paramiko作为SSH驱动程序,因此在我的本地配置ansible.cfg中配置:

[defaults]
transport=paramiko

不幸的是我收到了这个错误:

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 96, in run
    item_results = self._run_loop(items)
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 252, in _run_loop
    res = self._execute(variables=task_vars)
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 447, in _execute
    result = self._handler.run(task_vars=variables)
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/normal.py", line 33, in run
    results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/__init__.py", line 647, in _execute_module
    res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/__init__.py", line 721, in _low_level_execute_command
    rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
  File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/connection/paramiko_ssh.py", line 311, in exec_command
    chan.sendall(self._play_context.become_pass + '\n')
  File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 797, in sendall
    sent = self.send(s)
  File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 729, in send
    m.add_string(s[:size])
  File "/usr/lib/python2.7/dist-packages/paramiko/message.py", line 259, in add_string
    self.packet.write(s)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 1: ordinal not in range(128)

fatal: [server.example.com]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

看起来我的root密码是以ASCII身份传递的,而且它似乎在英镑符号(£)上挣扎。我认为这是主要的错误:

  

UnicodeEncodeError:'ascii'编解码器无法对位置1中的字符u'\ xa3'进行编码:序数不在范围内(128)

我猜这是一个错误(因为它处理得不好),但我无法想象之前没有人发现过这个错误。因此,我认为有一些选项可以通过UTF-8传递此密码 - 如果是这样,我该怎么做?

我正在使用来自GitHub的Ansible的当前devel分支,哈希46a97e1f551。我猜这个问题会受到使用中的子模块的影响,所以为了完整性,它们如下:

 1d0d5db97a26fefc5ce462ef5fac84086122bbe3 lib/ansible/modules/core (heads/devel-165-g1d0d5db)
 00b8b96906a741afa8eaa1245d0e229f099318a5 lib/ansible/modules/extras (heads/devel-116-g00b8b96)

我认为这不重要,因为它感觉像是Python问题或配置问题,但本地和远程机器都是Ubuntu 14.04。

1 个答案:

答案 0 :(得分:0)

我目前的解决方案是禁用Paramiko并使用默认的SSH传输机制。这之前没有用,并挂在需要su的命令上,这就是为什么我首先交换到Paramiko的原因。然而,这个问题似乎最近已得到修复。

Paramiko问题的错误报告is here