Ansible:将文件从本地复制到远程-“ AttributeError:'dict'对象没有属性'endswith'”

时间:2019-08-17 22:38:22

标签: ansible

我正在尝试将本地存储的文件(脚本和rpm)复制到一组服务器。当名称经过硬编码时,我可以复制文件,但是当我使用变量时,不能复制文件。

ansible-lint没有错误地回来。

使用变量替换时出现错误:

TASK [Copy cpu_gov.sh] 
***************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'dict' object has no attribute 'endswith'
fatal: [ceph3]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

在调试模式下,我可以看到在尾部的“ /”上是Python错误。变量的所有其他用法都可以正常工作,只有当它在“ src:”字段中时才会失败。

The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 145, in run
    res = self._execute()
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 650, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/copy.py", line 461, in run
    trailing_slash = source.endswith(os.path.sep)
AttributeError: 'dict' object has no attribute 'endswith'

fatal: [ceph3]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}
---
### Test
#

- hosts: all

  vars:
   #isdct_rpm: foobar.txt
   isdct_rpm: isdct-3.0.16-1.x86_64.rpm
   cpu_gov: cpu_gov.sh
   irq_bal: irq_balance.sh
   root_dir: /root
   bin_dir: /root/bin
   files_dir: /root/projects/ansible/bootstrap/files

  remote_user: root
  tasks:

这些工作很好-


  - name: ISDCT rpm exists?
    stat:
      path: "{{ root_dir }}/{{ isdct_rpm }}"
    register: isdct_rpm
    tags:
       - tools

  - name: cpu_gov exists?
    stat:
      path: "{{ bin_dir }}/{{ cpu_gov }}"
    register: cpu_gov
    tags:
       - tools

  - name: irq_balance exists?
    stat:
      path: "{{ bin_dir }}/{{ irq_bal }}"
    register: irq_bal
    tags:
       - tools

第一个任务是失败的任务:

  - name:  Copy ISDCT rpm
    copy:
      remote_src: no
      src: "{{ isdct_rpm }}"
      dest: "{{ root_dir }}"
    when: not isdct_rpm.stat.exists

这些工作正常:

  - name:  Copy rpm
    copy:
      remote_src: no
      src: isdct-3.0.16-1.x86_64.rpm
      dest: /root
    when: not isdct_rpm.stat.exists

  - name:  Copy cpu_gov.sh
    copy:
      remote_src: no
      src: cpu_gov.sh
      # - fails - src: "{{ cpu_gov }}"
      dest: "{{ bin_dir }}"
    when: not cpu_gov.stat.exists

  - name:  Copy irq_balance.sh
    copy:
      remote_src: no
      src: irq_balance.sh
      dest: /root
    when: not irq_bal.stat.exists
The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 145, in run
    res = self._execute()
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 650, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/copy.py", line 461, in run
    trailing_slash = source.endswith(os.path.sep)
AttributeError: 'dict' object has no attribute 'endswith'

fatal: [ceph3]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

2 个答案:

答案 0 :(得分:0)

您的vars部分中有一个名为isdct_rpm的变量,它是一个字符串,但是您正在ISDCT rpm exists?任务中注册一个具有相同名称的字典变量。这将覆盖字符串值。

停止尝试将相同的变量名用于两个不同的目的,我怀疑事情会按预期进行。

答案 1 :(得分:0)

@larsks回答了我的问题。我为变量和寄存器值使用了相同的名称。 这有效:

   #
  ###--- Copy the scripts over if needed
  #

  - name:  Copy ISDCT rpm
    copy:
      remote_src: no
      src: "{{ isdct_rpm }}"
      dest: "{{ root_dir }}"
    when: not isdctrpm.stat.exists

  - name:  Copy cpu_gov.sh
    copy:
      remote_src: no
      #src: cpu_gov.sh
      src: "{{ cpu_gov }}"
      dest: "{{ bin_dir }}"
    when: not cpugov.stat.exists

  - name:  Copy irq_balance.sh
    copy:
      remote_src: no
      src: "{{ irq_bal }}"
      dest: "{{ bin_dir }}"
    when: not irqbal.stat.exists