4

I've this task :

- name: script
  script: "scripts/script.sh"

And in this script I would like to use ansible var like {{myvar}}

Is that possible ?

THKS

2
  • Why not just try it? Commented Nov 30, 2017 at 11:26
  • :) I've already and it looks like its not working. I want to confirm or a workaround.. Commented Nov 30, 2017 at 11:31

2 Answers 2

8

As far as I am aware there isn't a way of directly using an Ansible variable within a script, as you describe.

There are a number of different ways of achieving the behavior I think you're after.

The first option would be to write your script in such a way that the desired variable value can be passed in as an argument. As described in the script module docs:

The script module takes the script name followed by a list of space-delimited arguments.

As such, you could run a script which takes the variable as an argument like:

- name: script
  script: scripts/script.sh {{myvar}}

Another option would be template out the script and use the Ansible template module to substitute the variable value directly into your script. You could then use either the command or shell Ansible modules to execute the script, configured from the template, on the remote node.

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

1 Comment

Thks, my var is a password so I will use the template solution.
0

If you have a long string with newlines or spaces, prefer use single quotas to pass it as one sting

Also you might find it useful endings of stdout or stdout_lines

- name: script
  script: "{{ role_path }}/scripts/script.sh '{{ myvar.stdout }}'"

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.