Ansible:没有名为'selectattr'的过滤器

时间:2018-02-10 04:05:55

标签: ansible jinja2 ansible-template

我正在进军Ansible世界。现在,我有一个任务,我想说:

  

如果任何文件列表不存在,则运行此任务

要做到这一点,我在这里有这段代码。

- name: Check if certificate already exists.
  stat:
    path: /etc/letsencrypt/live/{{ item }}/cert.pem
  with_items: "{{ cert_domains }}"  
  register: letsencrypt_cert

- name: Create the certs
  raw: certbot --nginx --noninteractive -d "{{ cert_domains.0 }}" -d "{{ cert_domains.1 }}"
  register: certbot_create
  changed_when: certbot_create.stdout
  when:
    - "{{ letsencrypt_cert.results|selectattr('stat','false') | length > 0 }}"

然而,当我运行我的过程时,它会遇到以下错误:

"msg": "The conditional check '{{ 
letsencrypt_cert.results|selectattr('stat','false') | length > 1 }}' failed.     
The error was: template error while templating string: no filter named 
'selectattr'. String: {{ letsencrypt_cert.results|selectattr('stat','false') 
| length > 0 }}\

在像LINQ这样的东西中,我会做这样的事情:

  list.where( n => n.equals(false)).Count() > 0

可能出现什么问题?

为了彻底,这是我的版本:

ansible 2.4.2.0

python version = 2.6.9

姓名:Jinja2版本:2.7.2

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我使用'pip install jinja2'安装了Jinja2。我需要做的是

'sudo yum install python-jinja2' 

让事情有效。

答案 1 :(得分:0)

试试这个:

- name: Create the certs
  raw: certbot --nginx --noninteractive -d "{{ cert_domains.0 }}" -d "{{ cert_domains.1 }}"
  register: certbot_create
  changed_when: certbot_create.stdout
  when:
    - letsencrypt_cert.results | selectattr('stat', 'defined') | map(attribute='stat') | selectattr('exists', 'equalto', false) | list | length > 0
相关问题