How can I perform shell-safe escaping on a variable in Ansible?

0

Consider the following Ansible task:

- name: "Install Python packages"
  ansible.builtin.pip:
    name:
      - "foobar"
    extra_args: "--no-index --find-links=file://{{ role_path }}/files/pip/"

This task installs a Python package from a pre-downloaded package file which is stored with the Ansible role.

However, there is a gotcha here: if the path on disk where the Ansible role is located contains whitespace or a backslash, the --find-links argument will be parsed incorrectly.

What I need is some form of filter or function which will apply the equivalent of printf '%q' to the {{ role_path }} variable, escaping all whitespace or special characters. Is there such a function in Ansible or Jinja2?

ansible jinja2 python
2021-11-23 22:58:16
1

0

Found it: {{ role_path | quote }}

2021-11-23 23:04:40

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................