ansible 2.0中的模板传输错误

template transfer error in ansible 2.0

我有一个非常简单的角色,它在 ansible 1.9.4 中运行良好,但是当我 运行 使用 ansible 2.0 的相同角色时,我在模板传输期间遇到错误。 ansible 2.0 中的模板模块有什么变化吗?

我的角色布局:

.
|-- handlers
|   `-- main.yml
|-- tasks
|   `-- main.yml
|-- templates
|   |-- etc_default_isc-dhcp-server.j2
|   `-- etc_dhcp_dhcpd.conf.j2
`-- vars
    `-- main.yml

我已经使用 ansible ping 模块测试了与远程服务器的连接:

ansible -m ping all -u vagrant -k
SSH password:

192.168.33.10 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

这是我遇到错误的任务:

- name: Write the modified 'isc-dhcp-server' file
   template: 
    src: templates/etc_default_isc-dhcp-server.j2 
    dest: /etc/default/isc-dhcp-server 
    backup: yes

这是一个错误:

<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC sshpass -d25 ssh -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o Port=22 -o User=vagrant -o ConnectTimeout=10 -tt 192.168.33.10 '( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454001802.16-105340173307790 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454001802.16-105340173307790 )" )'
<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC sshpass -d25 ssh -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o Port=22 -o User=vagrant -o ConnectTimeout=10 -tt 192.168.33.10 '/bin/sh -c '"'"'sudo -HE -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-phyquqhgvqgsigneasanzgmfyvgsoyby; rc=flag; [ -r /etc/default/isc-dhcp-server ] || rc=2; [ -f /etc/default/isc-dhcp-server ] || rc=1; [ -d /etc/default/isc-dhcp-server ] && rc=3; python -V 2>/dev/null || rc=4; [ x"$rc" != "xflag" ] && echo "${rc}  "/etc/default/isc-dhcp-server && exit 0; (python -c '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();
afile = open("'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'/etc/default/isc-dhcp-server'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'", "rb")
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
    hasher.update(buf)
    buf = afile.read(BLOCKSIZE)
afile.close()
print(hasher.hexdigest())'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"' 2>/dev/null) || (python -c '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'import sha; BLOCKSIZE = 65536; hasher = sha.sha();
afile = open("'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'/etc/default/isc-dhcp-server'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'", "rb")
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
    hasher.update(buf)
    buf = afile.read(BLOCKSIZE)
afile.close()
print(hasher.hexdigest())'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"' 2>/dev/null) || (echo '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'0  '"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'/etc/default/isc-dhcp-server)'"'"'"'"'"'"'"'"''"'"''
<192.168.33.10> PUT /var/folders/rq/yhlwx3c971x6p88qwmvhpg600000gn/T/tmpb1lTNM TO /home/vagrant/.ansible/tmp/ansible-tmp-1454001802.16-105340173307790/source
<192.168.33.10> SSH: EXEC sshpass -d25 sftp -b - -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o Port=22 -o User=vagrant -o ConnectTimeout=10 '[192.168.33.10]'
fatal: [192.168.33.10]: UNREACHABLE! => {"changed": false, "msg": "ERROR! SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh", "unreachable": true}

PLAY RECAP *********************************************************************
192.168.33.10              : ok=1    changed=0    unreachable=1    failed=0

这看起来与我一年多前看到的东西非常相似,paramiko 选择 SFTP 进行文件传输。使用“-c ssh”尝试运行你的剧本,看看是否能解决你的问题

由于您没有使用 SSH 密钥进行身份验证,因此您必须在 运行 带有 -vvv 选项的 ansible-playbook 之前尝试 运行 "export ANSIBLE_SCP_IF_SSH=y"。

如果仍然无法正常工作并且错误消息发生变化,请 post 新的错误消息,以便我们可以继续找到解决您问题的方法。