【问题标题】:The conditional check dict object' has no attribute ansible error条件检查dict对象'没有属性ansible错误
【发布时间】:2025-12-12 09:25:02
【问题描述】:

我有一本在 us-east-1 中运行没有问题的剧本,但在 aws 中的 ap-southeast-1 中有问题。这是剧本失败的部分

- hosts: "tag_deployment_group_{{ env }}_{{ app }}"
  gather_facts: false
  remote_user: root
  tasks:
    - name: "wait for instances to be reachable"
  wait_for_connection:
    delay: 60
    timeout: 500
- ec2_remote_facts:
    region: ap-southeast-1
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
    filters:
      "tag:Env": "{{ env }}"
  register: instance_facts
- add_host:
    name: "{{ item.tags.Name }}"
    ansible_host: "{{ item.private_ip_address }}"
    group: "{{ env }}_{{app}}"
  with_items: "{{ instance_facts.instances }}"
- name: "copy cron script"
  copy: src=/etc/ansible/files/delete.sh dest=/root/scripts/delete.sh
  when: item.tags.app_type == 'platform'
  with_items: "{{ instance_facts.instances }}"

我得到的错误如下

"failed": true, 
"msg": "The conditional check 'item.tags.app_type == 'platform'' 
failed. The error was: error while evaluating conditional 
(item.tags.app_type == 'platform'): 'dict object' has no attribute 
'app_type'\n\nThe error appears to have been 
ts.yml': line 80, column 7, but may\nbe elsewhere in the file depending 
on the exact syntax problem.\n\nThe offending line appears to be:\n\n      
with_items: \"{{ instance_facts.instances }}\"\n    - name: \"copy cron 
script\"\n      ^ here\n"

【问题讨论】:

  • 不太确定,请看这里

标签: python amazon-ec2 ansible jinja2


【解决方案1】:

显然app_type 标记缺少实例。

附:但我怀疑你的剧本中存在设计缺陷——如果你希望 delete.sh 脚本出现在带有标签 app_type == 'platform' 的实例上,那么你的剧本是错误的。

【讨论】:

  • 实际上它在我将其更改为 ```` 后开始工作,当:“item.tags.app_type 已定义且 item.tags.app_type 中的“平台”” ```` 来自您的旧帖子.但是,如果 ``` app_type == 'platform' ```,剧本的目的是复制脚本。你能解释一下吗?
  • 一般你应该在不同的播放中运行add_host 和该主机的任务。见*.com/a/47647197/2795592
  • 感谢您的澄清。我会这样实现
最近更新 更多