0

I want to pass an empty string as a variable value from ansible-playbook to PowerShell file.

Ansible Code:

vars:
 name: {{file_name}}

tasks:
  - name: run ps1 
    win_shell: "script.ps1 -database_name {{ name }}"
    no_log: false

Powershell code 

param(

[Parameter(Mandatory = $true)][AllowEmptyString()][string]$database_name 

)

So if there is no name in file_name then I want to send an empty string to PowerShell file! How can I do that?

2
  • Please edit your question and fix the formatting. As for your question, what steps have you tried so far that are not working for you? Commented Jun 2, 2020 at 4:45
  • Hi @mdaniel thanks. I want to pass an empty string as a variable ( like name = " ") to PowerShell file if there is no filename declared or not define. I tried with win_shell: "script.ps1 -database_name {{ name | default ( ' ' ) }}. but its not working Commented Jun 2, 2020 at 12:20

1 Answer 1

2

Likely you need | quote as in {{ name | default("") | quote }} in order to wrap the result in shell quote variables. Without that, jinja2 does not know that you want the empty string to render as "" instead of blank; you can also side-step that with {{ name | default('""') }} but that's highly unlikely to be what you want since if name is "hello world" then {{ name | default("") }} will render in your command as -database_name hello world which will almost certainly fail

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.