Black Dynamite
Black Dynamite

Reputation: 4147

Ansible: no filter named 'selectattr'

I'm taking a foray into the world of Ansible. Right now, I have a task where I am trying to say:

If any of a list of files don't exist, then run this task

To accomplish this, I have this piece of code right here.

- 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 }}"

However, when I run my process it greets me with the following error:

"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 }}\

In something like LINQ, i would do something like this:

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

What could be going wrong?

For the sake of thoroughness, here are my versions:

ansible 2.4.2.0

python version = 2.6.9

Name: Jinja2 Version: 2.7.2

Any help on this is greatly appreciated.

Upvotes: 1

Views: 5574

Answers (2)

Black Dynamite
Black Dynamite

Reputation: 4147

I had installed Jinja2 using 'pip install jinja2'. What I needed to do was

'sudo yum install python-jinja2' 

to get things working.

Upvotes: 2

xbalaji
xbalaji

Reputation: 1050

Try this:

- 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

Upvotes: 0

Related Questions