Ansible:没有名为 'selectattr' 的过滤器
Ansible: no filter named 'selectattr'
我正在涉足 Ansible 的世界。现在,我有一个任务要说:
If any of a list of files don't exist, then run this task
为了完成这个,我这里有这段代码。
- 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版本=2.6.9
名称:Jinja2 版本:2.7.2
非常感谢任何帮助。
试试这个:
- 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
我使用 'pip install jinja2' 安装了 Jinja2。我需要做的是
'sudo yum install python-jinja2'
让事情正常进行。
我正在涉足 Ansible 的世界。现在,我有一个任务要说:
If any of a list of files don't exist, then run this task
为了完成这个,我这里有这段代码。
- 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版本=2.6.9
名称:Jinja2 版本:2.7.2
非常感谢任何帮助。
试试这个:
- 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
我使用 'pip install jinja2' 安装了 Jinja2。我需要做的是
'sudo yum install python-jinja2'
让事情正常进行。