参数必须是str而不是字节

时间:2016-12-29 01:34:06

标签: python unicode ansible

我正在运行Travis-CI的Ansible 2.2.0.0,以便安装我们在项目中使用的Terraform的通用共享版本。

我可以在没有问题的情况下在本地运行它,但是当我在Travis中运行它时,似乎对来自变量的字符串的某些编码失败:

[WARNING]: Host file not found: /etc/ansible/hosts

[WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [terraform : create terraform directory] **********************************
changed: [localhost]

TASK [terraform : install terraform] *******************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failure downloading https://releases.hashicorp.com/terraform/0.7.13/terraform_0.7.13_linux_amd64.zip, write() argument must be str, not bytes"}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=1

Host Ansible和Python版本:

vagrant@ubuntu-14:/vagrant/ansible$ python3 --version
Python 3.4.3
vagrant@ubuntu-14:/vagrant/ansible$ ansible --version
ansible 2.2.0.0
  config file = /vagrant/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Travis Ansible和Python版本:

$ python --version
Python 3.4.2
$ ansible --version
ansible 2.2.0.0
  config file = 
  configured module search path = Default w/o overrides

我已在两个地方验证LANGen_US.UTF-8

这是我的剧本:

---
  - hosts: localhost
    roles:
      - role: terraform
        terraform_install_root: "{{ ansible_env.HOME }}/terraform/"
        bin_dir: "{{ ansible_env.HOME }}/.local/bin"

以下是roles/terraform/tasks/main.yml

---
 - name: create terraform directory
   file: path={{ terraform_install_root }}/{{ terraform_version }} state=directory

 - name: install terraform
   unarchive:
     copy: no
     src: "https://releases.hashicorp.com/terraform/{{ terraform_version }}/terraform_{{ terraform_version }}_linux_amd64.zip"
     dest: "{{ terraform_install_root }}/{{ terraform_version }}"
     creates: "{{ terraform_install_root }}/{{ terraform_version }}/terraform"

 - name: ensure bin directory exists
   file: path={{ bin_dir }} state=directory

 - name: create terraform symlink
   file: src={{ terraform_install_root }}/{{ terraform_version }}/terraform dest={{ bin_dir }}/terraform state=link

以下是roles/terraform/vars/main.yml

---
terraform_version: "0.7.13"
terraform_install_root: /opt/terraform/
bin_dir: /usr/local/bin

似乎出于某种原因,Ansible未能将某些内容转换为UTF-8,即使我没有做任何奇怪的事情,并且在本地运行正常运行在Travis上运行。

此外,似乎任何这些文件中都没有非ASCII字符:

$ file -i ansible/roles/terraform/tasks/main.yml
ansible/roles/terraform/tasks/main.yml: text/plain; charset=us-ascii
$ file -i ansible/roles/terraform/vars/main.yml
ansible/roles/terraform/vars/main.yml: text/plain; charset=us-ascii
$ file -i ansible/travis-playbook.yml
ansible/travis-playbook.yml: text/plain; charset=us-ascii

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

使用此任务src周围的引号:

 - name: install terraform
   unarchive:
     copy: no
     src: "https://releases.hashicorp.com/terraform/{{ terraform_version }}/terraform_{{ terraform_version }}_linux_amd64.zip"
     dest: "{{ terraform_install_root }}/{{ terraform_version }}"
     creates: "{{ terraform_install_root }}/{{ terraform_version }}/terraform"

然后运行它:

ansible-playbook -i 192.168.33.33, terraform.yml                                                               2 ↵

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.33.33]

TASK [terraform-stackoverflow : create terraform directory] ********************
ok: [192.168.33.33]

TASK [terraform-stackoverflow : install terraform] *****************************
changed: [192.168.33.33]

TASK [terraform-stackoverflow : ensure bin directory exists] *******************
changed: [192.168.33.33]

TASK [terraform-stackoverflow : create terraform symlink] **********************
changed: [192.168.33.33]

PLAY RECAP *********************************************************************
192.168.33.33              : ok=5    changed=3    unreachable=0    failed=0

答案 1 :(得分:0)

尽管如此,我更新了使用Travis' trusty(Ubuntu 14.04)图像测试版,问题消失了。

应该注意的是precise是Ubuntu 12.04,这是4年,现在已经有5年了。

答案 2 :(得分:0)

这是一个已知的Ansible错误(#5791)并已在develop中修复,但它尚未发布(commit ansible/ansible@1963e50)。

也许你可以在目标主机上安装一个单独的2.7 Python,只是为了使用Ansible? (您可以将解释器配置为与inventory中的ansible_python_interpreter一起使用。)我发现尝试将Python3与Ansible一起使用是一个永无止境的游戏,但是公平地说是Ansible正积极致力于解决这个问题。

相关问题