0
votes

I have a variable in variables.yml. In my ansible playbook, I would like to use that variable.But it starts with "/" character. Ansible throws the following error.

TASK [Display all variables/facts known for a host] ******************************************************************** fatal: [127.0.0.1]: FAILED! => {"msg": "template error while templating string: unexpected '/'. String: {{/forexcbm-r-0.0.1}}"}

When I remove "/" character from that variable, playbook is working properly.Is there anyway I can escape and use "/" character as variable prefix ? Thanks for your attention

set-prefix.yml

---
- name: Apigee Ansible Root
  hosts: localhost
  connection: local
  become: true
  vars_files:
    - variables.yml

  tasks:
  - name: Display all variables/facts known for a host
    debug:
      var: "{{ proxy_base_path_prefix }}"

variables.yml

proxy_base_path_prefix: /forexcbm-r-0.0.1
2
proxy_base_path_prefix: "/forexcbm-r-0.0.1" ??Luv33preet

2 Answers

0
votes

When using the var argument of debug you have to specify the name of the variable, not the content.

So change your play to:


- name: Apigee Ansible Root
  hosts: localhost
  connection: local
  become: true
  vars_files:
    - variables.yml

  tasks:
  - name: Display all variables/facts known for a host
    debug:
      var: proxy_base_path_prefix
  - name: Alternative display
    debug:
      msg: "{{ proxy_base_path_prefix }}"
0
votes

The parameter var: expands the expression by default

Wrong syntax (if the value of proxy_base_path_prefix shall be printed)

- debug:
    var: "{{ proxy_base_path_prefix }}"

Correct

- debug:
    var: proxy_base_path_prefix

Notes

1) It is possible "to specify the content of a variable" in the var: parameter of the debug module. The play below

- hosts: localhost
  vars:
    var1: var2
    var2: xxx
  tasks:
    - debug:
        var: "{{ var1 }}"

gives

  var2: xxx

2) /forexcbm-r-0.0.1 isn't a valid variable name.

Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

This is the reason of the error

... templating string: unexpected '/'.