简体   繁体   English

参数必须是str而不是字节

[英]Argument must be str not bytes

I'm running Ansible 2.2.0.0 from Travis-CI in order to install a common shared version of Terraform that we are using in our project. 我正在从Travis-CI运行Ansible 2.2.0.0,以便安装我们在项目中使用的Terraform的通用共享版本。

I can run it locally without issues, but when I run it in Travis, it seems to fail on some encoding of a string sourced from a variable: 我可以在本地运行它而不会出现问题,但是当我在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 and Python versions: 主机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 and Python versions: 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

I have validated that LANG is en_US.UTF-8 in both places. 我已经验证LANG在两个地方都是en_US.UTF-8

Here is my playbook: 这是我的剧本:

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

Here is roles/terraform/tasks/main.yml : 这是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

Here is roles/terraform/vars/main.yml : 这是roles/terraform/vars/main.yml

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

It appears that for some reason, Ansible is failing to convert something into UTF-8, even though I'm not doing anything strange, and what runs locally just fine does not run on Travis. 看来由于某种原因,Ansible无法将某些内容转换为UTF-8,即使我没有做任何奇怪的事情,并且在Travis上也无法在本地运行。

Furthermore, it appears that there are no non-ASCII characters in any of these files: 此外,似乎这些文件中没有任何非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

Any ideas? 有任何想法吗?

Use the quotes around src of this task: 在此任务的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"

Then run it: 然后运行它:

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

As much of a cop-out as this is, I updated to use Travis' trusty (Ubuntu 14.04) image beta and the problem went away. 尽可能多地解决问题,我更新为使用Travis的trusty (Ubuntu 14.04)图片Beta,问题消失了。

It should be noted that precise is Ubuntu 12.04, which is four, coming on five years old. 应该注意的是, precise是Ubuntu 12.04,它已经发布五年了,现在是四个。

It's a known Ansible bug ( #5791 ) and has been fixed in develop , although it is not in a release yet ( commit ansible/ansible@1963e50 ). 这是一个众所周知Ansible的bug( #5791 ),并已被固定在develop ,虽然它不是一个版本,但( 提交ansible / ansible @ 1963e50 )。

Perhaps you can install a separate 2.7 Python on the target host just for the use of Ansible? 也许您可以仅使用Ansible在目标主机上安装单独的2.7 Python? (You can configure the interpreter to use with ansible_python_interpreter in the inventory .) I found trying to use Python3 with Ansible is a never-ending game of whack-a-bug, although in all fairness the Ansible is actively working on fixing that. (您可以在清单中将解释器配置为与ansible_python_interpreter一起使用。)我发现尝试将Python3与Ansible一起使用是一个永无止境的whack-a-bug游戏,尽管公平地说,Ansible一直在积极地解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM