Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolate a variable in a shell command run by Ansible

Tags:

ansible

I want to run Ansible task similar to this one:

---
- name: symlink the nodejs executable to node
  command: ln -sf "$(which nodejs)" /usr/bin/node
  sudo: True

I found that the part "$(which nodejs)" would not be interpolated to string, but taken literalle - consequently, creating a malformed symlink.

How can I interpolate this part to a proper string with Ansible command module?

like image 247
luqo33 Avatar asked Jul 07 '26 07:07

luqo33


1 Answers

Well,

I was probably to eager to ask first rather than try to find solution myself. Here is one solution:

---
- name: Find path to nodejs
  command: which nodejs
  register: nodejs_path

- name: symlink the nodejs executable to node
  command: "ln -sf {{ item }} /usr/bin/node"
  sudo: True
  with_items: nodejs_path.stdout
like image 106
luqo33 Avatar answered Jul 10 '26 11:07

luqo33



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!