This ansible task is supposed to check whether an specific version of an npm package is installed and, if not, install it. The when condition I am using involves a formatted string with a defined variable, pm2_version, yet for some reason my attempt to use that variable in the test is causing an error.
I expect that the expression f'pm2@{pm2_version}' should create a formatted string, but clearly that expectation is incorrect. What is the correct way to use a variable here?
Here are the relevant tasks:
- name: List installed packages
shell: 'npm list -g --depth=0'
args:
executable: /bin/bash
environment:
PATH: "{{ ansible_env.HOME }}/.nvm/versions/node/{{node_version}}/bin:{{ ansible_env.PATH }}"
register: npm_package_list
- name: Install pm2
shell: 'npm i -g pm2@{{pm2_version}}'
args:
executable: /bin/bash
environment:
PATH: '{{ ansible_env.HOME }}/.nvm/versions/node/{{node_version}}/bin:{{ ansible_env.PATH }}'
when: npm_package_list.stdout.find(f'pm2@{pm2_version}') == -1
But running the playbook, I get the following error:
FAILED! => {"msg": "The conditional check 'npm_package_list.stdout.find(f'pm2@{pm2_version}') == -1' failed. The error was: template error while templating string: expected token ',', got 'string'.