Joyce Babu
Joyce Babu

Reputation: 20654

Use expression as filter argument in ansible

I want to search the local filesystem for files matching a glob pattern, and get the path relative to a parent directory. Following is the code

- name: check for local configuration files
  debug:
    var: 
     - "{{ item | relpath('{{ playbook_dir }}/templates/httpd/{{ vhost.user }}') }}"
  with_fileglob: "{{ playbook_dir }}/templates/httpd/{{ vhost.user }}/*.j2"

The above code is not working since Ansible is unable to resolve the nested expression?

I am planning to pre-evaluate the expression into a variable using set_fact and pass it to relpath.

Is it possible to achieve the result without this extra variable declaration? Is it possible to use an expression as argument to a filter?

Upvotes: 0

Views: 743

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68239

You can access variables by name inside jinja expressions:

"{{ item | relpath(playbook_dir+'/templates/httpd/'+vhost.user) }}"

Upvotes: 4

Related Questions