首页 > 解决方案 > Ansible 列表为空时有长度

问题描述

我正在做“aws rds describe-db-instances”:

- name: list recovery points by resource
  shell: aws rds describe-db-instances --db-instance-identifier {{ item }} --query "DBInstances[?(LatestRestorableTime>'{{ time }}')].[DBInstanceIdentifier,LatestRestorableTime]" --output text
  loop: "{{ db_identifier }}"
  register: recovery_points
  environment:
    AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
    AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
    AWS_SESSION_TOKEN: "{{ security_token }}"  
  -->until: (recovery_points.results|map(attribute='stdout')|list|length) > 0
  -->retries: 20
  -->delay: 30
  when: db_identifier is defined

这应该检查早于“时间”的恢复点,还有 db_identifier|lenght = 2。

- debug:
    msg: "{{ recovery_points.results | map(attribute='stdout') | list }}"

结果:

['', '']

我面临的问题是,当没有恢复点并且它返回这个空列表时,它说这个列表的长度是 2。

- debug:
    msg: "{{ recovery_points.results | map(attribute='stdout') | list | length }}"

结果:

2

有了这个,我想在 shell 命令中包含“直到”条件(你可以看到前面有 -->),但由于这个长度,我不能这样做。

我该如何解决它?

标签: amazon-web-servicesansiblejinja2

解决方案


推荐阅读