Ansible - 基于dict的循环,统计不起作用

时间:2017-10-27 12:36:46

标签: ansible

我目前正在创建使用我的ansible-playbook自动获取Letsencrypt证书的可能性。

我想检查/etc/letsencrypt/domain.tld目录是否存在,如果不存在,我需要获取证书。

---
- name: LETSENCRYPT | Checking for existing certificates
  stat:
    path: /etc/letsencrypt/live/{{ item.value.server_name }}
  register: le_cert_exists
  with_dict: "{{ sites }}"
  when: item.value.letsencrypt | default(false) | bool

- name: DEBUG | Output result of le cert exists
  debug:
    var: le_cert_exists

- name: LETSENCRYPT | Output sites that need a new certificate
  debug:
    msg: Obtain certificate here
    var: item.item
  with_items: le_cert_exists.results
  when: item.stat.exists is defined and not item.stat.exists

到目前为止它正在工作,除了最后一个功能。最后一项任务不断被跳过或失败,并出现以下错误:

fatal: [-]: FAILED! => {"msg": "The conditional check 'item.stat.exists is defined and not item.stat.exists' failed. The error was: error while evaluating conditional (item.stat.exists is defined and not item.stat.exists): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stat'\n\nThe error appears to have been in '/path/to/main.yml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: LETSENCRYPT | Output sites that need a new certificate\n  ^ here\n"}

有没有人举例说明如何轻松完成这项工作?

我只需要在目录不存在时执行命令。

1 个答案:

答案 0 :(得分:0)

with_items: le_cert_exists.results错了,
这个with_items: "{{ le_cert_exists.results }}"是正确的。