Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a script module with ansible variable

Tags:

ansible

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

like image 506
lovi Avatar asked Oct 20 '25 12:10

lovi


1 Answers

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.

like image 124
PicoutputCls Avatar answered Oct 22 '25 04:10

PicoutputCls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!