Answers: <p>Use regex_escape filter from Ansible. It will help you escape the special character <code>$</code> in your string.</p>
  <pre><code>win_shell: 'C:\\TEMP\\myscript.ps1 -password {{ password | regex_escape }}'
  </code></pre>
  <p>Ref: <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_escape_filter.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_escape_filter.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-03-25T11:46:34.400Z
Answers: 2024-03-25T11:46:34.400Z
Answers: Amartya Sinha
Answers: <p>If you have to do this, I would suggest using win_template and copying the (file with the password in it) to the operating system and running it directly.</p>
  <p>Do it in a <code>block</code> and shred the file in an <code>always</code> section so you can be pretty sure you've trashed that credential when you're done.</p>
  <p>Don't copy it to c:\temp either, but copy it to your ansible users's local temp dir so at least it's not necessarily world readable.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-03-25T15:53:51.377Z
Answers: 2024-03-25T15:53:51.377Z
Answers: Peter Turner
Body: <p>Below is my script</p>
  <p><code>win_shell: 'C:\\TEMP\\myscript.ps1 -password {{ password }}'</code></p>
  <p>But when it executes, it get executed as below and as there is a dollar ($) in password its not working properly.</p>
  <p><code>&quot;cmd&quot;: &quot;C:\\\\TEMP\\\\myscript.ps1 -password abc$efg&quot;</code></p>
  <p>I want the script to be executed either as</p>
  <p><code>&quot;cmd&quot;: C:\\\\TEMP\\\\myscript.ps1 -password abc$efg,</code></p>
  <p>or as</p>
  <p><code>&quot;cmd&quot;: 'C:\\\\TEMP\\\\myscript.ps1 -password abc$efg',</code></p>
  <p>Any hits?</p>
  
Comments: CC BY-SA 4.0
Comments: 2024-03-25T13:34:20.393Z
Comments: Possibly you know this, but this is probably very bad form.  It's a good question - escaping strings in ansible is a bit of a chore, but you know that a command like this is going to divulge your password to the users of the OS and possibly be logged, right?
Comments: Peter Turner
Tags: <ansible>
Title: Run Ansible win_shell without double quotes
UserDisplayName: Nice Thomas
ThreadText: # Run Ansible win_shell without double quotes
  
  (Asked by: username_0 on 2024-03-16)
  
  Below is my script
  
  `win_shell: 'C:\\TEMP\\myscript.ps1 -password {{ password }}'`
  
  But when it executes, it get executed as below and as there is a dollar ($) in password its not working properly.
  
  `"cmd": "C:\\\\TEMP\\\\myscript.ps1 -password abc$efg"`
  
  I want the script to be executed either as
  
  `"cmd": C:\\\\TEMP\\\\myscript.ps1 -password abc$efg,`
  
  or as
  
  `"cmd": 'C:\\\\TEMP\\\\myscript.ps1 -password abc$efg',`
  
  Any hits?
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Possibly you know this, but this is probably very bad form.  It's a good question - escaping strings in ansible is a bit of a chore, but you know that a command like this is going to divulge your password to the users of the OS and possibly be logged, right?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-03-25. Score: 1
  
  Use regex\_escape filter from Ansible. It will help you escape the special character `$` in your string.
  
  ```
  win_shell: 'C:\\TEMP\\myscript.ps1 -password {{ password | regex_escape }}'
  
  ```
  
  Ref: <https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_escape_filter.html>
  
  
  ---
  
  ### Answer by username_1 on 2024-03-25. Score: 0
  
  If you have to do this, I would suggest using win\_template and copying the (file with the password in it) to the operating system and running it directly.
  
  Do it in a `block` and shred the file in an `always` section so you can be pretty sure you've trashed that credential when you're done.
  
  Don't copy it to c:\temp either, but copy it to your ansible users's local temp dir so at least it's not necessarily world readable.
  
  
  ---
  
  