2

I read this page of Ansible docs about Advanced Syntax topics.

---
my_unsafe_array:
    - !unsafe 'unsafe element'
    - 'safe element'

my_unsafe_hash:
    unsafe_key: !unsafe 'unsafe value'

What is the use of this unsafe keyword ?

It is telling us about the unsafe and raw strings.

I don't know what to make from that, cannot think of any use case . Can anybody specify any use case where this would be useful?

U880D
  • 8,601
  • 6
  • 24
  • 40
Luv33preet
  • 1,686
  • 7
  • 33
  • 66

1 Answers1

3

The use case is if you get some data from any URL, user input or anything else where you can't be sure if there are no curly braces.

Let's take this stupid example and let's assume file_str is getting there from outside (web api, script, ... ). Just run this playbook and enter {{ user_pass }} when prompted:

---

- hosts: localhost
  connection: local
  vars:
    - user_pass: user_secret_pass
      
  vars_prompt:
    - name: file_str
      prompt: File content
      private: False

  tasks:
    - shell:  
        echo {{ file_str }} > /tmp/echo_fstr

You will see user_secret_pass in /tmp/echo_fstr. With the !unsafe keyword you would see {{ user_pass }} int /tmp/echo_fstr .


In Ansible v2.3 it was not working due to Issue #23734 "!unsafe broken in Ansible 2.3.0.0?".

U880D
  • 8,601
  • 6
  • 24
  • 40
ProfHase85
  • 11,763
  • 7
  • 48
  • 66